views:

1455

answers:

5

I'm having trouble with the MySQLdb module.

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

(I'm using a custom port)

the error I get is:

Error 2002: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
Which doesn't make much sense since that's the default connection set in my.conf.. it's as though it's ignoring the connection info I give.. The mysql server is definitely there:
[root@baster ~]# mysql -uroot -p -P3000
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use testdb;
Database changed
mysql> 

I tried directly from the python prompt:

>>> db = MySQLdb.connect(user='root', passwd='', port=3000, host='localhost', db='pyneoform')
Traceback (most recent call last):
File "", line 1, in 
File "/usr/lib64/python2.5/site-packages/MySQLdb/__init__.py", line 74, in Connect
return Connection(*args, **kwargs)
File "/usr/lib64/python2.5/site-packages/MySQLdb/connections.py", line 169, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")
>>>

I'm confused... :(

A: 

Maybe try adding the keyword parameter unix_socket = None to connect()?

David Zaslavsky
Just tried that, it doesn't like None, so I did '' instead and now it complains that '' isn't a valid socket. weird, it's like it only wants to connect via socket..
Ian
+2  A: 

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

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

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

Dana the Sane
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:3300 0.0.0.0:* LISTEN 5361/mysqld
Ian
Yeah, my bad, in the examples above I wrote port 3000. I fixed that.. and strangely, the problem persists. heh. ugh.
Ian
Have you restarted the server since you updated the configuration?
Dana the Sane
I restarted earlier today.. but I didn't modify the server, I just updated the script to use 3300 instead of 3000 (that was a mistake in the code).
Ian
I just noticed something weird..[root@baster httpd]# mysql -uroot -p -P666Enter password: mysql>Apparently mysql ignores my port number.. :|
Ian
I think that unless you specify a host, it connects via unix socket by default. Try using -h or --host as above, this should also make the command fail.
Dana the Sane
[root@baster httpd]# mysql -uroot -p -P3000 -h=localhostEnter password: ERROR 2005 (HY000): Unknown MySQL server host '=localhost' (2)[root@baster httpd]# Aha.
Ian
Cancel that. I'm stupid.[root@baster httpd]# mysql -uroot -p -P3300 -hlocalhostEnter password: mysql>
Ian
I checked my.conf, "skip-networking" isn't there either. This is so weird.
Ian
Have you tried to connect with your code again, using port 3300?
Dana the Sane
Yeah, doesn't work.. I did get it to work by putting in the unix_socket however.. but this is really weird that it wont work outside of the socket.. boo.
Ian
Well, it's probably something silly. Hopefully a proper mysql guru will wander by soon.
Dana the Sane
Yeah, probably, I'm new to linux in general, so sockets are a bit strange for me.. I'm used to doing everything over TCP/IP.
Ian
Yup! Just found out. Mysql ignores custom ports and goes to socket mode if you use "localhost" and not "127.0.0.1".http://www.reddit.com/r/Python/comments/8d4ic/im_new_to_python_wheres_the_best_placeforum_to/c08wy9s
Ian
Great that you found it, that's a useful piece of info.
Dana the Sane
A: 

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

Kalisky
A: 

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

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

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

Choose your poison. (Pun not intended ;) )

Ch'marr
A: 

i had this issue where the unix socket file was some place else, python was trying to connect to a non-existing socket. once this was corrected using the unix_socket option, it worked.

thushara