今天安裝了win7 64bit的MySQL 社區版,版本號爲 8.0.12,安裝步驟:(參考https://www.cnblogs.com/reyinever/p/8551977.html)
- 下載、解壓介質
- 設置MySQL_HOME環境變量,並將";%MYSQL_HOME%\bin"添加到PATH最後(不要忽略首位分號)
- 生成data文件(管理員運行CMD,cd到bin/,“mysqld --initialize-insecure --user=mysql”)
- “mysql -install”(避免啓動mysql報錯“服務名無效”)
- 啓動mysql,net start mysql
啓動後首次使用root連接無需密碼,登錄後嘗試更改root密碼:“update mysql.user set authentication_string=password(‘123456’) where user = ‘root’;”
結果報錯,報錯信息爲“”
刪掉password字樣,update執行成功,flush後重連,結果使用新密碼死活無法登錄root
只好設置跳過密碼驗證:
- 停止mysql服務:“net stop mysql”
- 設置跳過驗證:“mysqld --shared-memory --skip-grant-tables”,(注意:一定要有–shared-memory,否則無法正常設置–skip-grant-tables並啓動mysql服務)
- 無密碼登錄:在mysql/bin/目錄下新開一個CMD窗口,無需重複啓動mysql,直接輸入"mysql",此時應該可以連接成功
重設root密碼時又遇到問題
由於此時處於"–skip-grant-tables",故使用"ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘123456’" 無法修改root密碼,會報錯;
嘗試使用“update mysql.user set _string=password(‘123456’) where user = ‘root’”;再次報錯,提示(‘123456’)附近有語法錯誤,刪去‘password()’字樣可執行,但此時依舊無法使用新密碼登錄.
查閱資料無果,猜測是不使用password()設密碼時,密碼原樣存入user表,但登錄是會對輸入密碼進行編碼(MySQL的密碼認證插件是“mysql_native_password”,而現在使用的是“caching_sha2_password”),且尚不知道如何在設置密碼時使用相同編碼策略。
最後使用update語句,將root密碼設置爲空(update mysql.user set authertication_string=’’ where user = ‘root’;),竟然可以"mysql -uroot -p"顯示"Enter password:"時直接回車,無密碼正常登錄。
原因:
- MySQL 在8.0.11中移除了password();(The PASSWORD() function. Additionally, PASSWORD() removal means that SET PASSWORD … = PASSWORD(‘auth_string’) syntax is no longer available. )
- MySQL 8.0.4中改了默認加密方式爲“caching_sha2_password”(Starting with MySQL 8.0.4, we are changing the default authentication plugin for MySQL server from mysql_native_password to caching_sha2_password. )