在Linux上装mysql8后,需要修改默认的密码,按照网上的操作,踩了N多的坑,终于把问题解决了,具体原因是mysql8和mysql5的版本不同造成的。
我尝试的错误命令如下:
update user set password=password("123456") where user="root";
set password for 'root'@'localhost' = password('123456');
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
ALTER USER 'root'@'%' IDENTIFIED BY 'Asd1234@';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'Asd1234@';
update user set authentication_string='' where user = 'root';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'Asd1234@';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Asd1234@';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'Asd1234@';
SET PASSWORD FOR 'root'@'%' = PASSWORD('123456');
正确的步骤如下:
第一步:输入以下指令 vim /etc/my.cnf
vim /etc/my.cnf
第二步:在[mysqld] 下加命令行kip-grant-tables,然后保存退出,退出方法为先按Esc,然后再输入:wq
kip-grant-tables
详细图片如下:
第三步:重启mysql,重启命令为service mysqld restart;
service mysqld restart;
第四步:输入mysql -u root -p ,在输入密码的地方直接按Enter键
mysql -u root -p;
第五步:输入flush privileges命令
flush privileges;
第六步:输入use mysql以及select user,host from user;命令用来确认root的host是哪个
use mysql;
select user, host from user;
-----------------+-----------+
| user | host |
+------------------+-----------+
| root | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
+------------------+-----------+
第七步:输入修改密码的指令alter user'root'@'%' IDENTIFIED BY 'MyNewPass@666';
alter user'root'@'%' IDENTIFIED BY 'MyNewPass@666'; //MyNewPass@666 就是你需要改成的密码