[ACCEPTED]-MySQLdb connection problems-connection

Accepted answer
Score: 54

Changing localhost to 127.0.0.1 solved my problem using MySQLdb:

db = MySQLdb.connect(
    host = '127.0.0.1', 
    user = 'root', 
    passwd = '', 
    db = 'testdb', 
    port = 3000)

Using 4 127.0.0.1 forces the client to use TCP/IP, so that 3 the server listening to the TCP port can 2 pickle it up. If host is specified as localhost, a Unix 1 socket or pipe will be used.

Score: 12

add unix_socket='path_to_socket' where path_to_socket should be the path of the MySQL 1 socket, e.g. /var/run/mysqld/mysqld2.sock

Score: 3

Make sure that the mysql server is listening 16 for tcp connections, which you can do with 15 netstat -nlp (in *nix). This is the type 14 of connection you are attempting to make, and 13 db's normally don't listen on the network 12 by default for security reasons. Also, try 11 specifying --host=localhost when using the 10 mysql command, this also try to connect 9 via unix sockets unless you specify otherwise. If 8 mysql is not configured to listen for tcp connections, the 7 command will also fail.

Here's a relevant 6 section from the mysql 5.1 manual on unix sockets and troubleshooting 5 connections. Note that the error described 4 (2002) is the same one that you are getting.

Alternatively, check 3 to see if the module you are using has an 2 option to connect via unix sockets (as David 1 Suggests).

Score: 3

I had this issue where the unix socket file 4 was some place else, python was trying to 3 connect to a non-existing socket. Once this 2 was corrected using the unix_socket option, it 1 worked.

Score: 2

Mysql uses sockets when the host is 'localhost' and 16 tcp/ip when the host is anything else. By 15 default Mysql will listen to both - you 14 can disable either sockets or networking 13 in you my.cnf file (see mysql.com for details).

In 12 your case forget about the port=3000 the 11 mysql client lib is not paying any attention 10 to it since you are using localhost and 9 specify the socket as in unix_socket='path_to_socket'.

If 8 you decided to move this script to another 7 machine you will need to change this connect 6 string to use the actual host name or ip 5 address and then you can loose the unix_socket 4 and bring back the port. The default port 3 for mysql is 3306 - you don't need to specify 2 that port but you will need to specify 3000 1 if that is the port you are using.

Score: 0

As far as I can tell, the python connector 11 can ONLY connect to mysql through a internet 10 socket: unix sockets (the default for the 9 command line client) is not supported.

In 8 the CLI client, when you say "-h localhost", it 7 actually interprets localhost as "Oh, localhost? I'll 6 just connect to the unix socket instead", rather 5 than the internet localhost socket.

Ie, the 4 mysql CLI client is doing something magical, and 3 the Python connector is doing something 2 "consistent, but restrictive".

Choose your 1 poison. (Pun not intended ;) )

More Related questions