全部產品
Search
文件中心

Elastic Compute Service:在Linux執行個體中安裝MySQL資料庫

更新時間:Jun 03, 2026

如果您需要在運行Linux系統的ECS執行個體上部署特定版本的MySQL資料庫,可以參考本文提供的步驟手動完成MySQL資料庫的部署。

前提條件

  • 執行個體已指派固定公網IP地址或綁定Elastic IP Address(EIP)。如您不清楚如何開通公網,請參見開通公網

  • ECS執行個體的安全性群組入方向規則已允許存取22連接埠。具體操作,請參見添加安全性群組規則

部署MySQL

Alibaba Cloud Linux 3

  1. 遠端連線ECS執行個體。具體操作,請參見使用Workbench登入Linux執行個體

  2. 安裝MySQL資料庫。

    #需要安裝compat-openssl10與舊版本OpenSSL庫相容。
    sudo yum install -y compat-openssl10
    #添加MySQL官方源
    sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el8-1.noarch.rpm
    #安裝MySQL服務
    sudo dnf install -y mysql-server
    #啟動MySQL服務並設定開機自啟
    sudo systemctl start mysqld
    sudo systemctl enable mysqld
  3. 查看root使用者預設初始密碼。

    echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD)
  4. 運行命令,啟動MySQL安全安裝嚮導。

    sudo mysql_secure_installation
    1. 輸入root使用者初始密碼。

      說明

      輸入密碼時介面不會顯示密碼的輸入過程,請您確保輸入的密碼正確。

      Securing the MySQL server deployment.
      Enter password for user root:
    2. 提示root密碼已到期,您需要設定新的root使用者密碼,需要輸入兩次新密碼。

      重要

      密碼原則要求密碼至少包含一個大寫字母、一個小寫字母、一個數字和一個特殊字元,並且密碼總長度至少為8個字元。

      The existing password for the user account root has expired. Please set a new password.
      New password:
      Re-enter new password:
    3. 修改root密碼後,輸入n,拒絕再次修改密碼。

      Change the password for root ? (Press y|Y for Yes, any other key for No) :
    4. 輸入y,移除匿名使用者。

      說明

      移除匿名使用者可以防止未經授權的使用者在沒有提供有效憑證的情況下訪問您的 MySQL 伺服器。

      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) :
    5. 輸入y,禁止MySQL的root使用者從遠程登入。

      說明

      如果有特定需求允許root使用者遠端存取,可以按Y以外任意鍵以允許遠程 root 登入。

      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) :
    6. 輸入y,移除MySQL內建的test資料庫。

      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) :
    7. 輸入y,重新載入許可權表,使之前所有更改生效。

      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) :

Alibaba Cloud Linux 2

  1. 遠端連線ECS執行個體。具體操作,請參見使用Workbench登入Linux執行個體

  2. 安裝MySQL資料庫。

    #添加MySQL官方源
    sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el7-1.noarch.rpm
    #安裝MySQL服務
    sudo yum install -y mysql-server
    #啟動MySQL服務並設定開機自啟
    sudo systemctl start mysqld
    sudo systemctl enable mysqld
  3. 查看root使用者預設初始密碼。

    echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD)
  4. 運行命令,啟動MySQL安全安裝嚮導。

    sudo mysql_secure_installation
    1. 輸入root使用者初始密碼。

      說明

      輸入密碼時介面不會顯示密碼的輸入過程,請您確保輸入的密碼正確。

      Securing the MySQL server deployment.
      Enter password for user root:
    2. 提示root密碼已到期,您需要設定新的root使用者密碼,需要輸入兩次新密碼。

      重要

      密碼原則要求密碼至少包含一個大寫字母、一個小寫字母、一個數字和一個特殊字元,並且密碼總長度至少為8個字元。

      The existing password for the user account root has expired. Please set a new password.
      New password:
      Re-enter new password:
    3. 修改root密碼後,輸入n,拒絕再次修改密碼。

      Change the password for root ? (Press y|Y for Yes, any other key for No) :
    4. 輸入y,移除匿名使用者。

      說明

      移除匿名使用者可以防止未經授權的使用者在沒有提供有效憑證的情況下訪問您的 MySQL 伺服器。

      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) :
    5. 輸入y,禁止MySQL的root使用者從遠程登入。

      說明

      如果有特定需求允許root使用者遠端存取,可以按Y以外任意鍵以允許遠程 root 登入。

      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) :
    6. 輸入y,移除MySQL內建的test資料庫。

      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) :
    7. 輸入y,重新載入許可權表,使之前所有更改生效。

      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) :

