All Products
Search
Document Center

Elastic Compute Service:Install MySQL on a Linux instance

Last Updated:Mar 14, 2025

To deploy a specific version of the MySQL database on an ECS instance with the Linux operating system, you can follow the steps outlined in this topic to manually complete the MySQL database deployment.

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.

  • The inbound rule for the security group associated with the ECS instance permits traffic through port 22. For detailed instructions, see Add Security Group Rules.

Deploy MySQL

Alibaba Cloud Linux 3

  1. Remotely connect to the ECS instance. For detailed instructions, see Logging on to a Linux instance using the Workbench tool over SSH.

  2. Install the MySQL database.

    Note

    To install MySQL version 8.0, change the official repository URL to https://repo.mysql.com/mysql80-community-release-el8-1.noarch.rpm.

    # Install compat-openssl10 to be compatible with the old version of the OpenSSL library.
    sudo yum install -y compat-openssl10
    # Add the official MySQL source
    sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el8-1.noarch.rpm
    # Install MySQL
    sudo dnf install -y mysql-server
    # Start MySQL and configure MySQL to automatically start on system startup
    sudo systemctl start mysqld
    sudo systemctl enable mysqld
  3. Retrieve the default initial password for the root user.

    echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD)
  4. Launch the MySQL secure installation wizard.

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

      Note

      Password characters are hidden during entry. Ensure accuracy when typing.

      image

    2. The root password has expired. You must set a new password for the root user and enter it twice for confirmation.

      Important

      The password must include at least one uppercase letter, one lowercase letter, one digit, one special character, and be at least eight characters long.

      image

    3. After you change the root password, enter n to decline resetting the password. image

    4. Enter 'y' to remove the anonymous user.

      Note

      Removing the anonymous user helps prevent unauthorized access to your MySQL server by users without valid credentials.

      image

    5. Enter y to disable remote logon for the MySQL root user.

      Note

      If you need to allow the root user remote access, press any key other than Y to enable remote root logon.

      image

    6. Enter 'y' to remove the default test database included with MySQL. image

    7. Enter 'y' to reload the privilege tables and ensure that all changes are applied. image

Alibaba Cloud Linux 2

  1. Remotely connect to the ECS instance. For detailed instructions, see Logging on to a Linux instance using the Workbench tool over SSH.

  2. Install the MySQL database.

    Note

    To install MySQL version 8.0, change the official source address to https://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm.

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

    echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD)
  4. Launch the MySQL secure installation wizard.

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

      Note

      Password characters are hidden during entry. Ensure accuracy when typing.

      image

    2. The root password has expired. You must set a new password for the root user and enter it twice for confirmation.

      Important

      The password must include at least one uppercase letter, one lowercase letter, one digit, one special character, and be at least eight characters long.

      image

    3. After you change the root password, enter n to decline resetting the password. image

    4. Enter 'y' to remove the anonymous user.

      Note

      Removing the anonymous user helps prevent unauthorized access to your MySQL server by users without valid credentials.

      image

    5. Enter y to disable remote logon for the MySQL root user.

      Note

      If you need to allow the root user remote access, press any key other than Y to enable remote root logon.

      image

    6. Enter 'y' to remove the default test database included with MySQL. image

    7. Enter 'y' to reload the privilege tables and ensure that all changes are applied. image

CentOS 8

  1. Remotely connect to the ECS instance. For detailed instructions, see Logging on to a Linux instance using the Workbench tool via SSH.

  2. Install the MySQL database.

    Note

    To install MySQL version 8.0, change the official repository URL to https://repo.mysql.com/mysql80-community-release-el8-1.noarch.rpm.

    # Add the official MySQL source
    sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el8-1.noarch.rpm
    # Install MySQL
    sudo dnf install -y mysql-server
    # Start MySQL and configure MySQL to automatically start on system startup
    sudo systemctl start mysqld
    sudo systemctl enable mysqld
  3. Launch the MySQL secure installation wizard.

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

    2. Select 2 to enforce a password policy requiring at least one uppercase letter, one lowercase letter, one digit, one special character, and a minimum length of eight characters. image

    3. You must set a new password for the root user and enter it twice for confirmation.

      Important

      The password must include at least one uppercase letter, one lowercase letter, one digit, one special character, and be at least eight characters long.

      image

    4. Enter y to confirm the use of the provided password. image

    5. Enter 'y' to remove the anonymous user.

      Note

      Removing the anonymous user helps prevent unauthorized access to your MySQL server by users without valid credentials.

      image

    6. Enter y to disable remote logon for the MySQL root user.

      Note

      If you need to allow the root user remote access, press any key other than Y to enable remote root logon.

      image

    7. Enter 'y' to remove the default test database included with MySQL. image

    8. Enter 'y' to reload the privilege tables and ensure that all changes are applied. image

