歡迎您光臨本站 註冊首頁

Linux MySQL忘記root密碼解決方案

←手機掃碼閱讀     limiyoyo @ 2020-06-08 , reply:0

在使用MySQL數據庫時,由於某些原因長時間沒有登陸MySQL,或者由於工作交接完成度不高,導致數據庫root登陸密碼忘記,如何解決?

一、更改my.cnf配置文件

1、用命令編輯/etc/my.cnf配置文件,即:vim /etc/my.cnf 或者 vi /etc/my.cnf 或者 nano /etc/my.cnf

2.在[mysqld]下添加skip-grant-tables,然後保存並退出

3.重啟mysql服務:service mysqld restart

二、更改root用戶名

1、重啟以後,執行mysql命令進入mysql命令行

2、修改root用戶密碼

MySQL> UPDATE mysql.user SET Password=PASSWORD('新密碼') where USER='root';
 MySQL> flush privileges;
 MySQL> exit

注意:以上是5.7之前的版本使用,5.7之後的版本沒有Password字段,password字段改成了authentication_string

mysql> update mysql.user set authentication_string=password(‘root123456') 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

n>mysql -u ******* -p #以該用戶登錄成功.
 Enter password: ********
 …………………………
 mysql>

注意:5.7之後的版本,密碼不能過於簡單,如123456,否則會報以下錯誤:ERROR 1819 (HY000): Your password does not satisfy the current policy requirements。

此時,要麼把密碼設置複雜點,要麼修改配置:

  這個其實與validate_password_policy的值有關。    validate_password_policy有以下取值:    默認是1,即MEDIUM,所以剛開始設置的密碼必須符合長度,且必須含有數字,小寫或大寫字母,特殊字符。  有時候,只是為了自己測試,不想密碼設置得那麼複雜,譬如說,我只想設置root的密碼為123456。  必須修改兩個全局參數:    首先,修改validate_password_policy參數的值    mysql> set global validate_password_policy=0;  Query OK, 0 rows affected (0.00 sec)  1  2  validate_password_length(密碼長度)參數默認為8,我們修改為1    mysql> set global validate_password_length=1;  Query OK, 0 rows affected (0.00 sec)  1  2  4,完成之後再次執行修改密碼語句即可成功    mysql> alter user 'root'@'localhost' identified by '123456';  Query OK, 0 rows affected (0.00 sec)

 

3、最後把/etc/my.cnf中的skip-grant-tables註釋掉,然後重啟mysql,即:service mysqld restart

OK,下面我們就可以使用root新的密碼登錄MySQL了。

                                                       

   


[limiyoyo ] Linux MySQL忘記root密碼解決方案已經有237次圍觀

http://coctec.com/docs/linux/show-post-237430.html