我的操作系統(tǒng)是ubuntu18.04,以下是我的mysql版本:
安裝完成后,登錄mysql的時候就出現(xiàn)了如下錯誤:
因?yàn)榘惭b的過程中沒讓設(shè)置密碼,可能密碼為空,但無論如何都進(jìn)不去mysql。
下面是我的處理過程:
Step1:修改mysqld.cnf配置文件
在ubuntu的terminal(也即終端)上輸入sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf,進(jìn)入到這個配置文件,然后在這個配置文件中的[mysqld]這一塊中加入skip-grant-tables這句話。
1 [mysqld] 2 # 3 # * Basic Settings 4 # 5 user = mysql 6 pid-file = /var/run/mysqld/mysqld.pid 7 socket = /var/run/mysqld/mysqld.sock 8 port = 3306 9 basedir = /usr10 datadir = /var/lib/mysql11 tmpdir = /tmp12 lc-messages-dir = /usr/share/mysql13 skip-external-locking14 character-set-server=utf815 collation-server=utf8_general_ci16 skip-grant-tables <-- add here
作用:就是讓你可以不用密碼登錄進(jìn)去mysql。
保存:wq,退出。輸入:service mysql restart,重新啟動mysql。
step2:設(shè)置root密碼
在終端上輸入mysql -u root -p,遇見輸入密碼的提示直接回車即可,進(jìn)入mysql后,分別執(zhí)行下面三句話:
1 use mysql; 然后敲回車2 update user set authentication_string=password("你的密碼") where user="root"; 然后敲回車3 flush privileges; 然后敲回車
結(jié)果如下圖:
然后輸入quit,退出mysql。
step3:注釋掉skip-grant-tables
重新進(jìn)入到mysqld.cnf文件中去把剛開始加的skip-grant-tables這條語句給注釋掉。
1 [mysqld] 2 # 3 # * Basic Settings 4 # 5 user = mysql 6 pid-file = /var/run/mysqld/mysqld.pid 7 socket = /var/run/mysqld/mysqld.sock 8 port = 3306 9 basedir = /usr10 datadir = /var/lib/mysql11 tmpdir = /tmp12 lc-messages-dir = /usr/share/mysql13 skip-external-locking14 character-set-server=utf815 collation-server=utf8_general_ci16 # skip-grant-tables <-- add # here
再返回終端輸入mysql -u root -p,應(yīng)該就可以進(jìn)入數(shù)據(jù)庫了。
step4:問題解決
如果此時還是報出錯誤,那么就需要返回step3中,把注釋掉的那條語句重新生效(就是刪除#符號),重新進(jìn)入mysql中,先任意選擇一個數(shù)據(jù)庫,比如use mysql;
然后輸入select user, plugin from user; 看下圖:
從圖中可以看到在執(zhí)行了select user, plugin from user; 后,錯誤原因是因?yàn)?strong>plugin root的字段是auth_socket,那我們改掉它,替換為mysql_native_password就行了。輸入:
1 update user set authentication_string=password("ln122920"),plugin='mysql_native_password' where user='root';
然后回車執(zhí)行以下,再輸入select user,plugin from user;回車,我們能看到root用戶的字段改成功了。
最后quit退出。返回執(zhí)行step3。
那么這個問題就完全解決了。
參考鏈接:http://www.cnblogs.com/py1612919884/p/9327015.html
============= 更新 =============
在mysql8版本中,上面更新代碼的語句似乎有所變化,那個句法會被告知是錯誤的,這里我貼一下沒有語法錯誤的:
ALTER user 'root'@'localhost' IDENTIFIED BY 'newpassward'; //newpassward 新密碼
將這句話對應(yīng)到上面Step2即可。
如果執(zhí)行本語句出現(xiàn)The MySQL server is running with the --skip-grant-tables option so it cannot execute this statemen這個錯誤,
則解決如下:
先flush privileges,然后再執(zhí)行上面修改密碼的語句---Step2。
============= 更新【MacOS】=============
在MacOS下,由于沒有mysql的配置文件,我們修改密碼的時候,需要這么做,以我的為例,我是通過brew安裝的mysql,進(jìn)入mysql安裝目錄下:cd /usr/local/Cellar/mysql@5.7/5.7.29/bin。
Step1:./mysqld_safe --skip-grant-tables 來禁止mysql的驗(yàn)證功能。
Step2: mysql -u root 這會不需要密碼進(jìn)入mysql的交互窗口。
Step3: FLUSH PRIVILEGES 清除權(quán)限
Step4: SET PASSWORD FOR 'root'@'localhost' = PASSWORD('你的新密碼'); 設(shè)置新密碼。
這樣就修改/添加密碼成功了。