Updated on 2024-12-09 GMT+08:00

Deploying MySQL

Introduction

MySQL is an open-source relational database management system. Like other relational databases, MySQL stores data in tables consisting of rows and columns. Users can define, manipulate, control, and query data using a structured query language (usually called SQL). This tutorial describes how you can deploy MySQL in Huawei Cloud EulerOS (HCE) 2.0.

Preparations

  • Prepare an ECS and assign a public IP address or elastic IP address (EIP) to the ECS.
  • Ensure that inbound security group rules allow traffic to flow to the ECS over ports 22 and 3306.

Procedure

  1. Install the MySQL server and client.

    1. Run the following command to install the MySQL server and client:
      dnf install mysql-server mysql-common mysql -y
    2. Run the following command to check the MySQL version:
      mysql -V

      If information similar to the following is displayed, the MySQL server and client are successfully installed:

      mysql  Ver 8.0.37 for Linux on x86_64 (Source distribution)

  2. Configure MySQL.

    1. Run the following command to start MySQL:
      systemctl start mysqld
    2. Run the following command to check the MySQL status:
      systemctl status mysqld

      If active (running) is displayed, MySQL is started.

      To set mysqld to automatically enable upon system boot, run the following command:

      systemctl enable mysqld

    3. Run the following command to perform security configuration for MySQL:
      mysql_secure_installation

      Customize options based on personal requirements and prompt information.

      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: Y // Choose whether to set the password component.
      
      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: 2 // Select the password security policy.
      Please set the password for root here.
      
      New password: 
      
      Re-enter new password: 
      
      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
      By default, a MySQL installation has an anonymous user,
      allowing anyone to log in to 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 // Choose whether to remove anonymous users.
      Success.
      
      
      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 // Choose whether to disallow remote login of the root user.
      Success.
      
      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 // Choose whether to remove the test database.
       - Dropping test database...
      Success.
      
       - Removing privileges on test database...
      Success.
      
      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 // Choose whether to reload privilege tables.
      Success.
      
      All done!

  3. Remote connect to the database.

    1. Run the following command on the MySQL server to connect to the database:
      mysql -uroot -p

      Enter the password of the root user.

    2. Enter the following statement to create a test account and password:
      create user 'test'@'%' identified by 'test****';

      test indicates the account name, and test**** indicates the password.

    3. Execute the following statement to grant all database permissions to the test account:
      grant all privileges on *.* to 'test'@'%';
    4. Execute the following statement to update the permissions:
      flush privileges;
    5. Execute the following command to exit the database:
      exit
    6. Run the following command on the MySQL client to remotely connect to the database:
      mysql -h <IP address of the MySQL client> -utest -p

      Alternatively, use the Navicat or Visual Studio Code plug-in for remote connections.

The password and permission configurations are used only for tests. Exercise caution when using them in the service environment.