MySQL 5.6 root密碼初始化
阿新 • • 發佈:2019-02-15
從MySQL 5.6.6以後,安裝rpm之後,root使用者會被分配一個隨機密碼,同時在mysql.user表中新增了一個password expired欄位,任何客戶端首次連線後必須首先修改密碼才能做下面的操作。這是因為在rpm的spec指令碼中,初始化資料庫的指令碼改為
/usr/bin/mysql_install_db --rpm --user=mysql --random-passwords
新增了--random-passwords引數
ERROR 1862 (HY000): Your password has expired. To log in you must
change it using a client that supports expired passwords.
那麼如果是在指令碼中如何重新設定密碼呢?
首先提取密碼
但是發現執行下面的命令時
Indicate to the server that the client is can handle sandbox mode if the account used to connect has an expired password. This can be useful for noninteractive invocations of mysql because normally the server disconnects noninteractive clients that attempt to connect using an account with an expired password. (See Section 6.3.6, “Password Expiration and Sandbox Mode”.) This option was added in MySQL 5.6.12.
執行
/usr/bin/mysql_install_db --rpm --user=mysql --random-passwords
新增了--random-passwords引數
按照官網說明,隨機密碼儲存在~/.mysql_secret,這個由安裝rpm的使用者決定,使用kickstart安裝時,檔案放在/tmp/目錄下。檔案內容如下:
# The random password set for the root user at Thu Sep 25 03:30:31 2014 (local time): 9udLegnPieCdVC8m
通過客戶端連線mysql伺服器時,也應該使用對應的版本,老的版本可能由於不支援過期密碼而無法連線,出現類似下面的錯誤:ERROR 1862 (HY000): Your password has expired. To log in you must
change it using a client that supports expired passwords.
通過對應的mysql客戶端連線後,執行SET PASSWORD命令可以重新設定root使用者密碼,expired欄位變為N。
首先提取密碼
pass=$(awk '/password/{print $NF}' /tmp/.mysql_secret)
就是取最後一列。但是發現執行下面的命令時
echo "SET PASSWORD = PASSWORD('');" | mysql -u root --password='u0jt49zWghGMOalT'
仍然回顯上面提到的錯誤資訊,查詢man可以發現新版客戶端增加了一個--connect-expired-password引數Indicate to the server that the client is can handle sandbox mode if the account used to connect has an expired password. This can be useful for noninteractive invocations of mysql because normally the server disconnects noninteractive clients that attempt to connect using an account with an expired password. (See Section 6.3.6, “Password Expiration and Sandbox Mode”.) This option was added in MySQL 5.6.12.
執行
echo "SET PASSWORD = PASSWORD('');" | mysql -u root --password='u0jt49zWghGMOalT' -b --connect-expired-password
OK, 密碼重置成功。
網上有人介紹另外一種重置密碼的方法,沒有驗證,貼出來供參考mysqladmin -u root --password='u0jt49zWghGMOalT' password ''