1、安裝完MySQL5.7之後,使用mysql -u root -p登錄MySQL時報錯:
[root@localhost bin]# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
2、百度搜了很多解決方法,都是使用安全模式啓動數據:
service mysqld stop #首先關閉mysql服務
mysqld_safe --user=root --skip-grant-tables --skip-networking & #然後使用mysqld_safe跳過權限方式啓動
mysql #然後直接登錄,跳過密碼驗證
update mysql.user set password=password('123456') where user='root'; #將密碼設置爲123456
3、但是在安裝目錄/usr/bin/下根本找不到mysqld_safe這個文件,所以這個方法無效。
4、找了大半天,終於找到一個介紹,skip-grant-tables這個是配置參數,可以放在mysql配置文件裏面修改,然後把這個配置參數放到/etc/my.cnf文件裏面。
5、添加進去後重啓mysql服務,就可以正常登進去mysql數據庫了;
6、但是設置密碼的時候,輸入update mysql.user set password=password('123456') where user='root';發現password字段不存在,突然間心態大崩。
7、最後執行select * from mysql.user;命令查看找到user表authentication_string這個列的值與password的只相似,於是將這個字段的值備份一下。
8、執行update mysql.user set authentication_string=password('123456') where user='root';
9、然後退出mysql,執行systemctl restart mysqld.service重啓數據庫,再次登錄,終於解決問題!
---------------------
版權聲明:本文爲CSDN博主「ckg8933」的原創文章,遵循CC 4.0 by-sa版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/ckg8933/article/details/81946742