全部產品
Search
文件中心

:安裝與配置MySQL資料庫

更新時間:Jul 13, 2024

通過閱讀本文,您可以瞭解到Linux環境下安裝和配置MySQL資料庫的方法。

操作步驟

  1. 登入Linux伺服器終端。
    說明 本文以Ubuntu 18.04版本為例說明,其他Linux發行版本,請根據實際情況操作。
  2. 更新軟體源並安裝wget。
    sudo apt update

    sudo apt install wget

  3. 下載MySQL資料庫安裝包。
    wget https://dev.mysql.com/get/mysql-apt-config_0.8.16-1_all.deb -y
    說明 如果沒有安裝wget,需要先執行apt install wget安裝wget。
  4. 使用deb封裝更新軟體源。
    sudo dpkg -i mysql-apt-config_0.8.16-1_all.deb

    在彈出的對話方塊中選擇MySQL Server & Cluster (Currently selected: mysql-5.7),然後選擇Ok

    install_mysql_01
  5. 擷取軟體包的所有可安裝版本。
    sudo apt update

    sudo apt-cache policy mysql-server

    mysql-server:
      Installed: (none)
      Candidate: 5.7.32-1ubuntu18.04
      Version table:
      ...

    其中,5.7.32-1ubuntu18.04表示MySQL資料庫安裝版本。

  6. 根據擷取到的安裝版本安裝MySQL5.7,安裝過程中需設定root使用者密碼並妥善儲存。
    sudo apt install -f mysql-client=5.7.32-1ubuntu18.04 mysql-community-server=5.7.32-1ubuntu18.04 mysql-server=5.7.32-1ubuntu18.04
  7. 配置MySQL資料庫。
    1. 設定常規化安全。
      sudo mysql_secure_installation

      請根據提示資訊設定,如下所示:

      Enter current password for root (enter for none): <Enter password>
      VALIDATE PASSWORD PLUGIN can be used to test passwords 
      and improve security. It checks the strength of password 
      and allows the users to set only those passwords which are 
      secure enough. Would you like to setup VALIDATE PASSWORD plugin? 
      
      Press y|Y for Yes, any other key for No: Y 
      
      There are three levels of password validation policy: 
      
      LOW    Length >= 8 
      MEDIUM Length >= 8, numeric, mixed case, and special characters 
      STRONG Length >= 8, numeric, mixed case, special characters and dictionary                 
      
      Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1 
      Using existing password for root. 
      Estimated strength of the password: 25  
      Change the password for root ? ((Press y|Y for Yes, any other key for No) : d
      Remove anonymous users? [Y/n] Y 
      Disallow root login remotely? [Y/n] Y 
      Remove test database and access to it? [Y/n] Y 
      Reload privilege tables now? [Y/n] Y 
    2. 開啟遠端存取。
      1. 編輯MySQL資料庫的設定檔。

        sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

      2. I鍵,將bind-address的值修改為0.0.0.0。
      3. Esc鍵,輸入:wq!儲存並退出。
    3. 重啟MySQL資料庫服務使配置生效。
      sudo systemctl restart mysql
  8. 驗證MySQL資料庫服務是否正常。
    1. 以root使用者登入MySQL資料庫。
      mysql -u root -p
    2. 在MySQL資料庫互動控制台查詢現有資料庫。
      show databases;
      +--------------------+
      | Database           |
      +--------------------+
      | information_schema |
      | mysql              |
      | performance_schema |
      | sys                |
      +--------------------+
      5 rows in set (0.00 sec)

      如上所示,現有MySQL資料庫服務正常。