#!/usr/bin/python # -*- coding: UTF-8 -*- import pymysql # 打开数据库连接 db = pymysql.connect("111.230.43.239","root","password","RUNBOO" ) # 使用cursor()方法获取操作游标 cursor = db.cursor() # 使用execute方法执行SQL语句 cursor.execute("SELECT VERSION()") # 使用 fetchone() 方法获取一条数据 data = cursor.fetchone() print ("Database version : %s " % data) # 关闭数据库连接 db.close()
报错:pymysql.err.InternalError: (1130, u”Host ‘218.20.44.100’ is not allowed to connect to this MySql server,MySQL不允许从远程访问
解决步骤:
(1)添加用户允许从任何主机连接到mysql服务器
例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话。
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; FLUSH PRIVILEGES;
如果你想允许用户root从ip为192.168.1.1的主机连接到mysql服务器,并使用password作为密码
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.1' IDENTIFIED BY 'password' WITH GRANT OPTION; FLUSH PRIVILEGES;
如果你想允许用户root从ip为192.168.1.1的主机连接到mysql服务器的dk数据库,并使用password作为密码
GRANT ALL PRIVILEGES ON dk.* TO 'root'@'192.168.1.1' IDENTIFIED BY 'password' WITH GRANT OPTION; FLUSH PRIVILEGES;
已经将任何主机允许连接到mysql服务器:
成功