MySQL 5.7 初始密碼和密碼複雜度問題

神諭丶發表於2016-04-08
〇 環境:
CentOS 6.5 64bit  (文末有Debian/Ubuntu deb包的密碼問題)

mysql-server版本:5.7.11



〇 
背景:

mysql 5.7以後,安全性大幅度上升,用習慣了老版本
的使用者剛使用會有一些不適。
如,預設安裝後初始密碼不為空;
或找不到初始密碼在哪;

再比如,設定新密碼會報錯ERROR 1819 (HY000): Your password does not satisfy the current policy requirements。
其主要實現方式主要是通過【validate_password】外掛實現。

mysql安裝之後,通常需要初始化datadir等。

如果根據安裝方式的不同,通過service mysqld start來第一次啟動資料庫的話,會初始化資料庫。
並預設安裝驗證密碼外掛(預設安裝為
新特性,5.6中安裝方法見下
  1. Initializing MySQL database: [ OK ]
  2. Installing validate password plugin: [ OK ]
  3. Starting mysqld: [ OK ]
實際上該步驟初始化方式是通過mysqld --initialize來實現。



〇 初始密碼:
在5.7.6以後的版本,如5.7.11,預設初始的密碼是在errlog中。
檢視配置檔案或者通過ps -ef|grep error=可以知道errlog預設位置。
  1. # ps -ef | grep error=
  2. mysql 2387 2189 0 23:10 pts/0 00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
  3. root 2464 1131 0 23:28 pts/0 00:00:00 grep error=

通過查errlog中的資訊,可以得知初始密碼如下:
  1. # cat /var/log/mysqld.log | grep "temporary password"
  2. 2016-04-08T15:10:00.920194Z 1 [Note] A temporary password is generated for root@localhost: yP(DO;EM&2CY

故通過

  1. mysql -uroot -p'yP(DO;EM&2CY'
即可登入

並且第一次使用隨機生成的密碼登入後,必須第一步做密碼修改操作,否則會丟擲:
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.




〇 密碼複雜度:
修改當前登入mysql使用者密碼和老版本中一樣:
  1. mysql> SET PASSWORD=PASSWORD('Root1234@');
  2. Query OK, 0 rows affected, 1 warning (0.00 sec)
  3. mysql> FLUSH PRIVILEGES;
  4. Query OK, 0 rows affected (0.00 sec)
若使用老版本的SET PASSWORD=PASSWORD('')方式來修改密碼的話,也會丟擲警告:
大致意思是這種方式將會在未來版本中移除,

  1. Warning 1287 'SET PASSWORD = PASSWORD('')' is deprecated and will be removed in a future release. Please use SET PASSWORD = '' instead

新的修改方式是:(不需要flush privileges;)
  1. mysql> SET password='Root1234@';

當然,官方還建議使用如下方式來修改密碼

  1. ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

同時,密碼“過於簡單”時也會報錯:

  1. ERROR 1819 (HY000): Your password does not satisfy the current policy requirements。

這個validate_password密碼強度審計外掛決定了你設定的密碼是否“過於簡單”。
  1. mysql> SHOW VARIABLES LIKE 'vali%';
  2. +--------------------------------------+--------+
  3. | Variable_name                        | Value  |
  4. +--------------------------------------+--------+
  5. | validate_password_dictionary_file    |        |
  6. | validate_password_length             | 8      |
  7. | validate_password_mixed_case_count   | 1      |
  8. | validate_password_number_count       | 1      |
  9. | validate_password_policy             | MEDIUM |
  10. | validate_password_special_char_count | 1      |
  11. +--------------------------------------+--------+
  12. 6 rows in set (0.00 sec)

5.7初始化後,預設會安裝這個外掛,若沒有安裝,則SHOW VARIABLES LIKE 'vali%'則會返回空。
對應引數的value值也為預設值,以下是這些值的解釋
  1.  validate_password_length 8 # 密碼的最小長度,此處為8。
  2.  validate_password_mixed_case_count 1 # 至少要包含小寫或大寫字母的個數,此處為1。
  3.  validate_password_number_count 1 # 至少要包含的數字的個數,此處為1。
  4.  validate_password_policy MEDIUM # 強度等級,其中其值可設定為0、1、2。分別對應:
  5.                            【0/LOW】:只檢查長度。
  6.                            【1/MEDIUM】:在0等級的基礎上多檢查數字、大小寫、特殊字元。
  7.                            【2/STRONG】:在1等級的基礎上多檢查特殊字元字典檔案,此處為1。
  8.  validate_password_special_char_count 1 # 至少要包含的個數字符的個數,此處為1。

想要關閉這個外掛,則在配置檔案中加入以下並重啟mysqld即可:
[mysqld]
validate_password=off

或者
UNINSTALL PLUGIN validate_password;
即可

重啟mysqld後通過SHOW PLUGINS;可以查到:
  1. +-------------------+----------+-------------------+----------------------+-----+
  2. | validate_password | DISABLED | VALIDATE PASSWORD | validate_password.so | GPL |
  3. +-------------------+----------+-------------------+----------------------+-----+



〇 5.7.6及之前的版本:

而在5.7.6之前的版本,初始化方式也不一樣,故預設初始化密碼存放的位置也不一樣:
  1. bin/mysql_install_db --basedir=/usr --datadir=/var/lib/mysql --user=root
  2. [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
其中,在5.7.6後續版本中,mysql_install_db已經被廢棄,而後續版本將通過mysqld --initialize 來初始化。

在5.6.8以後的版本,如果是通過mysql_install_db指令碼來初始化,配合--random-passwords選項,則密碼會在/root/.mysql_secret中,如

  1. cat /root/.mysql_secret
  2. # Password set for user 'root@localhost' at 2016-04-08 16:13:51
  3. yP(DO;EM&2CY





〇 5.6版本中的validate_password外掛

當然,mysql 5.6也可以安裝這個外掛,安裝起來十分簡單:

修改配置檔案:
[mysqld]
plugin-load=validate_password.so
然後在mysql中安裝:
mysql> INSTALL PLUGIN validate_password SONAME 'validate_password.so';



〇 5.7版本初始化時不設定初始密碼,並不安裝validate_password外掛
當然在一些實驗機環境中,可能不需要這樣做,mysql5.7也提供了不安裝該外掛並不生成隨機密碼的操作。
只需要在初始化時指定--initialize-insecure即可,比如:
  1. mysqld --initialize-insecure --datadir=/var/lib/mysql --basedir=/usr --user=mysql
此時,SHOW VARIABLES LIKE 'vali%';也會為空,因為該外掛沒有被安裝。



〇 關於5.7中 
mysqld 其他選項引數
可以用如下命令去檢視

  1. mysqld --verbose --help | grep password

〇 Debian/Ubuntu 包安裝的密碼問題
在通過deb包安裝的時候,會被詢問指定root密碼,如果設定為空,會輸出:
 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.



〇 參考文件:

MySQL 5.6 Reference Manual / Security / Security Plugins / The Password Validation Plugin  
MySQL 5.7 Reference Manual / Initializing the Data Directory Manually Using mysqld




作者微信公眾號(持續更新)

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29773961/viewspace-2077579/,如需轉載,請註明出處,否則將追究法律責任。

相關文章