Updated on 2024-10-14 GMT+08:00

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

If you cannot access your DB instance through a floating IP address, bind an EIP to the DB instance and connect to it through the EIP.

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

Prerequisites

  1. You have bound an EIP to the target DB instance and configured security group rules.
    1. Bind an EIP to the target DB instance.
    2. Obtain the IP address of the ECS you use to connect to the DB instance.
    3. Configure security group rules.

      Add the IP address obtained in 1.b and the instance port to the inbound rule of the security group.

    4. Run the ping command to ping the EIP bound in 1.a to ensure that the EIP is accessible through the ECS.
  2. You have installed a database client to connect to DB instances.

    In Linux, you need to install a MariaDB client on your device. 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 Commands (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 instance name to go to the Basic Information page.
  5. In the DB Information area, check whether SSL is enabled.

    • If yes, go to 6.
    • If no, click . In the displayed dialog box, click OK to enable SSL. Then go to 6.

  6. Click next to the SSL field 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 Linux or Windows. For details, see How Can I Import the Root Certificate to a Windows or Linux OS?
  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>

    EIP of the DB instance to be connected.

    <port>

    Port of the DB instance to be connected.

    <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

    If the connection fails, ensure that all prerequisites are correctly configured and try again.

Connecting to a DB Instance Using Commands (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 instance name to go to the Basic Information page.
  5. In the DB Information area, check whether SSL is enabled.

    • If yes, click . In the displayed dialog box, click OK to disable SSL. Then go to 6.
    • If no, go to 6.

  6. Connect to the RDS DB 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>

    EIP of the DB instance to be connected.

    <port>

    Port of the DB instance to be connected.

    <userName>

    Username of the database account. The default administrator is root.

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

    Enter password:
    Figure 2 Non-SSL connection example

    If the connection fails, ensure that preparations have been correctly made in Prerequisites and try again.