ApsaraDB RDS is a stable, reliable, and scalable online database service. When you use an Elastic Compute Service (ECS) instance to build a business on the cloud, you can use an RDS instance to store business data. This topic describes how to create an ApsaraDB RDS for MySQL instance and connect a Linux ECS instance to the ApsaraDB RDS for MySQL instance.

Prerequisites

An ECS instance is created. For more information, see Create an instance by using the wizard.

In the example, an ECS instance that has the following configurations is used. You can configure the ECS instance based on your business requirements.
  • Region and zone: Hangzhou Zone I
  • Instance type: ecs.g6.large
  • Disk category: enhanced SSD (ESSD)
  • Image: Alibaba Cloud Linux 3 64-bit public image
  • Network type: Virtual Private Cloud (VPC). A public IP address is assigned to the instance.

Background information

RDS is built on top of the Apsara Distributed File System and high-performance SSDs of Alibaba Cloud. RDS supports the MySQL, SQL Server, PostgreSQL, and MariaDB database engines. It provides a portfolio of solutions for scenarios such as disaster recovery, backup, restoration, monitoring, and migration to reduce your O&M burdens. For more information, see What is ApsaraDB RDS?

Step 1: Create an RDS instance

  1. Go to the ApsaraDB RDS buy page.
  2. Complete the configurations of the RDS instance.
    In this example, an ApsaraDB RDS for MySQL instance is created. For more information about how to create an ApsaraDB RDS for MySQL instance, see Create an ApsaraDB RDS for MySQL instance. In this example, the following configurations are used. You can configure the RDS instance based on your business requirements.
    1. In the Basic Configurations step, configure the following parameters:
      • Region: Select China (Hangzhou).
        Note If you want to transmit data between the ECS instance and the RDS instance over the internal network, you must deploy the ECS instance and the RDS instance in the same region and the same VPC. Transmission over the internal network is more secure and stable and provides higher performance than transmission over the Internet.
      • Database Engine: Select MySQL 8.0.
      • Edition: Select High-availability.
      • Storage Type: Select Local SSD.
      • Zone of Primary Node: Select Hangzhou Zone I.
      • Deployment Method: Select Multi-zone Deployment.
      • Zone of Secondary Node: Select Automatically Allocated.
      • Instance Type: Select rds.mysql.t1.small.
      Use the default values for other parameters.
    2. Click Next: Instance Configuration.
    3. In the Instance Configuration step, configure the following parameters:
      • Network Type: Select VPC.
      • VPC and VSwitch of Primary Node: Select the same VPC and vSwitch as the ECS instance to which you want to connect.
      Use the default values for other parameters.
    4. Click Next: Confirm Order. Confirm the configurations and pay for the order.
      Up to 10 minutes are required to create an RDS instance. You can view and refresh the state of the RDS instance on the Instances page. If the state of the instance changes to Running, the RDS instance is created and runs normally.

Step 2: Create a database and a standard account for the database

For information about how to create a database and an account for an ApsaraDB RDS for MySQL instance, see Create databases and accounts for an ApsaraDB RDS for MySQL instance. In this example, a database named test01 is created for the RDS instance and a standard account named testuser01 is created to log on to the test01 database.

  1. Go to the Instances page.
  2. In the top navigation bar, select the China (Hangzhou) region.
  3. Find and click the ID of the RDS instance that you created.
  4. Create a database on the RDS instance.
    1. In the left-side navigation pane of the Basic Information page, click Databases and then click Create Database.
    2. In the Create Database dialog box, configure the following parameters and click Create:
      • Database Name: Specify a name for the database. Example: test01.
      • Supported Character Set: Select utf8.
      Use the default values for other parameters.
  5. Create a standard account.
    1. In the left-side navigation pane of the Basic Information page, click Accounts. On the Accounts tab, click Create Account.
    2. In the Create Account panel, configure the following parameters and click OK:
      • Database Account: Specify a name for the account. Example: testuser01.
      • Account Type: Select Standard Account.
      • Authorized Databases: After you select Standard Account, the Authorized Databases section appears. You must grant the testuser01 account read and write permissions on the test01 database. Authorized Databases
      • Password and Confirm Password: Specify a password. We recommend that you set a complex password to improve data security. Keep your password confidential.
      Use the default values for other parameters.

Step 3: Configure a whitelist for the RDS instance and obtain the internal endpoint and internal port number of the RDS instance

Before you obtain the internal endpoint and internal port number of the RDS instance, you must check whether the ECS instance and the RDS instance meet the conditions for communication over the internal network. For more information, see the "Step 1: Check whether your application can connect to the RDS instance over an internal network" section in Use a database client or the CLI to connect to an ApsaraDB RDS for MySQL instance. In this example, the ECS instance and the RDS instance reside within the same VPC and are able to communicate with each other over the internal network.

  1. Go to the Instances page.
  2. In the top navigation bar, select the China (Hangzhou) region.
  3. Find and click the ID of the RDS instance that you created.
  4. Configure a whitelist for the RDS instance.
    1. In the left-side navigation pane of the Basic Information page, click Data Security.
    2. On the Whitelist Settings tab, find the whitelist named default and click Modify on the right side.
    3. In the Edit Whitelist dialog box, remove the default 127.0.0.1 from the IP Addresses field and add <Internal IP address of the ECS instance>.
      For more information about how to view the IP addresses of an ECS instance, see View the information about instances on the Instances page.
    4. Click OK.
  5. Obtain the internal endpoint and internal port number of the RDS instance.
    1. In the left-side navigation pane of the Basic Information page, click Database Connection.
    2. On the Database Connection page, view the Internal Endpoint and Internal Port values.
      Internal Endpoint
      You must save the internal endpoint and internal port number for the subsequent connections to the database.

Step 4: Connect to the ApsaraDB RDS for MySQL database from the ECS instance

  1. Log on to the Linux ECS instance.
    For more information, see Connection methods .
  2. Optional: Install MySQL on the ECS instance.
    If MySQL is not installed on your ECS instance, install MySQL first. For example, you can run the following command to install MySQL on an ECS instance that runs Alibaba Cloud Linux or CentOS:
    yum -y install mysql
  3. Run the following command to connect to the MySQL database:
    mysql -h <Internal endpoint of the RDS instance> -P <Internal port number of the RDS instance> -u<Standard account of the RDS instance> -p
    Variables:
    • <Internal endpoint of the RDS instance>: the internal endpoint of the RDS instance. Example: rm-bp1bj****.mysql.rds.aliyuncs.com.
    • <Internal port number of the RDS instance>: the internal port number of the RDS instance. For example, the default port number of the MySQL database is 3306.
    • <Standard account of the RDS instance>: the standard account of the RDS instance. In this example, testuser01 is used.
  4. In the Enter password: command line, enter the password of testuser01.
    Note When you enter a password, no command output is returned to maximize data security. You need only to enter the correct password and then press the Enter key.
    The following figure shows that you are connected to the MySQL database. If the connection fails, see the "Common connection errors" section in Common connection errors. Log on to the MySQL database
  5. Optional: Run the following command in the MySQL client to view the details of the database:
    show databases;
    You can view the test01 database in the command output. test01

References

Getting Started