MySQL5.7.12新密碼登陸方式及密碼策略

在Centos6.6上安裝MySQL5.7.12時,遇到了一個問題

安裝後在/root目錄下沒有發現有.mysql_secret這個文件,因此沒有無法按照官方文檔上說的那樣使用,這裏記錄下,mysql

解決方式:sql

首先修改MySQL受權登陸方式---(跳過受權驗證方式啓動MySQL):
[root@test ~]# mysqld_safe --skip-grant-tables & [1] 3401 [root@test ~]# 2016-05-19T12:47:56.564385Z mysqld_safe Logging to '/var/log/mysqld.log'. 2016-05-19T12:47:56.589376Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
檢查MySQL啓動狀況 [root@test
~]# ps -ef | grep mysql root 3401 2880 0 20:47 pts/1 00:00:00 /bin/sh /usr/bin/mysqld_safe --skip-grant-tables mysql 3548 3401 0 20:47 pts/1 00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --skip-grant-tables --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
這時登陸MySQL再也不須要驗證
[root@test ~]# mysql

成功登陸MySQL後:

切換到mysql系統庫:
mysql> use mysql;

修改root帳戶登陸密碼:
mysql> update user set password=password('') where user='root';
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
---報錯沒有password這個數據字段列
 描述user表
mysql> desc user;
...
| authentication_string  | text                              | YES  |     | NULL                  |       |
| password_expired       | enum('N','Y')                     | NO   |     | N                     |       |
| password_last_changed  | timestamp                         | YES  |     | NULL                  |       |
| password_lifetime      | smallint(5) unsigned              | YES  |     | NULL                  |       |
| account_locked         | enum('N','Y')                     | NO   |     | N                     |       |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
---沒發現password列,可是找到這5個跟密碼相關的數據字段
 查詢一下相關的密碼信息:
mysql> select user,host,authentication_string,password_expired from user;
+-----------+-----------+-------------------------------------------+------------------+
| user      | host      | authentication_string                     | password_expired |
+-----------+-----------+-------------------------------------------+------------------+
| root      | localhost | *9AA01F6E2A80A823ACB72CC07337E2911404B5B8 | Y                |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | N                |
+-----------+-----------+-------------------------------------------+------------------+
---到這裏不難發現root帳戶的密碼已過時,還比5.6多出了一個mysql.sys用戶
 修改密碼
mysql> update user set authentication_string=password('123abc') where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit

密碼修改爲功,測試:

重啓MySQL:
[root@test ~]# /etc/init.d/mysqld restart

登陸測試:
[root@test ~]# mysql -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.12-enterprise-commercial-advanced
...
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
---報錯,須要使用alter user 修改密碼
mysql
> alter user root@'localhost' identified by 'oracle'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements ---報錯,密碼不知足制定的密碼負責度要求
mysql
> alter user 'root'@'localhost' identified by 'Abc!123D'; Query OK, 0 rows affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)

 關於密碼策略

mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password_dictionary_file    |        |
| validate_password_length             | 8      |
| validate_password_mixed_case_count   | 1      |
| validate_password_number_count       | 1      |
| validate_password_policy             | MEDIUM |
| validate_password_special_char_count | 1      |
+--------------------------------------+--------+
6 rows in set (0.02 sec) 

mysql> show plugins;
+----------------------------+----------+--------------------+----------------------+-------------+
| Name                       | Status   | Type               | Library              | License     |
+----------------------------+----------+--------------------+----------------------+-------------+
| binlog                     | ACTIVE   | STORAGE ENGINE     | NULL                 | PROPRIETARY |

...
| validate_password          | ACTIVE   | VALIDATE PASSWORD  | validate_password.so | PROPRIETARY |
+----------------------------+----------+--------------------+----------------------+-------------+
---能夠經過在配置文件[mysqld]標籤中添加 validate_passwor=off ,來關閉密碼策略
以下:
...
| validate_password          | DISABLED | VALIDATE PASSWORD  | validate_password.so | PROPRIETARY |
+----------------------------+----------+--------------------+----------------------+-------------+

總結

1) 安裝好mysql後,第一次啓動時,root管理密碼會在/root/.mysql_secret中隨機生成shell

2) 至5.7後,MySQL的 mysql.user 表中的密碼字段由以前的 password 改成 authentication_stringoracle

3) 使用--skip-grant-tables 參數啓動,跳過MySQL的受權驗證,--skip-networking參數,跳過遠程登陸socket

4) 修改MySQL密碼方式:ide

法1:update user set authentication_string=password('123abc') where user='root';
工具

法2:set password=password('newpassword');測試

法3:alter user root@'localhost' identified by 'oracle';ui

法4:在shell下使用MySQL工具:mysqladmin -uroot -poldpassword pasword "newpassword"this

5) 關於MySQL密碼策略:

決定是否使用該插件(及強制/永久強制使用)
--validate-password=ON/OFF/FORCE/FORCE_PLUS_PERMANENT
 
validate_password_dictionary_file           > 插件用於驗證密碼強度的字典文件路徑。
validate_password_length                        > 密碼最小長度。
validate_password_mixed_case_count     > 密碼至少要包含的小寫字母個數和大寫字母個數。
validate_password_number_count    > 密碼至少要包含的數字個數。
validate_password_policy                         > 密碼強度檢查等級, 0/LOW、1/MEDIUM、2/STRONG
validate_password_special_char_count    > 密碼至少要包含的特殊字符數。
 
其中關於validate_password_policy-密碼強度檢查等級:
0/LOW    > 只檢查長度
1/MEDIUM      > 檢查長度、數字、大小寫、特殊字符
2/STRONG      > 檢查長度、數字、大小寫、特殊字符字典文件

後記

通過一段時間後,發現mysql初始密碼原來被記錄到了日誌文件中

查找日誌位置
[root@test /var/lib/mysql]# ps -ef | grep mysql root 5604 1 0 22:40 pts/1 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql mysql 5802 5604 5 22:40 pts/1 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 root 5837 2880 0 22:40 pts/1 00:00:00 grep --color mysql
藏在日誌文件中的臨時密碼 [root@test
/var/lib/mysql]# grep "A temporary password" /var/log/mysqld.log 2016-05-17T16:46:53.059632Z 1 [Note] A temporary password is generated for root@localhost: +wGVA#to(4tu
相關文章
相關標籤/搜索