MySQL5.7忘記root密碼處理

tangyunoracle發表於2017-07-11
MySQL忘記root密碼,而且不能使用作業系統認證直接進入資料庫修改密碼時,需要重置root密碼。
1、在配置檔案/etc/my.cnf新增skip-grant-tables一行,跳過密碼驗證。
2、重啟mysql資料庫主程式# /etc/init.d/mysqld restart(也可以直接先停掉MySQL程式後使用skip-grant-tables引數重啟MySQL)
3、登入資料庫修改密碼。
mysql> update user set authentication_string=password('') where user='root' and host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> exit
這裡需要修改的欄位是authentication_string,這點和之前的版本不同。
4、這個時候,如果你設定的密碼太簡單,則在資料庫執行任何命令都會報類似如下錯誤:
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> show database;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database' at line 1
mysql> update user set authentication_string=password('mysql') where user='root' and host='localhost';
ERROR 1046 (3D000): No database selected
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
5、注意:如果只想設定簡單密碼需要修改兩個全域性引數:
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=1;
mysql> set global validate_password_policy=0;
       Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1;
       Query OK, 0 rows affected (0.00 sec)
mysql> set password=password("mysql");
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
-------------------------End--------------------------------------------------------------------------

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24930246/viewspace-2141913/,如需轉載,請註明出處,否則將追究法律責任。

相關文章