(1)修改配置文件/etc/my.cnf,在配置文件[mysqld]下添加skip-grant-tables,重启MySQL服务即可免密码登录
# you have installed the server correctly (see above) so it reads this# file.
[mysqld]
skip-grant-tables
(2)用空密码的 root 用户连接到 MySQL,并且更改 root 口令:
[root@localhost mysql]# mysql -uroot
-->1.进入mysql数据库:
mysql> use mysql;
返回:Database changed
2.mysql8.0 以下给root用户设置新密码:
mysql> update user set password=password(“新密码”) where user=“root”;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123';
密码为空:update user set authentication_string = NULL where user = 'root';
删除my.conf的skip-grant-tables,重启mysql--》ALTER USER'root'@'localhost'IDENTIFIED WITH mysql_native_password BY'123';
}
3.刷新数据库
mysql> flush privileges;
返回:Query OK, 0 rows affected (0.00 sec)
4.root 用户只允许在服务器的远程登录
update user set host='%' where user='root';
5.退出mysql:mysql> quit
6.重启mysql:systemctl restart mysql.service
重新用 root 登录时,必须输入新口令。mysql -uroot -p