2017-09-26

CentOS 7.X 安裝 MYSQL

前言:mysql官網上yum的位置
http://dev.mysql.com/downloads/repo/yum/

1.檢查centos系統中是否有mysqlyum安裝包
[root@localhost src]# yum list *mysql*

已載入外掛程式:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * extras: centos.ustc.edu.cn
 * updates: centos.ustc.edu.cn

錯誤:沒有匹配的套裝軟體可以列出
[root@localhost src]#

2.下載mysql安裝包
[root@localhost src]# wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

--2017-02-25 07:01:34--  https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
正在解析主機 dev.mysql.com (dev.mysql.com)... 137.254.60.11
正在連接 dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... 已連接。
已發出 HTTP 請求,正在等待回應... 302 Found
位置:https://repo.mysql.com//mysql57-community-release-el7-9.noarch.rpm [跟隨至新的 URL]
--2017-02-25 07:01:36--  https://repo.mysql.com//mysql57-community-release-el7-9.noarch.rpm
正在解析主機 repo.mysql.com (repo.mysql.com)... 104.88.152.154
正在連接 repo.mysql.com (repo.mysql.com)|104.88.152.154|:443... 已連接。
已發出 HTTP 請求,正在等待回應... 200 OK
長度:9224 (9.0K) [application/x-redhat-package-manager]
正在保存至: mysql57-community-release-el7-9.noarch.rpm
100%[======================================>] 9,224       --.-K/s 用時 0s     
2017-02-25 07:01:36 (72.9 MB/s) - 已保存 mysql57-community-release-el7-9.noarch.rpm [9224/9224])

3.yum命令安裝mysql
進入到包含有mysql安裝包的路徑
[root@localhost src]# ls
mysql57-community-release-el7-9.noarch.rpm

[root@localhost src]# yum localinstall mysql57-community-release-el7-9.noarch.rpm

已載入外掛程式:fastestmirror, langpacks
正在檢查 mysql57-community-release-el7-9.noarch.rpm: mysql57-community-release-el7-9.noarch
mysql57-community-release-el7-9.noarch.rpm 將被安裝
正在解決依賴關係
--> 正在檢查事務
---> 套裝軟體 mysql57-community-release.noarch.0.el7-9 將被 安裝
--> 解決依賴關係完成
base/7/x86_64                                            | 3.6 kB     00:00    
extras/7/x86_64                                          | 3.4 kB     00:00    
extras/7/x86_64/primary_db                               | 122 kB     00:00    
updates/7/x86_64                                         | 3.4 kB     00:00    
updates/7/x86_64/primary_db                              | 2.9 MB     00:02    

依賴關係解決

================================================================================
 Package             架構   版本                                         大小
================================================================================
正在安裝:
 mysql57-community-release
                     noarch el7-9 /mysql57-community-release-el7-9.noarch 8.6 k

事務概要
================================================================================
安裝  1 套裝軟體

總計:8.6 k
安裝大小:8.6 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告:RPM 資料庫已被非 yum 程式修改。
** 發現 7 個已存在的 RPM 資料庫問題, 'yum check' 輸出如下:
icedtea-web-1.6.2-4.el7.x86_64 有缺少的需求 java-1.8.0-openjdk
ipa-client-4.4.0-12.el7.centos.x86_64 有已安裝衝突 freeipa-client: ipa-client-4.4.0-12.el7.centos.x86_64
ipa-client-common-4.4.0-12.el7.centos.noarch 有已安裝衝突 freeipa-client-common: ipa-client-common-4.4.0-12.el7.centos.noarch
ipa-common-4.4.0-12.el7.centos.noarch 有已安裝衝突 freeipa-common: ipa-common-4.4.0-12.el7.centos.noarch
jline-1.0-8.el7.noarch 有缺少的需求 java >= ('0', '1.5', None)
1:libreoffice-core-5.0.6.2-3.el7.x86_64 有缺少的需求 java-headless >= ('1', '1.6', None)
1:libreoffice-ure-5.0.6.2-3.el7.x86_64 有缺少的需求 libjvm.so()(64bit)
  正在安裝    : mysql57-community-release-el7-9.noarch                      1/1
  驗證中      : mysql57-community-release-el7-9.noarch                      1/1

已安裝:
  mysql57-community-release.noarch 0:el7-9                                     

完畢!

4 檢查mysql源是否安裝成功,如下截圖顯示mysql源安裝成功
[root@localhost src]# yum repolist enabled |grep "mysql.*-community.*"
mysql-connectors-community/x86_64        MySQL Connectors Community           3
mysql-tools-community/x86_64             MySQL Tools Community                4
mysql57-community/x86_64                 MySQL 5.7 Community Server          16
[root@localhost src]#

5 安裝mysql
[root@localhost src]# yum install mysql-community-server

