我這裏是經過mysql官方的yum源來安裝的mysql-community-server ,當前版本是MySQL 5.7.12 。html
wget rpm -ivh mysql57-community-release-el6-8.noarch.rpm yum install mysql-community-server service mysqld start
第一次啓動後會有個初始化的過程,會產生root帳戶的隨機密碼。mysql
爲了增強安全性,MySQL5.7爲root用戶隨機生成了一個密碼,在error_log中,關於error_log的位置,若是安裝的是RPM包,則默認是 /var/log/mysqld.log 。sql
找到生成的隨機密碼數據庫
mysql -u root -p'zXMgg%#L3=;1' mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.12 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql> show databases; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
登錄上事後,進行正常操做會受限,提示你必須修改密碼後才能進行操做。
安全
好吧,根據提示修改密碼:bash
mysql> SET PASSWORD = PASSWORD('123456'); ERROR 1819 (HY000): Your password does not satisfy the current policy requirements mysql> SET PASSWORD = PASSWORD("root"); ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
可是提示根據當前密碼策略,設置的密碼不容許。socket
查閱官方文檔後發現有如下三種密碼策略:ide
Policy | Tests Performed |
0 or LOW |
Length |
1 or MEDIUM |
Length; numeric, lowercase/uppercase, and special characters |
2 or STRONG |
Length; numeric, lowercase/uppercase, and special characters; dictionary file |
當前密碼策略默認爲1 也就是 MEDIUMui
mysql root@localhost:(none)> show VARIABLES like "%password%" +---------------------------------------+---------+ | Variable_name | Value | |---------------------------------------+---------| | default_password_lifetime | 0 | | disconnect_on_expired_password | ON | | log_builtin_as_identified_by_password | OFF | | mysql_native_password_proxy_users | OFF | | old_passwords | 0 | | report_password | | | sha256_password_proxy_users | OFF | | 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 | +---------------------------------------+---------+ 13 rows in set Time: 0.030s
因此你更改密碼的策略是 數字 小寫字母 大寫字母 特殊字符 長度至少8位 。this
更改完密碼就能夠進行數據庫的操做了。
mysql root@localhost:(none)> show DATABASES; +--------------------+ | Database | |--------------------| | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set Time: 0.009s
接下來修改默認密碼策略(固然實際環境是不推薦修改成更低安全策略的)
mysql root@localhost:(none)> set global validate_password_policy = 0; Query OK, 0 rows affected Time: 0.003s
如今設置完默認密碼策略後,就只有 密碼長度限制 了。默認爲字符長度至少8位。
其中:
validate_password_number_count指定了密碼中數據的長度,
validate_password_special_char_count指定了密碼中特殊字符的長度,
validate_password_mixed_case_count指定了密碼中大小字母的長度。
這些參數,默認值均爲1,因此validate_password_length最小值爲4,若是你顯性指定validate_password_length的值小於4,儘管不會報錯,但validate_password_length的值將設爲4。
mysql root@localhost:(none)> set global validate_password_length = 3; Query OK, 0 rows affected Time: 0.004s mysql root@localhost:(none)> show VARIABLES like "validate_password_length" +--------------------------+---------+ | Variable_name | Value | |--------------------------+---------| | validate_password_length | 4 | +--------------------------+---------+ 1 row in set Time: 0.010s
若是修改了validate_password_number_count,validate_password_special_char_count,validate_password_mixed_case_count中任何一個值,則validate_password_length將進行動態修改。
MySQL 5.7 默認安裝了 validate_password 插件。 因此多了以上步驟。
----------------------------------------------------------------------------
經過my.cnf 配置文件設置密碼策略的級別
"/etc/my.cnf" 28L, 987C 22,1 All # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock validate_password_policy=2
最後一行 validate_password_policy 設置mysql啓動的時候密碼策略級別。 若是設置爲3 ,那麼須要指定字典文件。
固然你也能夠經過 my.cnf 配置文件關閉 validate_password 插件。
只須要添加一行
validate_password = off
編輯完配置文件後,重啓mysqld服務便可生效。
mysql root@localhost:(none)> show VARIABLES like "validate_password%" +-----------------+---------+ | Variable_name | Value | |-----------------+---------| +-----------------+---------+ 0 rows in set Time: 0.008s
關閉validate_password插件後,就沒有了validate_password的一些參數變量。
MySQL官方對於 validate_password 插件的使用介紹:
http://dev.mysql.com/doc/refman/5.6/en/validate-password-plugin.html#option_mysqld_validate-password
--------------------------------------------------------------------------------
MySQL 新版本默認監聽在IPv6的地址族上。
更改成監聽IPv4地址族,修改 my.cnf 添加一行配置:
bind-address = 0.0.0.0
重啓mysqld 便可。