All Products
Search
Document Center

Elastic Compute Service:Install MySQL on a Linux instance

Last Updated:Mar 31, 2025

This topic describes how to manually deploy a specific version of MySQL database on an Elastic Compute Service (ECS) instance that runs Linux.

Prerequisites

  • A public IP address is automatically assigned to the ECS instance. Alternatively, an elastic IP address (EIP) is associated with the ECS instance. For instructions on how to enable public bandwidth, see Enable public bandwidth.

  • An inbound rule is added to a security group associated with the ECS instance to open port 22. For information about how to add a security group rule, see Add a security group rule.

Deploy MySQL

Alibaba Cloud Linux 3

  1. Connect to an ECS instance. For more information, see Use Workbench to connect to a Linux instance over SSH.

  2. Install MySQL.

    Note

    For example, to install MySQL 8.0, change the repository address to https://repo.mysql.com/mysql80-community-release-el8-1.noarch.rpm.

    # Install compat-openssl10, which is compatible with earlier versions of OpenSSL libraries. 
    sudo yum install -y compat-openssl10
    # Add an official MySQL repository.
    sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el8-1.noarch.rpm
    # Install MySQL.
    sudo dnf install -y mysql-server
    # Start and configure MySQL to automatically start on system startup.
    sudo systemctl start mysqld
    sudo systemctl enable mysqld
  3. Query the default initial password of the root user.

    echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD)
  4. Run the following command to configure MySQL security settings:

    sudo mysql_secure_installation
    1. Enter the initial password of the root user.

      Note

      The password characters are hidden when you enter the password. Make sure that the password that you enter is correct.

      image

    2. If an error message indicating that the root password expired appears, specify a new password for the root user by entering the new password twice.

      Important

      The password must be at least eight characters in length, and contain at least one uppercase letter, one lowercase letter, one digit, and one special character.

      image

    3. After you change the root password, enter n to skip re-specifying a new password. image

    4. Enter y to remove the anonymous user.

      Note

      This prevents unauthorized users from skipping the step of entering valid credentials and accessing your MySQL server.

      image

    5. Enter y to prevent the root user from accessing MySQL.

      Note

      If you want to allow the root user to connect to the MySQL server in specific scenarios, press a key other than y to allow remote root user logons.

      image

    6. Enter y to remove the pre-installed test database of the MySQL server.image

    7. Enter y to reload the permissions table to allow the preceding modifications to take effect.image

Alibaba Cloud Linux 2

  1. Connect to an ECS instance. For more information, see Use Workbench to connect to a Linux instance over SSH.

  2. Install MySQL.

    Note

    For example, to install MySQL 8.0, change the repository address to https://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm.

    # Add an official MySQL repository.
    sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el7-1.noarch.rpm
    # Install MySQL.
    sudo yum install -y mysql-server
    # Start and configure MySQL to automatically start on system startup.
    sudo systemctl start mysqld
    sudo systemctl enable mysqld
  3. Query the default initial password of the root user.

    echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD)
  4. Run the following command to configure MySQL security settings:

    sudo mysql_secure_installation
    1. Enter the initial password of the root user.

      Note

      The password characters are hidden when you enter the password. Make sure that the password that you enter is correct.

      image

    2. If an error message indicating that the root password expired appears, specify a new password for the root user by entering the new password twice.

      Important

      The password must be at least eight characters in length, and contain at least one uppercase letter, one lowercase letter, one digit, and one special character.

      image

    3. After you change the root password, enter n to skip re-specifying a new password. image

    4. Enter y to remove the anonymous user.

      Note

      This prevents unauthorized users from skipping the step of entering valid credentials and accessing your MySQL server.

      image

    5. Enter y to prevent the root user from accessing MySQL.

      Note

      If you want to allow the root user to connect to the MySQL server in specific scenarios, press a key other than y to allow remote root user logons.

      image

    6. Enter y to remove the pre-installed test database of the MySQL server.image

    7. Enter y to reload the permissions table to allow the preceding modifications to take effect.image

CentOS 8

  1. Connect to an ECS instance. For more information, see Use Workbench to connect to a Linux instance over SSH.

  2. Install MySQL.

    Note

    For example, to install MySQL 8.0, change the repository address to https://repo.mysql.com/mysql80-community-release-el8-1.noarch.rpm.

    # Add an official MySQL repository.
    sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el8-1.noarch.rpm
    # Install MySQL.
    sudo dnf install -y mysql-server
    # Start and configure MySQL to automatically start on system startup.
    sudo systemctl start mysqld
    sudo systemctl enable mysqld
  3. Run the following command to configure MySQL security settings:

    sudo mysql_secure_installation
    1. Enter y to enable the VALIDATE PASSWORD component.image

    2. Enter 2. The password must be at least eight characters in length, and contain at least one uppercase letter, one lowercase letter, one digit, and one special character.image

    3. To specify a new password for the root user, enter the new password twice.

      Important

      The password must be at least eight characters in length, and contain at least one uppercase letter, one lowercase letter, one digit, and one special character.

      image

    4. Enter y to continue using the provided password.image

    5. Enter y to remove the anonymous user.

      Note

      This prevents unauthorized users from skipping the step of entering valid credentials and accessing your MySQL server.

      image

    6. Enter y to prevent the root user from accessing MySQL.

      Note

      If you want to allow the root user to connect to the MySQL server in specific scenarios, press a key other than y to allow remote root user logons.

      image

    7. Enter y to remove the pre-installed test database of the MySQL server.image

    8. Enter y to reload the permissions table to allow the preceding modifications to take effect.image

