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