通过 Python 连接远程 MySQL 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12972867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:18:00  来源:igfitidea点击:

Connect on remote MySQL database through Python

pythonmysql

提问by user706838

I have tried the following script but unfortunately doesn't work. I am using a free MySQL database provider. Any ideas?

我尝试了以下脚本,但不幸的是不起作用。我正在使用免费的 MySQL 数据库提供程序。有任何想法吗?

import MySQLdb

myDB = MySQLdb.connect(host="208.11.220.249",port=3306,user="XXXXX",passwd="XXXXX",db="XXXXX")
cHandler = myDB.cursor()
cHandler.execute("SHOW DATABASES")
results = cHandler.fetchall()
for items in results:
    print items[0]

Currently, I am getting the following error:

目前,我收到以下错误:

super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1044, "Access denied for user 'XXXXX'@'%' to database 'XXXXX'")

回答by solaimuruganv

 GRANT ALL
 ON  *.*
 TO [email protected]  -- client ip address
 IDENTIFIED BY 'pwd';

Edit

编辑

This is SQL that you'd run on the database in order to ensure that the userhas access to everything. pwdis the user's password.

这是您在数据库上运行的 SQL,以确保可以user访问所有内容。pwduser的密码。

Basically, this answer assumes that the connection issue is a credentials issue.

基本上,此答案假定连接问题是凭据问题。

回答by Ranjith Ramachandra

This is what I would do

这就是我会做的

  1. See if the port is actually open on that machine.
  2. On the machine you are connecting from, open console/cmd/terminal and see if you can connect using mysql -u XXXX -h 208.11.220.249 -p. If your mysql client can not connect, then there is no way you can connect using python
  1. 查看该端口是否在该机器上实际打开。
  2. 在您连接的机器上,打开控制台/cmd/终端,看看您是否可以使用mysql -u XXXX -h 208.11.220.249 -p. 如果你的mysql客户端连接不上,那你就没有办法用python连接了

回答by mahdieh Saeed

You don't have permission for connecting to database with this user. Follow these steps:

您无权使用此用户连接到数据库。按着这些次序:

1.try connecting to mysql DB: mysql -h208.11.220.249 -uXXXXX -pXXXXX XXXXX

1.尝试连接mysql数据库:mysql -h208.11.220.249 -uXXXXX -pXXXXX XXXXX

2.If you don't have permission for connecting to DB ,try creating user that has remote permission

2.如果您没有连接数据库的权限,请尝试创建具有远程权限的用户

GRANT ALL ON DB.* -- Database name TO user@ip -- client ip address IDENTIFIED BY 'pwd';

GRANT ALL ON DB.* -- 数据库名称 TO user@ip -- 客户端 IP 地址 IDENTIFIED BY 'pwd';

3.on the last check my.cnf . "bind-address" must be 0.0.0.0 If you want to connect all remote addresses.

3.在最后检查 my.cnf 。"bind-address" 必须为 0.0.0.0 如果要连接所有远程地址。

回答by DuckPool

open your database in mysql and type the following :

在 mysql 中打开您的数据库并键入以下内容:

mysql> GRANT ALL ON *.* TO root@'x' IDENTIFIED BY 'Your-password'

where x is the ip address of the user that wants to access your db.

其中 x 是想要访问您的数据库的用户的 IP 地址。

use this link for more information: How to Allow MySQL Client to Connect to Remote MySQL server

使用此链接了解更多信息: 如何允许 MySQL 客户端连接到远程 MySQL 服务器