我正在尝试连接在我的 VPS 上运行的 MySql(假设主机名 db.example.com)。
问题是:在 db.example.com 上,我可以在本地连接 mysql(从数据库服务器本身),但我不能远程(从我的开发机器)。
我应该如何检查设置?
我可以通过 ssh 连接到 db.example.com 并通过运行以下命令进行检查。
在服务器 db.example.com(ubuntu 18.04) 上:
$ mysql -u satrex -p
> success
mysql> select user, host from mysql.user where user = 'satrex';
+--------+------+
| user | host |
+--------+------+
| satrex | % |
+--------+------+
$ sudo ufw status
>Status: active
To Action From
-- ------ ----
3306 ALLOW Anywhere
Apache Full ALLOW Anywhere
3306 (v6) ALLOW Anywhere (v6)
Apache Full (v6) ALLOW Anywhere (v6)
$ sudo lsof -i:3306
> COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysqld 597 mysql 43u IPv4 16358 0t0 TCP *:mysql (LISTEN)
$ sudo cat /etc/mysql/mysql.conf.d/mysqld.cnf
>
[mysqld]
port = 3306
....
# bind-address = 127.0.0.1
bind-address = 0.0.0.0
然后我尝试从开发机器连接。
在开发机器上(macOS catalina)
我可以通过 telnet 连接到服务器。
$ telnet db.example.com 80
> Trying 160.16.xxx.xxx ...
Connected to db.example.com.
但在端口 3306 上被拒绝。
$ telnet db.example.com 3306
> Trying 160.16.xxx.xxx ...
telnet: connect to address 160.16.xxx.xxx: Connection refused
telnet: Unable to connect to remote host
和mysql客户端一样。
$ mysql --host=db.example.com --user=satrex --password
> Enter password:
> ERROR 2003 (HY000): Can't connect to MySQL server on 'db.example.com:3306' (61)
我想知道我缺少连接的基本设置。 前面应该是配置MySQL本身。
现在 db.myself 正在监听 ufw 显示的 80 端口, 另一方面,端口 3306,ufw 显示为侦听但没有运气。
答案 0 :(得分:0)
首先您需要检查您的安全组是否允许 EC2 实例访问 0.0.0.0(全局)
然后在您的 SQL EC2 实例上检查绑定地址 sudo grep -R bind /etc/mysql
如果它看起来像这样,那么我们需要修改一些东西
转到此路径 /etc/mysql/mysql.conf.d
并修改此文件 sudo vim mysqld.cnf
绑定地址应该是 bind-address = 0.0.0.0
终于到了重启mysql服务的时候了
sudo systemctl restart mysql.service
对我来说,这个解决方案是有效的,如果它对你有用的话,谢谢 如果你仍然觉得有些困难look into this question