出現這種情況可能是密碼錯誤或者未授權
步驟概括
1、找到mysql配置文件,/etc/my.cnf ,在配置文件加入skip-grant-tables,然後重啓mysql服務即可生效
2、免密登錄mysql,修改密碼,重新重啓mysql
5.7 之前版本:update user set password = password('root') where user='root';
5.7及之後版本:update user set authentication_string = password('root') where user='root';
3、授權
grant all privileges on *.* to 'root'@'%' identified by 'root';
flush privileges;
詳細步驟
1、找到mysql配置文件,/etc/my.cnf ,在配置文件加入skip-grant-tables
2、重啓mysql服務即可生效
systemctl restart mysqld.service //重啓mysql服務
systemctl status mysqld.service //查看mysql狀態
systemctl stop mysqld.service //停止mysql服務
3、進入mysql,修改mysql密碼
mysql -uroot -p
不用輸入密碼,直接回車,即可進入mysql命令界面
首先進入表mysql,如圖
修改密碼:這裏要根據版本來執行不同的SQL語句了,因爲版本不同,存儲密碼的字段可能不相同。5.7以前的版本可以用以下語句更新root密碼:
update user set password = password('root') where user='root';
如果是高版本,則會提示錯誤信息
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
而我本地mysql版本是5.7.21,裏邊沒有password,是因爲已經改成authentication_string字段了,於是重新執行修改字段後的更新語句
update user set authentication_string = password('root') where user='root';
修改成功!重啓mysql,記得將skip-grant-tables註釋。
4、授權遠程連接
指定某個ip可以登錄,那麼我就可以將授權的語句改成:
grant all privileges on *.* to 'root'@'192.168.xx.xx' identified by 'root';
如果你是本地登錄的,那麼:
grant all privileges on *.* to 'root'@'localhost' identified by 'root';
當然也可以設置所有ip都可登錄
grant all privileges on *.* to 'root'@'%' identified by 'root';
如果授權成功,會有Query OK的提示。
然後執行fluse privileges ,使授權立即生效
flush privileges;
也許會提示錯誤:ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
這樣只需執行如下命令即可:
mysql -u root -p #登錄
mysql>set global validate_password_policy=0; #密碼強度設爲最低等級
mysql>set global validate_password_length=4; #密碼允許最小長度爲4
mysql>flush privileges; #更新授權表,生效
===============關於 mysql 密碼策略相關參數========================
1、validate_password_length 固定密碼的總長度;
2、validate_password_dictionary_file 指定密碼驗證的文件路徑;
3、validate_password_mixed_case_count 整個密碼中至少要包含大/小寫字母的總個數;
4、validate_password_number_count 整個密碼中至少要包含阿拉伯數字的個數;
5、validate_password_policy 指定密碼的強度驗證等級,默認爲 MEDIUM;
validate_password_policy 的取值:
0/LOW:只驗證長度;
1/MEDIUM:驗證長度、數字、大小寫、特殊字符;
2/STRONG:驗證長度、數字、大小寫、特殊字符、字典文件;
6、validate_password_special_char_count 整個密碼中至少要包含特殊字符的個數
可通過sql語句查看密碼策略,查相關資料可知,validate_password_length最小值爲4,即使你設置爲小於4的值,mysql也會默認設置爲4。
show variables like 'validate_password%';
歡迎加羣交流:700637673