Python与远程MySQL,通过Python连接到远程MySQL

在尝试通过Python的PyMySQL库连接到远程MySQL服务器时遇到了连接拒绝错误。问题在于MySQL服务器可能只绑定了本地主机。解决方案是编辑MySQL配置文件`/etc/mysql/my.cnf`,将`bind-address`从`127.0.0.1`改为`0.0.0.0`,然后重启MySQL服务。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值