[英]PyMySQL can't connect to MySQL on localhost
I'm trying to connect to MySQL on localhost using PyMySQL:我正在尝试使用 PyMySQL 连接到本地主机上的 MySQL:
import pymysql
conn = pymysql.connect(db='base', user='root', passwd='pwd', host='localhost')
but (both on Python 2.7 and Python 3.2) I get the error:但是(在 Python 2.7 和 Python 3.2 上)我得到了错误:
socket.error: [Errno 111] Connection refused
socket.error: [Errno 111] 连接被拒绝
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (111)")
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (111)")
I'm sure mysqld is running because I can connect using mysql command or phpMyAdmin.我确定 mysqld 正在运行,因为我可以使用mysql命令或 phpMyAdmin 进行连接。 Moreover, I can connect using MySQLdb on Python 2 with nearly the same code:
此外,我可以使用几乎相同的代码在 Python 2 上使用 MySQLdb 进行连接:
import MySQLdb
conn = MySQLdb.connect(db='base', user='root', passwd='pwd', host='localhost')
It seems that the problem is on PyMySQL side rather than MySQL but I have no idea how to solve it.似乎问题出在 PyMySQL 而不是 MySQL 方面,但我不知道如何解决它。
Two guesses:两种猜测:
Run mysqladmin variables | grep socket
运行
mysqladmin variables | grep socket
mysqladmin variables | grep socket
to get where the socket is located, and try setting up a connection like so: mysqladmin variables | grep socket
以获取mysqladmin variables | grep socket
所在的位置,并尝试像这样设置连接:
pymysql.connect(db='base', user='root', passwd='pwd', unix_socket="/tmp/mysql.sock")
Run mysqladmin variables | grep port
运行
mysqladmin variables | grep port
mysqladmin variables | grep port
and verify that the port is 3306. If not, you can set the port manually like so: mysqladmin variables | grep port
并验证端口是否为 3306。如果不是,您可以像这样手动设置端口:
pymysql.connect(db='base', user='root', passwd='pwd', host='localhost', port=XXXX)
Seems like changing localhost
to 127.0.0.1
fixes the error, at least in my configuration.似乎将
localhost
更改为127.0.0.1
修复了错误,至少在我的配置中是这样。 If it doesn't, I would look for errors in tcp sockets connection
and, of course, post it as a bug in pymysql
bugtrack.如果没有,我会在
tcp sockets connection
查找错误,当然,将其作为pymysql
错误发布。
I solved the issue by replacing localhost
with 127.0.0.1
and changing the password to my MYSQL database password as shown below;我通过用
127.0.0.1
替换localhost
并将密码更改为我的 MYSQL 数据库密码来解决这个问题,如下所示;
conn = pymysql.connect(
host = '127.0.0.1',
port = 3306,
user = 'root',
passwd = 'XXXXXXXXX',
db = 'mysql'
)
I met the same question and my solution is as follows:我遇到了同样的问题,我的解决方案如下:
ssh -fN -L 3307:mysql_host:3306 ssh_user@ssh_host
in my terminal.ssh -fN -L 3307:mysql_host:3306 ssh_user@ssh_host
。conn = pymysql.connect(db='base', user='root', passwd='pwd', host='localhost')
This error occurs because database does not support link directly.出现此错误是因为数据库不支持直接链接。
I asked why socket worked but not TCP and the answer was that bind-address
in /etc/my.cnf
was not set correctly.我问为什么套接字有效,但 TCP 无效,答案是
/etc/my.cnf
中的bind-address
设置不正确。 This could be your problem too since the socket methods works just fine but the TCP one does not.这也可能是您的问题,因为套接字方法工作得很好,但 TCP 方法却没有。
那些正在努力从 dockerised flask-sqlalchemy 或使用 pymysql 连接 localhost MySQL 的人,请查看此线程,非常有用如何将本地托管的 MySQL 数据库与 docker 容器连接
This worked for me:这对我有用:
import pymysql
db = pymysql.connect(host="localhost",port=8889,user="root",passwd="root")
cursor=db.cursor()
cursor.execute("SHOW DATABASES")
results=cursor.fetchall()
for result in results:
print (result)
if you want to find the port # go to mysql in terminal, and type:如果你想找到端口 # 在终端中转到 mysql,然后输入:
SHOW VARIABLES WHERE Variable_name = 'hostname';
SHOW VARIABLES WHERE Variable_name = 'port';
I had this same problem on AWS - and turns out that my security group was blocking the connection.我在 AWS 上遇到了同样的问题 - 结果是我的安全组阻止了连接。 I temporarily opened up all connections and voila!
我暂时打开了所有连接,瞧! It connected!
它连接了!
Do you have any type of FW or host-based FW that could be blocking the connection?您是否有任何类型的 FW 或基于主机的 FW 可能会阻止连接? I thought it was my code and all was fine.
我以为这是我的代码,一切都很好。 Also check the port you are connecting on.
还要检查您正在连接的端口。
如果您使用的是 Docker,您可能需要使用host.docker.internal
而不是localhost
。
我设法通过使用没有任何引用的端口来解决我的问题,如下所示:
port = 3306,
You need to add the port to the connection as well.您还需要将端口添加到连接中。 Try this and it works fine.
试试这个,它工作正常。
pymysql(Module Name).connect(host="localhost", user="root", passwd="root", port=8889, db="db_name")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.