參考網站:
https://www.linuxidc.com/Linux/2018-05/152586.htm
https://www.cnblogs.com/wangbaobao/p/7087032.html
博主使用mysql版本:5.7.22
1、修改my.cnf配置文件
vim /etc/my.cnf
在[mysqld]下添加skip-grant-tables,然后保存並退出;
2、重啟mysql
service mysqld restart
3、更改root用戶密碼
執行mysql命令,進入mysql命令行
mysql
mysql> use mysql; #使用mysql
mysql> select User from user; #此處為查詢用戶命令
mysql> update user set password=password("123456") where user="root";
#修改密碼報錯,5.5.*版本的mysql可以使用此方法修改密碼,5.7版本下的mysql則會報以下錯誤提示,
#原因是 5.7版本下的mysql數據庫下已經沒有password這個字段了,password字段改成了authentication_string
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
mysql> update mysql.user set authentication_string=password('123456') where user='root'; #修改密碼成功
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges; #立即生效
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
#先將/etc/my.cnf配置文件中的skip-grant-tables注釋掉,保存退出
#檢驗修改密碼是否
mysql -u root -p #以該用戶登錄成功.
Enter password: 123456
…………………………
mysql>