I am trying to establish a connection to MySQL that is running locally using python. i use the following code
import MySQLdb
db = MySQLdb.connect("localhost","username","password","dbname")
I am working on a virtual machine and I am running both the Python and MySQL on the VM. I am not able to make a connection.
I get this errror
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")
I have tried providing the ip address of the VM in place of localhost and still didn't work. I tried specifying the port number too. Is the connection different because it runs on a VM??
When you try to connect directly to mysql Database from command line does it work?
What OS is your VM running? If its a Unix then is there a mysqld process runnning?
In any case this may not have anything to do with Python. Your code snipppet looks ok. You just need to resolve the connectivity issue. Look at this page for details on how you could get to to the root of this problem.
http://dev.mysql.com/doc/refman/5.5/en/can-not-connect-to-server.html
I hope that this helps.
Assuming your client and mysql server on same machine as you said and mysql server is running you can try following
In your my.cnf
which would be mostly in /etc/mysql/my.cnf
in linux or check with your mysql installation on windows to find my.cnf
look for bind-address
directive and set IP address to your VM host.If on linux you can see your VM ip using ifconfig
or ipconfig
on windows
you may also try commenting out skip-networking
line if not already to allow remote clients
Restart mysqld
/etc/init.d/mysqld restart
on debian based distros, service mysqld restart
for Redhat or restart mysql service on windows
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.