iT邦幫忙

第 11 屆 iT 邦幫忙鐵人賽

DAY 10
0
自我挑戰組

30天學 LAMP 安裝系列 第 10

Day10 CentOS 7 安裝 MySQL 5.7/8.0

前言

根據 MySQL 的官方網頁 (https://www.mysql.com/support/supportedplatforms/database.html),MySQL 在 CentOS 7 作業系統支援的版本為5.6、5.7 與 8.0,等會我們就嘗試在 CentOS 7 安裝 MySQL 5.7 與 8.0 版本。

預先準備動作

  • 安裝 yum-utils 套件,等會需要用到 yum-config-manager 指令

    $ sudo yum install yum-utils -y
    
  • 安裝相對應套件,就會加入 MySQL 的 Yum 源,此 Yum 源已經將 MySQL 眾多軟體、眾多版本的套件都給內含了。

    $ sudo yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm -y
    

MySQL 5.7 安裝步驟

  • 如果要安裝的 MySQL 版本是 5.7:

    • 停用 MySQL 8.0 的 Yum 源,並啟用 MySQL 5.7 的 Yum 源

      $ sudo yum-config-manager --disable mysql80-community
      $ sudo yum-config-manager --enable mysql57-community
      
    • 檢查已啟用的 MySQL Yum 原是否為 5.7 版

      $ sudo yum repolist enabled | grep "mysql.*-community.*"
      mysql-connectors-community/x86_64       MySQL Connectors Community           118
      mysql-tools-community/x86_64            MySQL Tools Community                 95
      mysql57-community/x86_64                MySQL 5.7 Community Server           364
      
  • 如果要安裝的 MySQL 版本是 8.0:

    • 無需更換 Yum 源,因為預設就是 MySQL 8.0 的 Yum 源

    • 檢查已啟用的 MySQL Yum 源是否為 8.0 版

      $ sudo yum repolist enabled | grep "mysql.*-community.*"
      mysql-connectors-community/x86_64       MySQL Connectors Community           118
      mysql-tools-community/x86_64            MySQL Tools Community                 95
      mysql80-community/x86_64                MySQL 8.0 Community Server           129
      
  • 安裝 MySQL

    $ sudo yum install mysql-community-server -y
    
  • 啟動 MySQL 服務

    $ sudo systemctl start mysqld
    
  • 啟用 MySQL 服務,讓 MySQL 服務每次開機就會自動被帶起來

    $ sudo systemctl enable mysqld
    
  • 檢視 MySQL 服務啟動狀態

    $ sudo systemctl status mysqld
     mysqld.service - MySQL Server
       Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
       Active: active (running) since Tue 2019-09-10 20:37:18 CST; 48s ago
         Docs: man:mysqld(8)
               http://dev.mysql.com/doc/refman/en/using-systemd.html
     Main PID: 1825 (mysqld)
       CGroup: /system.slice/mysqld.service
               1825 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysql...
    
    Sep 10 20:37:15 centos7-cli.lab.example.com systemd[1]: Starting MySQL Server...
    Sep 10 20:37:18 centos7-cli.lab.example.com systemd[1]: Started MySQL Server.
    
  • 查目前 MySQL 資料庫系統內,root 帳號的暫時密碼(這部份是 MariaDB 所沒有的)

    $ sudo grep 'temporary password' /var/log/mysqld.log
    2019-09-10T13:28:00.598194Z 1 [Note] A temporary password is generated for root@localhost: uKx=wIr*K9.l
    
  • 加強 MySQL 安裝的安全性

    $ sudo mysql_secure_installation 
    
    Securing the MySQL server deployment.
    
    Enter password for user root: 
    
    The existing password for the user account root has expired. Please set a new password.
    
    New password: 
    
    Re-enter new password: 
    The 'validate_password' plugin is installed on the server.
    The subsequent steps will run with the existing configuration
    of the plugin.
    Using existing password for root.
    
    Estimated strength of the password: 100 
    Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y
    
    New password: 
    
    Re-enter new password: 
    
    Estimated strength of the password: 100 
    Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
    By default, a MySQL installation has an anonymous user,
    allowing anyone to log into MySQL without having to have
    a user account created for them. This is intended only for
    testing, and to make the installation go a bit smoother.
    You should remove them before moving into a production
    environment.
    
    Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
    Success.
    
    
    Normally, root should only be allowed to connect from
    'localhost'. This ensures that someone cannot guess at
    the root password from the network.
    
    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
    Success.
    
    By default, MySQL comes with a database named 'test' that
    anyone can access. This is also intended only for testing,
    and should be removed before moving into a production
    environment.
    
    
    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
     - Dropping test database...
    Success.
    
     - Removing privileges on test database...
    Success.
    
    Reloading the privilege tables will ensure that all changes
    made so far will take effect immediately.
    
    Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
    Success.
    
    All done! 
    
    • 這段 script 主要在做以下動作:
      • 設定 root 帳號的密碼(此 root 是 MySQL 資料庫系統內的帳號,跟作業系統層的 root 無關)。
      • 禁止 root 從非本機登入。
      • 移除 anonymous 匿名者帳號。
      • 移除測試資料庫。
    • 還有一點要說明,MySQL 比 MariaDB 多了 validate_password 這個預設安裝的外掛,他會檢查密碼是否符合複雜性要求:密碼至少包含一個大寫字母、一個小寫字母、一個數字、以及一個特殊符號,密碼長度至少八個字元。
  • 查版本

    $ sudo mysqld -V
    mysqld  Ver 5.7.27 for Linux on x86_64 (MySQL Community Server (GPL))
    
  • 登入資料庫進行簡單測試

    $ mysql -u root -h localhost -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 9
    Server version: 5.7.27 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2019, 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;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    +--------------------+
    4 rows in set (0.01 sec)
    
    mysql> exit
    Bye
    

參考資料


上一篇
Day09 CentOS 7 安裝 MariaDB
下一篇
Day11 CentOS 7 安裝 Apache
系列文
30天學 LAMP 安裝33

尚未有邦友留言

立即登入留言