All Products
Search
Document Center

Elastic Compute Service:Deploy MySQL on a Linux instance

Last Updated:Apr 01, 2024

MySQL is an open source relational database management system (RDBMS). The MySQL database system uses SQL for database management. MySQL is suitable for data storage and management in scenarios such as web application development, enterprise application development, data analysis and reporting, and software development and testing. This topic describes how to deploy MySQL on a Linux instance.

Procedure

If you want to deploy MySQL on an existing Elastic Compute Service (ECS) instance, make sure that the instance meets the following requirements:

  • The ECS instance is associated with an auto-assigned public IP address or an elastic IP address (EIP).

  • The ECS instance runs CentOS 7.x, Alibaba Cloud Linux 2, or Alibaba Cloud Linux 3.

  • An inbound rule is added to the security groups to which the ECS instance belongs to open ports 22, 80, 443, and 3306. For more information, see Add a security group rule.

Step 1: Install MySQL

  1. Connect to an ECS instance.

    For more information, see Connect to a Linux instance by using a password or key.

  2. Run the following command to update the YUM repository:

    sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
  3. Optional. If the ECS instance runs Alibaba Cloud Linux 3, run the following command to install the packages required for MySQL:

    sudo rpm -Uvh https://mirrors.aliyun.com/alinux/3/updates/x86_64/Packages/compat-openssl10-1.0.2o-4.0.1.al8.x86_64.rpm

  4. Run the following command to install MySQL:

    sudo yum -y install mysql-community-server --enablerepo=mysql80-community --nogpgcheck
  5. Run the following command to check the version of MySQL:

    mysql -V

    The following command output indicates that MySQL is installed:

    mysql Ver 8.0.33 for Linux on x86_64 (MySQL Community Server - GPL)

Step 2: Configure MySQL

  1. Run the following commands to enable MySQL to run on system startup:

    sudo systemctl start mysqld
    sudo systemctl enable mysqld
  2. Run the following command to obtain and record the initial password of the root user:

    sudo grep 'temporary password' /var/log/mysqld.log

    The following command output is returned:

    2022-02-14T09:27:18.470008Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: r_V&f2wyu_vI
    Note

    r_V&f2wyu_vI at the end of the command output is the initial password, which is required to configure the security settings for MySQL.

  3. Run the following command to configure the security settings of MySQL:

    sudo mysql_secure_installation
    1. Reset the password of the root user based on the command prompts.

      Note

      For data security purposes, no output is returned when you enter a password. You need only to enter the correct password and then press the Enter key.

      Enter password for user root: # Enter the initial password of the root user that you obtained.
      
      The existing password for the user account root has expired. Please set a new password.
      
      New password: # Enter a new password.
      
      Re-enter new password: # Enter the new password again.
      The 'validate_password' component is installed on the server.
      The subsequent steps will run with the existing configuration
      of the component.
      Using existing password for root.
      Change the password for root ? (Press y|Y for Yes, any other key for No) : Y # Enter Y to update the password. You can also enter N to skip the password update. 
      
      New password: # Enter a new password.
      
      Re-enter new password: # Enter the new password again.
      
      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 # Enter Y to use the new password.
    2. Delete the anonymous user.

      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 # Enter Y to delete the default anonymous user. 
      Success.
    3. Deny remote access by the root user.

      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 # Enter Y to deny remote access by the root user. 
      Success.
    4. Delete the test database and the access permissions on the database.

      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 # Enter Y to delete the test database and the access permissions on the database. 
       - Dropping test database...
      Success.
      
       - Removing privileges on test database...
      Success.
    5. Reload privilege tables.

      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 # Enter Y to reload privilege tables. 
      Success.
      
      All done!

    For information about the security settings of MySQL, see MySQL documentation.

Step 3: Connect to MySQL

We recommend that you use a non-root user to connect to MySQL. In this example, an account is created to connect to MySQL.

  1. Connect to an ECS instance.

    For information about the connection methods, see Connection method overview.

  2. Run the following command and enter the password for the root user to log on to MySQL:

    sudo mysql -uroot -p
  3. Run the following commands in sequence to create an account and allow remote access to MySQL by using the account.

    In this example, the dmsTest username and the Ecs@123**** password are used.

    Important

    When you create an account, replace the Ecs@123**** password with a valid password and keep the password confidential. The password must be 8 to 30 characters in length and must contain uppercase letters, lowercase letters, digits, and special characters. The following special characters are supported:

    ( ) ` ~ ! @ # $ % ^ & * - + = | { } [ ] : ; ' < > , . ? /

    # Create an account named dmsTest and grant the remote connection permissions to the account. 
    create user 'dmsTest'@'%' identified by 'Ecs@123****'; 
    # Grant all permissions on MySQL to the dmsTest account. 
    grant all privileges on *.* to 'dmsTest'@'%'; 
    # Refresh the permissions. 
    flush privileges; 
  4. Run the following command to exit MySQL:

    exit
  5. Connect to MySQL by using the dmsTest user.

    • We recommend that you use Alibaba Cloud Data Management (DMS) to access MySQL databases. For more information, see Register an Alibaba Cloud database instance.

    • You can use a MySQL client to log on to MySQL to perform tests. The MySQL client can be MySQL Workbench or Navicat.

References

  • You can use ApsaraDB RDS to manage your databases in an efficient manner and build a managed database service that provides high availability, reliability, security, and scalability. ApsaraDB RDS is a stable, reliable, and scalable database service provided by Alibaba Cloud. ApsaraDB RDS supports the MySQL, SQL Server, PostgreSQL, and MariaDB database engines and provides solutions for scenarios such as disaster recovery, backup, restoration, and migration. For more information, see What is ApsaraDB RDS?

  • You can use Alibaba Cloud Data Transmission Service (DTS) to smoothly migrate data from self-managed databases to an Alibaba Cloud database. For more information, see Overview of data migration scenarios and Migrate data from a self-managed MySQL database to an ApsaraDB RDS for MySQL instance.

  • Database Backup (DBS) is a cost-efficient and highly reliable cloud-native database backup platform that is provided by Alibaba Cloud. DBS helps you resolve issues such as time-consuming and cost-consuming backup. For more information, see What is DBS?