先设置跳过密码登录
vi /etc/my.cnf
进入之后在 【mysqld】间加入
skip-grant-tables
之后ESC退出。:wq保存退出。
–重启服务
service mysqld restart
–切换到mysql用户
mysql -u root
use mysql
–update mysql.user set authentication_string=password(‘root_password’) where user=‘root’;
在新的版本中已经没有password字段了,改为了authentication_string,password函数也不支持了。
需要用alter table 方式修改密码。
–grep ‘temporary password’ /var/log/mysqld.log
可以看到临时密码 为Uboe8SfiIr-C
使用临时密码登录root
mysql -u root -p
–此时提示密码不安全,mysql初始要求密码为8位,包含大小字母及数字,特殊字符。这里暂时使用’Aa12345!’;
修改密码策略的文章很多,在这里不赘述了。
这里又提示账户有问题,可以通过mysql.user的host看到,root的host为%,
所以应该改为
这里就成功了,然后刷新一下
flush privileges;
exit;
vi /etc/my.cnf
删掉刚才的跳过密码登录的代码段。
退出之后重启一下服务。
service mysqld restart;
其他问题
ERROR 1396 (HY000): Operation ALTER USER failed for ‘root’@’%’
在改为%之后,还可能出现该报错。
update user set authentication_string=’ ’ where user=‘root’;
flush privileges;
ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘123456’;
将authentication_string 置空之后,再进行修改密码。
- 原因还不清楚,水平有限。如有任何错误或问题,欢迎留言向您学习讨论。