I am trying to establish a connection to MySQL that is running locally using python. i use the following code
我正在尝试建立与使用python在本地运行的MySQL的连接。我使用以下代码
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.
我在虚拟机上工作,我在VM上运行Python和MySQL。我无法建立连接。
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??
我试过提供VM的IP地址代替localhost但仍然无法正常工作。我也试过指定端口号。连接是否不同,因为它在VM上运行?
2 个解决方案
#1
0
When you try to connect directly to mysql Database from command line does it work?
当您尝试从命令行直接连接到mysql数据库时它是否有效?
What OS is your VM running? If its a Unix then is there a mysqld process runnning?
您的VM运行的操作系统是什么?如果它是一个Unix那么有一个mysqld进程运行?
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.
无论如何,这可能与Python没有任何关系。你的代码snipppet看起来不错。您只需要解决连接问题。请查看此页面,详细了解如何找到此问题的根源。
http://dev.mysql.com/doc/refman/5.5/en/can-not-connect-to-server.html
I hope that this helps.
我希望这个对你有用。
#2
0
Assuming your client and mysql server on same machine as you said and mysql server is running you can try following
假设您的客户端和mysql服务器在您所说的同一台机器上运行并且mysql服务器正在运行,您可以尝试以下操作
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
在你的my.cnf中,这将主要在linux中的/etc/mysql/my.cnf或在windows上检查你的mysql安装,找到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
查找bind-address指令并为您的VM主机设置IP地址。如果在linux上,您可以在Windows上使用ifconfig或ipconfig查看您的VM ip
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
/etc/init.d/mysqld在基于debian的发行版上重启,为redhat服务mysqld重启或在windows上重启mysql服务
#1
0
When you try to connect directly to mysql Database from command line does it work?
当您尝试从命令行直接连接到mysql数据库时它是否有效?
What OS is your VM running? If its a Unix then is there a mysqld process runnning?
您的VM运行的操作系统是什么?如果它是一个Unix那么有一个mysqld进程运行?
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.
无论如何,这可能与Python没有任何关系。你的代码snipppet看起来不错。您只需要解决连接问题。请查看此页面,详细了解如何找到此问题的根源。
http://dev.mysql.com/doc/refman/5.5/en/can-not-connect-to-server.html
I hope that this helps.
我希望这个对你有用。
#2
0
Assuming your client and mysql server on same machine as you said and mysql server is running you can try following
假设您的客户端和mysql服务器在您所说的同一台机器上运行并且mysql服务器正在运行,您可以尝试以下操作
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
在你的my.cnf中,这将主要在linux中的/etc/mysql/my.cnf或在windows上检查你的mysql安装,找到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
查找bind-address指令并为您的VM主机设置IP地址。如果在linux上,您可以在Windows上使用ifconfig或ipconfig查看您的VM ip
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
/etc/init.d/mysqld在基于debian的发行版上重启,为redhat服务mysqld重启或在windows上重启mysql服务