Updated on 2024-03-19 GMT+08:00

Connecting to a DB Instance from a Linux ECS

You can connect to your DB instance using a Linux ECS installed with a MySQL client over a private network.

This section describes how to connect to a DB instance with SSL disabled. To connect to a DB instance with SSL enabled, see Using MySQL CLI to Connect to an Instance Through a Private Network.

Step 1: Buy an ECS

  1. Log in to the management console and check whether there is an ECS available.
    Figure 1 ECS
  2. Buy an ECS and select Linux (for example, CentOS) as its OS.

    To download a MySQL client to the ECS, bind an EIP to the ECS. The ECS must be in the same region, VPC, and security group as the RDS for MySQL DB instance for mutual communications.

    For details about how to purchase a Linux ECS, see "Purchasing an ECS" in Elastic Cloud Server Getting Started.

  3. On the ECS Information page, view the region and VPC of the ECS.
    Figure 2 ECS information

  4. On the Basic Information page of the RDS for MySQL instance, view the region and VPC of the DB instance.
    Figure 3 DB instance information

  5. Check whether the ECS and RDS for MySQL instance are in the same region and VPC.
    • If yes, go to Step 2: Test Connectivity and Install a MySQL Client.
    • If they are not in the same region, purchase another ECS or DB instance. The ECS and DB instance in different regions cannot communicate with each other. To reduce network latency, deploy your DB instance in the region nearest to your workloads.
    • If the ECS and DB instance are in different VPCs, change the VPC of the ECS to that of the DB instance. For details, see Changing a VPC.

Step 2: Test Connectivity and Install a MySQL Client

  1. Log in to the ECS. For details, see Login Using VNC in the Elastic Cloud Server User Guide.
  2. On the Instances page, click the DB instance name.
  3. Choose Connectivity & Security from the navigation pane. In the Connection Information area, obtain the floating IP address and database port of the DB instance.
    Figure 4 Connection information

  4. On the ECS, check whether the floating IP address and database port of the DB instance can be connected.

    telnet 192.168.6.144 3306

    • If yes, network connectivity is available.
    • If no, check the security group rules.
      • If in the security group of the ECS, there is no outbound rule with Destination set to 0.0.0.0/0 and Protocol & Port set to All, add an outbound rule for the floating IP address and port of the DB instance.
        Figure 5 ECS security group
      • If in the security group of the DB instance, there is no inbound rule allowing the access from the private IP address and port of the ECS, add an inbound rule for the private IP address and port of the ECS. For details, see Configuring Security Group Rules.
  5. Download the MySQL client installation package for Linux to the ECS. The package mysql-community-client-5.7.38-1.el6.x86_64.rpm is used as an example.

    A MySQL client running a version later than that of the DB instance is recommended.

    wget https://dev.mysql.com/get/mysql-community-client-5.7.38-1.el6.x86_64.rpm

  6. Install the MySQL client.

    rpm -ivh --nodeps mysql-community-client-5.7.38-1.el6.x86_64.rpm

    • If any conflicts occur during the installation, add the replacefiles parameter to the command and install the client again.

      rpm -ivh --replacefiles mysql-community-client-5.7.38-1.el6.x86_64.rpm

    • If a message is displayed prompting you to install a dependency package during the installation, add the nodeps parameter to the command and install the client again.

      rpm -ivh --nodeps mysql-community-client-5.7.38-1.el6.x86_64.rpm

Step 3: Connect to the DB Instance Using Commands (Non-SSL Connection)

  1. Run the following command on the ECS to connect to the DB instance:

    mysql -h <host> -P <port> -u <userName> -p

    Example:

    mysql -h 192.168.6.144 -P 3306 -u root -p

    Table 1 Parameter description

    Parameter

    Description

    <host>

    Floating IP address obtained in 3.

    <port>

    Database port obtained in 3. The default value is 3306.

    <userName>

    Administrator account root.

  2. Enter the password of the database account if the following information is displayed:
    Enter password:
    Figure 6 Connection successful

Follow-up Operations

After logging in to the DB instance, you can create or migrate databases.