某些特殊情形下,我们需要使用root用户来远程登录,管理mysql数据库;
准备工作:关闭防火墙
systemctl status firewalld //查看防火墙状态
systemctl stop firewalld //关闭防火墙
systemctl disable firewalld //开机防火墙防火墙不启用
iptables -F //关闭iptables防火墙
必须用防火墙的话放通3306端口,用的云服务器的话请放通安全组规则。
默认情况下,root用户是不具备远程登录的权限的,这也是基于安全考虑;
[root@test sysconfig]# mysql -uroot -p
。 。 。 。
mysql> use mysql;
mysql> select host,user from user where user="root"; //host没有%(代表所有主机),所以不能远程登录
+-----------+------+
| host | user |
+-----------+------+
| 127.0.0.1 | root |
| ::1 | root |
| localhost | root |
| test | root |
+-----------+------+
4 rows in set (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456(密码)' WITH GRANT OPTION;
//授权root用户远程登录
mysql> flush privileges; //刷新配置文件,立即生效
到这一步可以测下了;