pymysql无法连接到mysql [英] pymysql cannot connect to mysql

查看:305
本文介绍了pymysql无法连接到mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pymsql连接到MySQL数据库,主机为'115.28.236.225',并使用默认端口:3306.代码如下(db_connect.py):

I am trying to use pymsql to connect to MySQL db, the host is '115.28.236.225', and using the default port: 3306. The code is as bellow (db_connect.py):

import pymysql

def connDB():
    conn=pymysql.connect(host='115.28.236.225',user='root',passwd='xxx',db='yyy',charset='utf8', port=3306)
    cur=conn.cursor();
    return (conn,cur);

conn,cur=connDB()

我使用python db_connect.py运行,但是,我收到错误消息pymysql.err.OperationalError: (1045, u"Access denied for user 'root'@'58.196.159.221' (using password: YES)"),我不知道主机"58.196.159.221"来自何处,该主机与代码中的主机不对应.

I use python db_connect.py to run, however, I got the error message pymysql.err.OperationalError: (1045, u"Access denied for user 'root'@'58.196.159.221' (using password: YES)") , I don't know where the host '58.196.159.221' comes from, which does not correspond to the one in the code.

(我尝试使用MySql Workbench连接到MySQL,并且可以正常工作,因此,我确定python代码一定有问题).

(I have tried to use MySql Workbench to connect to MySQL, and that worked, so, I am sure it must be something wrong with the python code).

我该如何解决?预先感谢!

How can I solve this? Thanks in advance!

推荐答案

pymysql很棒,因为它纯粹是用python实现的-没有外部依赖关系.

pymysql is awesome because it is a implemented purely in python - no external dependencies.

我敢打赌,您没有为来自外部来源的root帐户设置适当的权限. 您看到pymysql.err.OperationalError: (1045,u"Access denied for user 'root'@'58.196.159.221'的原因是因为您可能只能从'root'@'localhost'访问mysql,58.196.159.221是运行python程序的系统的IP地址-不相信我吗?运行ifconfig并检查您的IP地址.

I'm willing to bet that you don't have the proper permissions set for the root account from external sources. The reason why you are seeing pymysql.err.OperationalError: (1045,u"Access denied for user 'root'@'58.196.159.221' is because you probably only have access to mysql from 'root'@'localhost', 58.196.159.221 is the IP address of the system your python program is running from - don't believe me? run ifconfig and check your IP address.

修复: 连接到mysql控制台并运行以下命令:

fix: Connect to the mysql console and run the following:

GRANT ALL ON root.* TO 'root'@'58.196.159.221' IDENTIFIED BY 'ENTER_PASSWORD_HERE' ;
FLUSH PRIVILEGES;

这将允许远程访问mysql.

This will allow for remote access to mysql.

如果只想从本地主机访问mysql,请运行以下命令:

If you want to only access mysql from the local host you'd run this:

GRANT ALL ON root.* TO 'root'@'localhost' IDENTIFIED BY 'ENTER_PASSWORD_HERE' ;
FLUSH PRIVILEGES;

如果要授予来自任何IP地址的访问权限,请执行以下操作:

If you want to grant access from any IP address:

GRANT ALL ON root.* TO 'root'@'%' IDENTIFIED BY 'ENTER_PASSWORD_HERE' ;
FLUSH PRIVILEGES;

FLUSH PRIVILEGES从mysql数据库重新加载特权,在更改用户权限后,这是必需的.

FLUSH PRIVILEGES reloads the privileges from the mysql database, which is necessary after you make a change to user permissions.

这篇关于pymysql无法连接到mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