已載入外掛程式:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * extras: centos.ustc.edu.cn
 * updates: centos.ustc.edu.cn
正在解決依賴關係
--> 正在檢查事務
---> 套裝軟體 mysql-community-server.x86_64.0.5.7.17-1.el7 將被 安裝
--> 正在處理依賴關係 mysql-community-common(x86-64) = 5.7.17-1.el7,它被套裝軟體 mysql-community-server-5.7.17-1.el7.x86_64 需要
--> 正在處理依賴關係 mysql-community-client(x86-64) >= 5.7.9,它被套裝軟體 mysql-community-server-5.7.17-1.el7.x86_64 需要
--> 正在檢查事務
---> 套裝軟體 mysql-community-client.x86_64.0.5.7.17-1.el7 將被 安裝
--> 正在處理依賴關係 mysql-community-libs(x86-64) >= 5.7.9,它被套裝軟體 mysql-community-client-5.7.17-1.el7.x86_64 需要
---> 套裝軟體 mysql-community-common.x86_64.0.5.7.17-1.el7 將被 安裝
--> 正在檢查事務
---> 套裝軟體 mariadb-libs.x86_64.1.5.5.52-1.el7 將被 取代
--> 正在處理依賴關係 libmysqlclient.so.18()(64bit),它被套裝軟體 2:postfix-2.10.1-6.el7.x86_64 需要
--> 正在處理依賴關係 libmysqlclient.so.18(libmysqlclient_18)(64bit),它被套裝軟體 2:postfix-2.10.1-6.el7.x86_64 需要
---> 套裝軟體 mysql-community-libs.x86_64.0.5.7.17-1.el7 將被 捨棄
--> 正在檢查事務
---> 套裝軟體 mysql-community-libs-compat.x86_64.0.5.7.17-1.el7 將被 捨棄
--> 解決依賴關係完成

依賴關係解決

======================================================================================================================================================
 Package                                        架構                      版本                                                                大小
======================================================================================================================================================
正在安裝:
 mysql-community-libs                           x86_64                    5.7.17-1.el7                     mysql57-community                    2.1 M
      替換  mariadb-libs.x86_64 1:5.5.52-1.el7
 mysql-community-libs-compat                    x86_64                    5.7.17-1.el7                     mysql57-community                    2.0 M
      替換  mariadb-libs.x86_64 1:5.5.52-1.el7
 mysql-community-server                         x86_64                    5.7.17-1.el7                     mysql57-community                    162 M
為依賴而安裝:
 mysql-community-client                         x86_64                    5.7.17-1.el7                     mysql57-community                     24 M
 mysql-community-common                         x86_64                    5.7.17-1.el7                     mysql57-community                    271 k

事務概要
======================================================================================================================================================
安裝  3 套裝軟體 (+2 依賴套裝軟體)

總下載量:190 M
Is this ok [y/d/N]: y
Downloading packages:
警告:/var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.17-1.el7.x86_64.rpm: V3 DSA/SHA1 Signature, 金鑰 ID 5072e1f5: NOKEY
mysql-community-common-5.7.17-1.el7.x86_64.rpm 的公開金鑰尚未安裝
(1/5): mysql-community-common-5.7.17-1.el7.x86_64.rpm                                                                          | 271 kB  00:00:00    
(2/5): mysql-community-libs-5.7.17-1.el7.x86_64.rpm                                                                            | 2.1 MB  00:00:03    
(3/5): mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm                                                                     | 2.0 MB  00:00:02    
(4/5): mysql-community-client-5.7.17-1.el7.x86_64.rpm                                                                          |  24 MB  00:00:29    
(5/5): mysql-community-server-5.7.17-1.el7.x86_64.rpm                                                                          | 162 MB  00:01:23    
------------------------------------------------------------------------------------------------------------------------------------------------------
總計                                                                                                                  2.1 MB/s | 190 MB  00:01:29    
file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 檢索金鑰
導入 GPG key 0x5072E1F5:
 用戶ID     : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
 指紋       : a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 套裝軟體     : mysql57-community-release-el7-9.noarch (installed)
 來自       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
是否繼續?[y/N]y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安裝    : mysql-community-common-5.7.17-1.el7.x86_64                                                                                        1/6
  正在安裝    : mysql-community-libs-5.7.17-1.el7.x86_64                                                                                          2/6
  正在安裝    : mysql-community-client-5.7.17-1.el7.x86_64                                                                                        3/6
  正在安裝    : mysql-community-server-5.7.17-1.el7.x86_64                                                                                        4/6
  正在安裝    : mysql-community-libs-compat-5.7.17-1.el7.x86_64                                                                                   5/6
  正在刪除    : 1:mariadb-libs-5.5.52-1.el7.x86_64                                                                                                6/6
  驗證中      : mysql-community-common-5.7.17-1.el7.x86_64                                                                                        1/6
  驗證中      : mysql-community-server-5.7.17-1.el7.x86_64                                                                                        2/6
  驗證中      : mysql-community-libs-5.7.17-1.el7.x86_64                                                                                          3/6
  驗證中      : mysql-community-libs-compat-5.7.17-1.el7.x86_64                                                                                   4/6
  驗證中      : mysql-community-client-5.7.17-1.el7.x86_64                                                                                        5/6
  驗證中      : 1:mariadb-libs-5.5.52-1.el7.x86_64                                                                                                6/6

