Centos 下mysql用戶丟失,root賬號在本地登錄

在本地打開mysql -u root -p
無論輸入什麼密碼都錯誤,又改了密碼結果還是不行

後來搜了百度都沒有什麼好的解決方法。
發現mysql庫中的user表缺少一個root指向host

怎麼確定你也是這個問題呢?這樣做:

  1. vim /etc/my.cnf
    在[mysqld]的段中加上一句:skip-grant-tables
    例如:
    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    skip-grant-tables
    保存並且退出vi。

  2. mysql
    進入mysql命令
    use mysql;
    select user,host,password from user where user=’root’;

| root | % | **** |
| root | ut12313 | *** |
| root | 127.0.0.1 | ****|
可見沒有host是localhost的
update user set host=’localhost’ where user=’root’ and host=’ut12313’;

然後把先前第一部的還原,然後設置密碼,或者原密碼登陸即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章