且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过Python连接到远程MySQL

更新时间:2023-12-05 14:26:28

很可能MySQL绑定到localhost。假设您使用的是Linux计算机:



在远程计算机上,编辑 /etc/mysql/my.cnf



查找 bind-address = 127.0.0.1 ,将其编辑为 bind-address = 0.0.0.0



重新启动MySQL

阅读更多: http://dev.mysql。 com / doc / refman / 5.1 / en / server-options.html#option_mysqld_bind-address

I have installed MySQL on a remote server. I also created some databases with some tables in them, all via SSH.

I have created the following users:

CREATE USER 'myname'@'localhost' IDENTIFIED BY 'mypassword';
CREATE USER 'remotMe'@'myIP' IDENTIFIED BY 'mypassword';

GRANT PRIVILEGES * . * TO 'myname'@'localhost';
GRANT PRIVILEGES * . * TO 'remoteMe'@'myIP';

Now, I would like to connect to this MySQL server via Python. I have installed PyMySQL. I am trying out the following code:

import pymysql
conn = pymysql.connect(host='ServerIP', port=3306, user='remoteMe', passwd='mypassword', db='myDB') 

But this gives me a connection refused error. Do I need to configure something else to make this work?

It's likely MySQL is bound to localhost. Assuming you're using a Linux machine:

On your remote machine, edit /etc/mysql/my.cnf

Find bind-address=127.0.0.1, edit it to bind-address=0.0.0.0, or just remove the line.

Restart MySQL

Read more: http://dev.mysql.com/doc/refman/5.1/en/server-options.html#option_mysqld_bind-address