已安裝:
  mysql-community-libs.x86_64 0:5.7.17-1.el7    mysql-community-libs-compat.x86_64 0:5.7.17-1.el7    mysql-community-server.x86_64 0:5.7.17-1.el7  

作為依賴被安裝:
  mysql-community-client.x86_64 0:5.7.17-1.el7                              mysql-community-common.x86_64 0:5.7.17-1.el7                            

替代:
  mariadb-libs.x86_64 1:5.5.52-1.el7                                                                                                                 

完畢!
[root@localhost src]#

6 啟動mysql服務(這個命令有點慢,大概2-3分鐘的樣子。。。,啟動時候螢幕沒有提示資訊)
[root@localhost src]# systemctl start mysqld
[root@localhost src]#

7 查看MySQL的啟動狀態(顯示下面的資訊,表示mysql啟動成功)
[root@localhost src]# systemctl status mysqld

mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 2017-02-25 08:01:43 CST; 1min 41s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 6228 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 6139 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 6230 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─6230 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

2 25 08:01:12 localhost systemd[1]: Starting MySQL Server...
2 25 08:01:43 localhost systemd[1]: Started MySQL Server.

8 設置開機啟動
shell> systemctl enable mysqld
shell> systemctl daemon-reload

9 修改root預設密碼
9.1 mysql安裝完成之後,在/var/log/mysqld.log檔中給root生成了一個預設密碼。通過下面的方式找到root預設密碼,然後登錄mysql進行修改:
[root@localhost log]# grep 'temporary password' /var/log/mysqld.log

2017-02-25T00:01:29.530714Z 1 [Note] A temporary password is generated for root@localhost: +Fx;UZHzy6MI

9.2 在下面的Enter password 輸入上面那個自動生成的密碼 +Fx;UZHzy6MI,回車後出現mysql>提示符號
[root@localhost log]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17

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>

9.3 mysql提示符號下,輸入如下命令,修改mysql root使用者密碼,把密碼修改為Root123456!@ (預設密碼策略要求:字母,數位,特殊字元,字母必須把含有大寫的)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Root123456!@';

9.4 注意:mysql5.7預設安裝了密碼安全檢查外掛程式(validate_password),預設密碼檢查策略要求密碼必須包含:大小寫字母、數位和特殊符號,並且長度不能少於8位。否則會提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements錯誤,如下圖所示:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

9.5 通過環境變數查看密碼策略資訊
validate_password_policy              | MEDIUM 表示為當前使用的是中級密碼策略
mysql> 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_check_user_name     | 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      |
+---------------------------------------+--------+
14 rows in set (0.01 sec)

mysql>

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配置,指定密碼策略

# 選擇0LOW),1MEDIUM),2STRONG)其中一種,選擇2需要提供密碼字典檔
validate_password_policy=0

如果不需要密碼策略,添加my.cnf檔中添加如下配置禁用即可:

validate_password = off

重新開機mysql服務使配置生效:

systemctl restart mysqld

10 添加遠端登陸用戶
默認只允許root帳戶在本地登錄,如果要在其它機器上連接mysql,必須修改root允許遠端連接,或者添加一個允許遠端連接的帳戶,為了安全起見,我添加一個新的帳戶:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'zhan'@'%' IDENTIFIED BY 'Zhan123456!@' WITH GRANT OPTION;

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>


11 配置預設編碼為utf8
11.1 首先退出mysql  mysql> exit
11.2 修改/etc/my.cnf設定檔,在[mysqld]下添加編碼配置,如下所示:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
#
# 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
"/etc/my.cnf" 29L, 1016C

12 重新開機mysql服務,讓11節設置的編碼生效
[root@localhost log]# systemctl restart mysqld
[root@localhost log]#

13 重新登陸mysql
[root@localhost log]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17 MySQL Community Server (GPL)

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>

14 查看mysql資料庫編碼
mysql> show variables like '%character%'
    -> ;
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

mysql>

15 mysql預設設定檔路徑
設定檔:/etc/my.cnf 
日誌檔:/var/log//var/log/mysqld.log 
服務啟動腳本:/usr/lib/systemd/system/mysqld.service 

socket文件:/var/run/mysqld/mysqld.pid