Reputation: 1473
Using MySQLdb on Cygwin and Python, I get the below error. I dont want to install MySQLDB on Cygwin. Is there a way that i can use the windows version of MySQL and just connect to it.
Python 2.7.5 (default, Oct 2 2013, 22:34:09)
[GCC 4.8.1] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> db = MySQLdb.connect(host="localhost", user="root", passwd="root" )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/MySQLdb/connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysql.sock' (9)")
I created the my.cnf file as below
$ cat /etc/my.cnf
[client]
host = 127.0.0.1
port = 3306
socket = /var/run/mysql/mysql.sock
I also created the mysql.sock file as below.
$ pwd
/var/run/mysql
$ cat mysql.sock
[client]
port=3306
socket=/var/mysql/mysql.sock
[mysqld]
port=3306
socket=/var/mysql/mysql.sock
key_buffer_size=16M
max_allowed_packet=8M
I dont know what else is needed for this to run. Any ideas?
Upvotes: 0
Views: 298
Reputation: 8615
The socket should be created automatically by the MySQL server during startup, but it's not always in the folder that MySQLdb
expects. You may find it at /tmp/mysql.sock
, which is the default value of the --socket
command line argument. If not, you can search for it with the following bash command:
$ find / -name 'mysql.sock' 2>/dev/null
If MySQLdb
by default looks for the socket at /var/lib/mysql/mysql.sock
, but you find that your socket is somewhere else, you can use the unix_socket
argument to the connection function, e.g.:
conn = MySQLdb.connect(user=myuser, passwd=mypass, unix_socket='/tmp/mysql.sock')
The host
and port
arguments are optional, and normally when you want to connect through a unix socket you can omit them both, because you want the default host and you're not using a TCP port.
Upvotes: 1
Reputation: 111
I haven't yet found a complete solution but if you use "127.0.0.1" instead of "localhost" it will work. Not sure why yet but localhost works with curl from cygwin but not mysql or python.
Update: You don't need to define either the .cnf or .sock file
Upvotes: 0