centos7下mysql5.7修改密碼和外部能訪問的步驟、講解和所遇到的那些坑(最全)
登入mysql報錯
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
1、登入安裝資料庫的機器
輸入vi /etc/my.cnf,在該配置檔案增加一行 skip-grant-tables,隨便什麼密碼都能登陸
2、重啟資料庫
service mysqld restart
3、連線資料庫
mysql -u root -p隨便輸入個密碼
4、進入資料庫mysql
use mysql
5、先檢視密碼是否過期(user表用於放使用者資訊)
#password_lifetime=0表示永久有效,password_expired='N'密碼未過期
select password_expired,password_last_changed,password_lifetime,authentication_string from user u where u.user='root';
6、修改root的密碼、有效時間和是否過期,並提交
update user set authentication_string =password('kang') ,password_lifetime=0,password_expired='N' where user='root';
flush privileges;
7、再檢視(密碼已改),再輸入exit退出
select password_expired,password_last_changed,password_lifetime,authentication_string from user u where u.user='root';
8、 輸入vi /etc/my.cnf,在該配置檔案刪掉 skip-grant-tables,使需要正確密碼才能登陸
9、重啟資料庫
service mysqld restart
此時又出現個問題,只有本地才能訪問,其他地方訪問還是報錯
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
這時候就需要設定其他機器也能訪問
1、檢視root哪些能訪問
2、更新其他機器也能訪問root, %表示都能訪問,localhost表示只有本機能訪問
update user set host='%' where user='root';