简体   繁体   English

PyMySQL 无法连接到本地主机上的 MySQL

[英]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:两种猜测:

  1. 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")
  2. 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:我遇到了同样的问题,我的解决方案如下:

  1. Run 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
  2. Then input your ssh password然后输入你的ssh密码
  3. 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.

相关问题 PyMySQL:- OperationalError: (2003, “无法连接到‘本地主机’上的 MySQL 服务器([Errno 111] 连接被拒绝)”) - PyMySQL:- OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)") pymysql-Google cloud-OperationalError:(2003年,“无法连接到MySQL服务器, - pymysql - google cloud - OperationalError: (2003, "Can't connect to MySQL server on 无法连接到“本地主机”上的 MySQL 服务器 - Can't connect to MySQL server on 'localhost 使用PyMySQL,我无法连接到RDS - Using PyMySQL, I can't connect to RDS 无法使用PyMySQL连接到Cloud SQL - Can't connect to Cloud SQL using PyMySQL python不连接mysql。 pymysql.err.OperationalError:(1045,“用户'root'@'localhost'的访问被拒绝(使用密码:NO)”) - python doesn't connect mysql. pymysql.err.OperationalError: (1045, “Access denied for user 'root'@'localhost' (using password: NO)”) pymysql无法连接到mysql - pymysql cannot connect to mysql pymysql:2003,“无法连接到%r(%s)上的MySQL服务器”%(self.host,e)) - pymysql: 2003, “Can't connect to MySQL server on %r (%s)” % (self.host, e)) Mysql-python无法连接到Windows 7上的本地主机服务器 - Mysql-python can't connect to localhost server on Windows 7 “无法连接到'localhost'(10061)上的MySQL服务器”错误在python中 - “Can't connect to MySQL server on 'localhost' (10061)” error in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM