伺服器安裝MySQL5.7
1、配置YUM源
在MySQL官網中下載YUM源rpm安裝包:http://dev.mysql.com/downloads/repo/yum/
# 下載mysql源安裝包 shell> wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm # 安裝mysql源 shell> yum localinstall mysql57-community-release-el7-8.noarch.rpm # 下載mysql源安裝包 shell> wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm # 安裝mysql源 shell> yum localinstall mysql57-community-release-el7-8.noarch.rpm
檢查mysql源是否安裝成功
shell> yum repolist enabled | grep "mysql.*-community.*"
看到上圖所示表示安裝成功。
可以修改vim /etc/yum.repos.d/mysql-community.repo源,改變預設安裝的mysql版本。比如要安裝5.6版本,將5.7源的enabled=1改成enabled=0。然後再將5.6源的enabled=0改成enabled=1即可。改完之後的效果如下所示:
2、安裝MySQL
shell> yum install mysql-community-server
3、啟動MySQL服務
shell> systemctl start mysqld
檢視MySQL的啟動狀態
shell> systemctl status mysqld ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled) Active: active (running) since 五 2016-06-24 04:37:37 CST; 35min ago Main PID: 2888 (mysqld) CGroup: /system.slice/mysqld.service └─2888 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid 6月 24 04:37:36 localhost.localdomain systemd[1]: Starting MySQL Server... 6月 24 04:37:37 localhost.localdomain systemd[1]: Started MySQL Server.
4、開機啟動
shell> systemctl enable mysqld
shell> systemctl daemon-reload
5、修改root本地登入密碼
mysql安裝完成之後,在/var/log/mysqld.log檔案中給root生成了一個預設密碼。通過下面的方式找到root預設密碼,然後登入mysql進行修改:
shell> grep 'temporary password' /var/log/mysqld.log
shell> mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
或者
mysql> set password for 'root'@'localhost'=password('MyNewPass4!');
注意:mysql5.7預設安裝了密碼安全檢查外掛(validate_password),預設密碼檢查策略要求密碼必須包含:大小寫字母、數字和特殊符號,並且長度不能少於8位。否則會提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements錯誤,如下圖所示:
通過msyql環境變數可以檢視密碼策略的相關資訊:
mysql> show variables like '%password%';
validate_password_policy:密碼策略,預設為MEDIUM策略
validate_password_dictionary_file:密碼策略檔案,策略為STRONG才需要
validate_password_length:密碼最少長度
validate_password_mixed_case_count:大小寫字元長度,至少1個
validate_password_number_count :數字至少1個
validate_password_special_char_count:特殊字元至少1個
上述引數是預設策略MEDIUM的密碼檢查規則。
共有以下幾種密碼策略:
策略 | 檢查規則 |
---|---|
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 |
MySQL官網密碼策略詳細說明:http://dev.mysql.com/doc/refman/5.7/en/validate-password-options-variables.html#sysvar_validate_password_policy
修改密碼策略
在/etc/my.cnf檔案新增validate_password_policy配置,指定密碼策略
# 選擇0(LOW),1(MEDIUM),2(STRONG)其中一種,選擇2需要提供密碼字典檔案
validate_password_policy=0
如果不需要密碼策略,新增my.cnf檔案中新增如下配置禁用即可:
validate_password = off
重新啟動mysql服務使配置生效:
systemctl restart mysqld
6、新增遠端登入使用者
預設只允許root帳戶在本地登入,如果要在其它機器上連線mysql,必須修改root允許遠端連線,或者新增一個允許遠端連線的帳戶,為了安全起見,我新增一個新的帳戶:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'yangxin'@'%' IDENTIFIED BY 'Yangxin0917!' WITH GRANT OPTION;
7、配置預設編碼為utf8
修改/etc/my.cnf配置檔案,在[mysqld]下新增編碼配置,如下所示:
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
重新啟動mysql服務,檢視資料庫預設編碼如下所示:
預設配置檔案路徑:
配置檔案:/etc/my.cnf
日誌檔案:/var/log//var/log/mysqld.log
服務啟動指令碼:/usr/lib/systemd/system/mysqld.service
socket檔案:/var/run/mysqld/mysqld.pid