Updated on 2025-06-24 GMT+08:00

Using MySQL CLI to Connect to an Instance Through a Private Network

If your applications are deployed on an ECS that is in the same region and VPC as your RDS for MariaDB instance, you are advised to connect to the DB instance through a floating IP address using the ECS.

This section describes how to connect a Linux ECS to a DB instance with SSL enabled or disabled through a floating IP address. SSL encrypts connections to the DB instance, making in-transit data more secure.

Prerequisites

  1. You have logged in to an ECS.
    • For details on how to create and log in to an ECS, see Purchasing an ECS and Logging In to an ECS in Elastic Cloud Server Getting Started.
    • To connect to a DB instance through an ECS, you must ensure that:
      • The ECS and DB instance are in the same VPC.
      • The ECS is allowed by the security group to access the DB instance.
        • If the security group with which the DB instance is associated is the default security group, you do not need to configure security group rules.
        • If the security group with which the DB instance is associated is not the default security group, check whether the security group rules allow the ECS to connect to the DB instance.

          If the security group rules allow the access from the ECS, you can connect to the DB instance through the ECS.

          If the security group rules do not allow the access from the ECS, you need to add a security group rule, allowing the ECS to access the DB instance.

  2. You have installed a database client to connect to DB instances.

    In Linux, install a MariaDB client on a device that can access RDS for MariaDB. It is recommended that you download a MariaDB client running a version later than that of the DB instance.

Connecting to a DB Instance Using a CLI (SSL Connection)

  1. Log in to the management console.
  2. Click in the upper left corner and select a region and a project.
  3. Click in the upper left corner of the page and choose Databases > Relational Database Service.
  4. On the Instances page, click the target instance name to go to the Overview page.
  5. Under SSL, click Enable and then click OK.
  6. Click Download under SSL to download Certificate Download.zip, and extract the root certificate ca.pem and bundle ca-bundle.pem from the package.
  7. Import the root certificate ca.pem to the Windows or Linux ECS.
  8. Connect to the RDS for MariaDB instance. In Linux, for example, run the following command:

    mysql -h <host> -P <port> -u <userName> -p --ssl-ca=<caName>

    Example:

    mysql -h 172.16.0.31 -P 3306 -u root -p --ssl-ca=ca.pem

    Table 1 Parameter description

    Parameter

    Description

    <host>

    Floating IP address. To obtain this parameter value, go to the Overview page of the target instance and find Floating IP Address.

    <port>

    Database port. By default, the value is 3306. To obtain this parameter value, go to the Overview page of the target instance and find Database Port.

    <userName>

    Username of the database account used for logging in to the DB instance. The default value is root.

    <caName>

    Name of the CA certificate. The certificate should be stored in the directory where the command is executed.

  9. Enter the password of the database account if the following information is displayed:

    Enter password:
    Figure 1 Connection example

Connecting to a DB Instance Using a CLI (Non-SSL Connection)

  1. Log in to the management console.
  2. Click in the upper left corner and select a region and a project.
  3. Click in the upper left corner of the page and choose Databases > Relational Database Service.
  4. On the Instances page, click the target instance name to go to the Overview page.
  5. Under SSL, click Disable and then click OK.
  6. Connect to the RDS for MariaDB instance. In Linux, for example, run the following command:

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

    Example:

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

    Table 2 Parameter description

    Parameter

    Description

    <host>

    Floating IP address. To obtain this parameter value, go to the Overview page of the target instance and find Floating IP Address.

    <port>

    Database port. By default, the value is 3306. To obtain this parameter value, go to the Overview page of the target instance and find Database Port.

    <userName>

    Username of the database account used for logging in to the DB instance. The default value is root.

  7. Enter the password of the database account if the following information is displayed:

    Enter password:
    Figure 2 Non-SSL connection example