CentOS 7

  1. Remotely connect to the ECS instance. For detailed instructions, see how to log on to a Linux instance using the Workbench tool over SSH.

  2. Install the MySQL database.

    Note

    To install MySQL version 8.0, change the official source address to https://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm.

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

    echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD)
  4. Launch the MySQL secure installation wizard.

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

      Note

      Password characters are hidden during entry. Ensure accuracy when typing.

      image

    2. The root password has expired. You must set a new password for the root user and enter it twice for confirmation.

      Important

      The password must include at least one uppercase letter, one lowercase letter, one digit, one special character, and be at least eight characters long.

      image

    3. After you change the root password, enter n to decline resetting the password. image

    4. Enter 'y' to remove the anonymous user.

      Note

      Removing the anonymous user helps prevent unauthorized access to your MySQL server by users without valid credentials.

      image

    5. Enter y to disable remote logon for the MySQL root user.

      Note

      If you need to allow the root user remote access, press any key other than Y to enable remote root logon.

      image

    6. Enter 'y' to remove the default test database included with MySQL. image

    7. Enter 'y' to reload the privilege tables and ensure that all changes are applied. image

Ubuntu/Debian

  1. Connect to the ECS instance remotely. For specific operations, see Log on to a Linux instance by using the Workbench tool over SSH.

  2. Run the following commands to update the system APT repository and ensure the system package list is current.

    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
    # Use the dpkg command to install the downloaded MySQL APT configuration package
    sudo dpkg -i mysql-apt-config_0.8.33-1_all.deb
    1. (Optional) If the mysql-apt-config script detects that your operating system version is not officially supported, it will display an interface that allows you to select the option closest to your system version to continue the installation.

      Note

      For more information, see the FAQ about deploying MySQL.

      image

    2. The configuration page lists all components to be installed, including MySQL Server, Client, Tool, and a series of components. Confirm that the version and components to be installed are correct, switch to OK with the arrow keys, and press Enter. image

  4. Install the MySQL server.

    Important

    During the installation, when setting the password for the root user of MySQL, you must follow the password policy: The password must contain at least one uppercase letter, one lowercase letter, one digit, and one special character, and the total length must be at least eight characters. Enter your password and press Enter to confirm. Enter it twice to complete the setup. 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
    # Automatically start on system startup
    sudo systemctl enable mysql
  5. Run the command to start the MySQL secure installation wizard.

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

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

    3. Select 2 to enforce a password policy requiring at least one uppercase letter, one lowercase letter, one digit, one special character, and a minimum length of eight characters. image

    4. Enter n to reject changing the root user password.

      Note

      If you need to change the root user password, you can enter y to make the change.

      image

    5. Enter 'y' to remove the anonymous user.

      Note

      Removing the anonymous user helps prevent unauthorized access to your MySQL server by users without valid credentials.

      image

    6. Enter y to disable remote logon for the MySQL root user.

      Note

      If you need to allow the root user remote access, press any key other than Y to enable remote root logon.

      image

    7. Enter 'y' to remove the default test database included with MySQL. image

    8. Enter 'y' to reload the privilege tables and ensure that all changes are applied. image

Add remote access MySQL user

  1. Remotely connect to the ECS instance. For detailed instructions, see how to log on to a Linux instance using the Workbench tool over SSH.

  2. When setting up the security group, MySQL defaults to using port 3306. Make sure the instance's security group inbound rule permits traffic on port 3306. Should you opt for an alternative port, modify the security group settings to reflect the actual port in use. For detailed instructions, see Add security group rules.

  3. Create a user with remote access to all MySQL databases using the following command.

    Important
    • Replace <username> with the username you created for the MySQL user in the code.

    • Replace <password> in the code with the password you created for the MySQL user. Ensure the password adheres to the policy: It should include at least one uppercase letter, one lowercase letter, one digit, and one special character, with a minimum length of eight characters.

    # You need to enter the root user password after execution
    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. Test the effectiveness of the configured username and password.

    # <username> is the username of the newly created user. You need to enter the password of your new user after execution
    sudo mysql -u<username> -p

References

For more information, see the FAQ on MySQL deployment.