新装的 MySQL 通常会出现这样的情况:无法远程连接,但是本地连接是正常的.
问题原因
新装的 MySQL 通常默认的用户是 root, 而为了安全起见, root 用户是不允许远程连接.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.04 sec)
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select user, host from user;
+-----------+-----------+
| user | host |
+-----------+-----------+
| mysql.sys | localhost |
| root | localhost |
+-----------+-----------+
2 rows in set (0.00 sec)
解决办法
- 暴力修改(不安全,不合理,不建议,也就不介绍了)
- 新增一个对应权限的用户,并启用远程连接(安全、可靠、无污染).
# 新增用户及密码,并授予对应的权限
grant all privileges on yourDatabase.* to "yourUser"@"%" identified by "yourPassword";
# 刷新权限
flush privileges;