CentOS 8

  1. 遠端連線ECS執行個體。具體操作,請參見使用Workbench登入Linux執行個體

  2. 安裝MySQL資料庫。

    #添加MySQL官方源
    sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el8-1.noarch.rpm
    #安裝MySQL服務
    sudo dnf install -y mysql-server
    #啟動MySQL服務並設定開機自啟
    sudo systemctl start mysqld
    sudo systemctl enable mysqld
  3. 運行命令,啟動MySQL安全安裝嚮導。

    sudo mysql_secure_installation
    1. 輸入y,啟用VALIDATE PASSWORD組件。

      Securing the MySQL server deployment.
      Connecting to MySQL using a blank password.
      VALIDATE PASSWORD COMPONENT 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 component?
      Press y|Y for Yes, any other key for No:
    2. 輸入2,設定密碼原則要求密碼至少包含一個大寫字母、一個小寫字母、一個數字和一個特殊字元,並且密碼總長度至少為8個字元。

      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                  file
      Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:
    3. 您需要設定新的root使用者密碼,需要輸入兩次新密碼。

      New password:
      
      Re-enter new password:
      
      Estimated strength of the password: 100
      重要

      密碼原則要求密碼至少包含一個大寫字母、一個小寫字母、一個數字和一個特殊字元,並且密碼總長度至少為8個字元。

    4. 輸入y,繼續使用提供的密碼。

      Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :
    5. 輸入y,移除匿名使用者。

      說明

      移除匿名使用者可以防止未經授權的使用者在沒有提供有效憑證的情況下訪問您的 MySQL 伺服器。

      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) :
    6. 輸入y,禁止MySQL的root使用者從遠程登入。

      說明

      如果有特定需求允許root使用者遠端存取,可以按Y以外任意鍵以允許遠程 root 登入。

      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) :
    7. 輸入y,移除MySQL內建的test資料庫。

      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) :
    8. 輸入y,重新載入許可權表,使之前所有更改生效。

      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) :

CentOS 7

  1. 遠端連線ECS執行個體。具體操作,請參見使用Workbench登入Linux執行個體

  2. 安裝MySQL資料庫。

    #添加MySQL官方源
    sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el7-1.noarch.rpm
    #安裝MySQL服務
    sudo yum install -y mysql-server
    #啟動MySQL服務並設定開機自啟
    sudo systemctl start mysqld
    sudo systemctl enable mysqld
  3. 查看root使用者預設初始密碼。

    echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD)
  4. 運行命令,啟動MySQL安全安裝嚮導。

    sudo mysql_secure_installation
    1. 輸入root使用者初始密碼。

      說明

      輸入密碼時介面不會顯示密碼的輸入過程,請您確保輸入的密碼正確。

      Securing the MySQL server deployment.
      Enter password for user root:
    2. 提示root密碼已到期,您需要設定新的root使用者密碼,需要輸入兩次新密碼。

      重要

      密碼原則要求密碼至少包含一個大寫字母、一個小寫字母、一個數字和一個特殊字元,並且密碼總長度至少為8個字元。

      The existing password for the user account root has expired. Please set a new password.
      New password:
      Re-enter new password:
    3. 修改root密碼後,輸入n,拒絕再次修改密碼。

      Change the password for root ? (Press y|Y for Yes, any other key for No) :
    4. 輸入y,移除匿名使用者。

      說明

      移除匿名使用者可以防止未經授權的使用者在沒有提供有效憑證的情況下訪問您的 MySQL 伺服器。

      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) :
    5. 輸入y,禁止MySQL的root使用者從遠程登入。

      說明

      如果有特定需求允許root使用者遠端存取,可以按Y以外任意鍵以允許遠程 root 登入。

      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) :
    6. 輸入y,移除MySQL內建的test資料庫。

      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) :
    7. 輸入y,重新載入許可權表,使之前所有更改生效。

      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) :

