跳转至

MySQL允许root远程登录

MySQL默认用户登录是本地,也就是localhost

我的MySQL版本是:8.0.26

进入mysql:

Text Only
1
mysql -uroot -p

进入mysql后使用mysql数据库:

Text Only
1
mysql> USE mysql

查询现有用户的允许登录方式:

Text Only
1
mysql> SELECT host,user FROM user;

可以看到root的host是localhost:

Text Only
1
2
3
4
5
6
7
8
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| localhost | root             |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
+-----------+------------------+

使用更新语句,使root用户能够以任何ip登录(也就是%):

Text Only
1
mysql> UPDATE user SET host = '%' WHERE user = 'root';

得到响应:

Text Only
1
2
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

再次查询:

Text Only
1
mysql> SELECT host,user FROM user;

得到响应:

Text Only
1
2
3
4
5
6
7
8
+-----------+------------------+
| host      | user             |
+-----------+------------------+
| %         | root             |
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
+-----------+------------------+

最后刷新权限:

Text Only
1
mysql> FLUSH PRIVILEGES;

至此,root用户可以远程登录MySQL了。

评论