mysql -u root -p
mysql> Enter password: (输入刚才查询到的随机密码)
mysql>SET PASSWORD FOR 'root'@'localhost'="Root-123";
mysql> exit
1
2
3
4
用root新密码登录:
mysql -u root -pRoot-123
1
如果上面的方式不能修改可以使用下面安全模式修改root:
关闭服务
systemctl stop mysqld.service
vi /etc/my.cnf
mysqld下面添加skip-grant-tables 保存退出启动服务
systemctl start mysqld.service
mysql -u root 不用密码直接回车
use mysql
updateuserset authentication_string=password('Root-123') whereUser='root'and Host='localhost';
flush privileges;
exit;
vi /etc/my.cnf 把 skip-grant-tables 一句删除保存退出重启mysql服务
systemctl restart mysqld.service
再次登录即可
mysql -u root -pRoot-123
如果进行操作出现下面的提示:
You must reset your password usingALTERUSER statement before executing this statement.
就再设置一遍密码
set password = password('Root-123');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
6. 开放3306端口
允许使用用户名root密码Root-123456从任何主机连接到mysql服务器
mysql>GRANT ALL PRIVILEGES ON *.* TO'root'@'%' IDENTIFIED BY'Root-123456'WITH GRANT OPTION;
mysql>FLUSH PRIVILEGES;
mysql>exit;