Ubuntu/Debian

  1. 遠端連線ECS執行個體。具體操作,請參見使用Workbench登入Linux執行個體

  2. 運行以下命令,更新系統apt倉庫,確保您的系統包列表為最新。

    sudo apt-get update
  3. 安裝MySQL APT存放庫。

    #下載APT配置包
    sudo wget https://dev.mysql.com/get/mysql-apt-config_0.8.33-1_all.deb
    #使用 dpkg 命令安裝下載的 MySQL APT 配置包
    sudo dpkg -i mysql-apt-config_0.8.33-1_all.deb
    1. (可選)若mysql-apt-config指令碼檢測到您的作業系統版本不是其正式支援的版本,它將顯示一個介面,讓您選擇最接近您系統版本的支援選項以繼續安裝。

      說明

      擷取系統別名請參見部署MySQL常見問題

      Configuring mysql-apt-config
      The detected system (ubuntu bionic) is not supported by MySQL. If you believe the platform is compatible with one of the supported systems, one of the corresponding repositories may be selected.
      Add repository to unsupported system?
                                  ubuntu lunar
                                  ubuntu focal
                                  ubuntu jammy
                                  ubuntu noble
                                  ubuntu oracular
                                  debian bookworm
                                  abort
                                     <Ok>
    2. 配置頁面會列出待安裝的全部組件,其中包括MySQL Server 、Client、Tool等一系列組件,確認版本及待安裝組件無誤,按方向鍵切換到OK並斷行符號。

      Configuring mysql-apt-config
      The MySQL APT Repository features MySQL Server along with a variety of components. You may select the desired products to install and update from the official MySQL Repository, and also select the associated version series.
      Select "Ok" to save the configuration, and then execute "apt update" to load the selected package list. This configuration can be updated later, depending on your needs.
      Which MySQL product do you wish to configure?
          MySQL Server & Cluster (Currently selected: mysql-8.4-lts)
          MySQL Connectors (Currently selected: Enabled)
          Ok
                                    <Ok>
  4. 安裝MySQL伺服器。

    重要

    在安裝過程中,為MySQL的root使用者佈建密碼時,需遵循以下密碼原則:密碼必須包含至少一個大寫字母、一個小寫字母、一個數字和一個特殊字元,且總長度不少於8個字元。輸入您的密碼按斷行符號鍵確認,輸入兩次後完成設定。在安裝過程中彈出 Configuring mysql-community-server 配置對話方塊,提示為 MySQL root 賬戶設定強密碼;若留空則啟用 UNIX socket 認證。在 Enter root password 輸入框中輸入密碼,然後選擇 Ok。在 Configuring mysql-community-server 對話方塊中,在 Re-enter root password 輸入框中再次輸入 root 密碼進行確認,然後選擇 Ok。請勿與任何人共用該密碼。

    #更新包列表
    sudo apt update
    #安裝 MySQL 伺服器
    sudo apt install -y mysql-server
    #啟動 MySQL 服務
    sudo systemctl start mysql
    #檢查啟動狀態
    sudo systemctl status mysql
    #在系統啟動時自動啟動。
    sudo systemctl enable mysql
  5. 運行命令,啟動MySQL安全安裝嚮導。

    sudo mysql_secure_installation
    1. 輸入root使用者密碼。

    2. 輸入y,啟用VALIDATE PASSWORD組件。

      Securing the MySQL server deployment.
      Connecting to MySQL using a blank password.
      VALIDATE PASSWORD COMPONENT 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 component?
      Press y|Y for Yes, any other key for No:
    3. 輸入2,設定密碼原則要求密碼至少包含一個大寫字母、一個小寫字母、一個數字和一個特殊字元,並且密碼總長度至少為8個字元。

      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                  file
      Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:
    4. 輸入n,拒絕修改root使用者密碼。

      說明

      如果您需要更改root使用者密碼,可以輸入y以進行修改。

    5. 輸入y,移除匿名使用者。

      說明

      移除匿名使用者可以防止未經授權的使用者在沒有提供有效憑證的情況下訪問您的 MySQL 伺服器。

      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) :
    6. 輸入y,禁止MySQL的root使用者從遠程登入。

      說明

      如果有特定需求允許root使用者遠端存取,可以按Y以外任意鍵以允許遠程 root 登入。

      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) :
    7. 輸入y,移除MySQL內建的test資料庫。

      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) :
    8. 輸入y,重新載入許可權表,使之前所有更改生效。

      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) :

添加遠端存取MySQL使用者

  1. 遠端連線ECS執行個體。具體操作,請參見使用Workbench登入Linux執行個體

  2. 配置安全性群組時,MySQL 預設使用3306連接埠。確保執行個體安全性群組的入站規則開放3306連接埠。如果選擇了不同的連接埠,請根據實際情況調整安全性群組設定。具體步驟請參考添加安全性群組規則

  3. 執行命令建立使用者並設定其可以遠端存取MySQL所有資料庫。

    重要
    • 請將代碼中的<username>替換為建立MySQL使用者時所用的使用者名稱。

    • 請將代碼中的<password>替換為建立MySQL使用者時所用的密碼。需遵循以下密碼原則:密碼必須包含至少一個大寫字母、一個小寫字母、一個數字和一個特殊字元,且總長度不少於8個字元。

    #執行後需要輸入root使用者密碼
    sudo mysql -uroot -p \
    -e "CREATE USER '<username>'@'%' IDENTIFIED BY '<password>';" \
    -e "GRANT ALL PRIVILEGES ON *.* TO '<username>'@'%' WITH GRANT OPTION;" \
    -e "FLUSH PRIVILEGES;"
  4. 測試所配置的使用者名稱及密碼是否生效。

    #<username>是您新建立使用者的使用者名稱,執行後需要輸入您的新使用者密碼
    sudo mysql -u<username> -p

相關文檔

部署MySQL常見問題