CentOS 7

  1. Connect to an ECS instance. For more information, see Use Workbench to connect to a Linux instance over SSH.

  2. Install MySQL.

    Note

    For example, to install MySQL 8.0, change the repository address to https://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm.

    # Add an official MySQL repository.
    sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el7-1.noarch.rpm
    # Install MySQL.
    sudo yum install -y mysql-server
    # Start and configure MySQL to automatically start on system startup.
    sudo systemctl start mysqld
    sudo systemctl enable mysqld
  3. Query the default initial password of the root user.

    echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD)
  4. Run the following command to configure MySQL security settings:

    sudo mysql_secure_installation
    1. Enter the initial password of the root user.

      Note

      The password characters are hidden when you enter the password. Make sure that the password that you enter is correct.

      image

    2. If an error message indicating that the root password expired appears, specify a new password for the root user by entering the new password twice.

      Important

      The password must be at least eight characters in length, and contain at least one uppercase letter, one lowercase letter, one digit, and one special character.

      image

    3. After you change the root password, enter n to skip re-specifying a new password. image

    4. Enter y to remove the anonymous user.

      Note

      This prevents unauthorized users from skipping the step of entering valid credentials and accessing your MySQL server.

      image

    5. Enter y to prevent the root user from accessing MySQL.

      Note

      If you want to allow the root user to connect to the MySQL server in specific scenarios, press a key other than y to allow remote root user logons.

      image

    6. Enter y to remove the pre-installed test database of the MySQL server.image

    7. Enter y to reload the permissions table to allow the preceding modifications to take effect.image

Ubuntu or Debian

  1. Connect to an ECS instance. For more information, see Use Workbench to connect to a Linux instance over SSH.

  2. Run the following command to update the system Advanced Packaging Tool (APT) repository and ensure that the system package list is updated:

    sudo apt-get update
  3. Install the MySQL APT repository.

    # Download the APT configuration package.
    sudo wget https://dev.mysql.com/get/mysql-apt-config_0.8.33-1_all.deb
    # Run the dpkg command to install the MySQL APT configuration package that you downloaded.
    sudo dpkg -i mysql-apt-config_0.8.33-1_all.deb
    1. Optional. If the mysql-apt-config script detects that the operating system version is not officially supported, the system prompts you to select the closest supported version to proceed with installation.

      Note

      For information about how to obtain the system alias, see How do I obtain the system alias of a Ubuntu or Debian instance?

      image

    2. The configuration page displays all components that must be installed, such as MySQL Server, MySQL Client, and required tools. Confirm the version and components that you want to install, press the down arrow key to select OK, and then press the Enter key.image

  4. Install the MySQL server.

    Important

    During installation, you must follow the password policy when you specify a password for the MySQL root user. The password must be at least eight characters in length, and contain at least one uppercase letter, one lowercase letter, one digit, and one special character. Enter your password twice and press the Enter key. imageimage

    # Update the package list.
    sudo apt update
    # Install the MySQL server.
    sudo apt install -y mysql-server
    # Start the MySQL service.
    sudo systemctl start mysql
    # Check the startup status.
    sudo systemctl status mysql
    # Configure MySQL to automatically start on system startup. 
    sudo systemctl enable mysql
  5. Run the following command to configure MySQL security settings:

    sudo mysql_secure_installation
    1. Enter the root password. image

    2. Enter y to enable the VALIDATE PASSWORD component.image

    3. Enter 2. The password must be at least eight characters in length, and contain at least one uppercase letter, one lowercase letter, one digit, and one special character.image

    4. Enter n to skip changing the password of the root user.

      Note

      To change the password of the root user, enter y.

      image

    5. Enter y to remove the anonymous user.

      Note

      This prevents unauthorized users from skipping the step of entering valid credentials and accessing your MySQL server.

      image

    6. Enter y to prevent the root user from accessing MySQL.

      Note

      If you want to allow the root user to connect to the MySQL server in specific scenarios, press a key other than y to allow remote root user logons.

      image

    7. Enter y to remove the pre-installed test database of the MySQL server.image

    8. Enter y to reload the permissions table to allow the preceding modifications to take effect.image

Add a MySQL user for remote access

  1. Connect to an ECS instance. For more information, see Use Workbench to connect to a Linux instance over SSH.

  2. By default, MySQL uses port 3306. Make sure that port 3306 is open in an inbound security rule of a security group associated with the instance. If you use a different MySQL port, modify the security group settings based on the actual port. For more information, see Add a security group rule.

  3. Run the following command to create a user and grant the user access to all MySQL databases.

    Important
    • Replace <username> with the username of the created MySQL user.

    • Replace <password> with the password you used to create the MySQL user. The password must be at least eight characters in length, and contain at least one uppercase letter, one lowercase letter, one digit, and one special character.

    # After you run the command, enter the password of the root user.
    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. Run the following command to check whether you can connect to MySQL by using the username and password of the MySQL user that you created:

    # Replace <username> with the username of the user that you created. After you run the command, enter the password of the new user.
    sudo mysql -u<username> -p

Reference

MySQL deployment FAQ