Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL-python Cannot connect to server

Tags:

python

mysql

This is driving me crazy.

I have Python 2.5 and MySQL-python-1.2.3 from the .exe available here installed on Vista.

I have been making .php pages over the past few weeks and connected fine in order to test them in my browser.

$dbcnx=@mysql_connect("localhost", "root", "mypassword")

I have also been using mysql commandline with

mysql.exe -uroot -pmypassword just fine.

However, when I try to use MySQLdb with

conn = MySQLdb.connect(host = "localhost", user = "root", passwd = "mypassword") 

I get

OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")

I have checked that MySQL is running in services. I checked my MySQL config in my.ini and it is running on port=3306. I have even uninstalled and reinstalled MySQL 5.1. I read several pages of connection problems and answers on Google, but haven't yielded anything. It must be something simple I am overlooking, but does anyone have any more ideas?

like image 683
Amy Avatar asked Feb 02 '11 05:02

Amy


People also ask

Why MySQL server is not connecting?

normally means that there is no MySQL server running on the system or that you are using an incorrect Unix socket file name or TCP/IP port number when trying to connect to the server. You should also check that the TCP/IP port you are using has not been blocked by a firewall or port blocking service.

Can't connect to MySQL server on remote host?

To allow remote access to MySQL, you have to comment out bind-address (you did) and skip-networking in the configuration file. Next, you have to make sure the user is allowed remote access. Check your user with this: SELECT User, Host FROM mysql.


1 Answers

Don't use the windows version (all my relevant experience is on Linux), but I'd be willing to bet it will work if you use 127.0.0.1 in place of localhost.

Edit: -- a bit of explanation.

Firstly, check the entries in mysql.user table, for the host field (although by default, you probably have three entries for root which cover all bases). Most likely, your problem is caused by the fact that the MySQL service is listening on 127.0.0.1:3306, not localhost:3306, and your hosts file or other routing config is not being invoked properly by the python interpreter.

like image 159
simon Avatar answered Oct 02 '22 13:10

simon