ApsaraDB RDS is a stable, reliable, and scalable online relational database service. When you use an Elastic Compute Service (ECS) instance to build a business in 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 information about how to create an ECS instance, see Create an instance on the Custom Launch tab.
In this topic, 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: Enterprise SSD (ESSD)
Image: Alibaba Cloud Linux 3 64-bit public image
Network type: Virtual Private Cloud (VPC). A public IP address is automatically assigned to the instance.
Procedure
Step 1: Create an RDS instance
Go to the ApsaraDB RDS instance buy page.
Complete the configurations of the RDS instance.
In this example, an ApsaraDB RDS for MySQL instance is created. For more information, 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.
In the Basic Resources step, configure the following parameters:
Region: Select China (Hangzhou).
NoteIf you want to transmit data between the ECS instance and the RDS instance over the internal network, deploy the ECS instance and the RDS instance in the same region and the same VPC. Compared with the Internet, the internal network is more secure, stable, and efficient for data transmission.
Database Engine: Select MySQL 8.0.
Edition: Select High-availability Edition.
Storage Type: Select PL1 ESSD.
Zone of Primary Node: Select Hangzhou Zone J.
Deployment Method: Select Multi-zone Deployment.
Zone of Secondary Node: Select Hangzhou Zone K.
Instance Type: Select mysql.n2m.medium.2c.
Use the default values for other parameters.
Click Next: Instance Configuration.
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.
Click Next: Confirm Order. Confirm the configurations and complete the payment.
Up to 10 minutes are required to create an RDS instance. You can view and refresh the status of the RDS instance on the Instances page. If the status of the instance changes to Running, the RDS instance is created and runs as expected.
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 accounts and databases. In this example, a database named test01
and a standard account named testuser01
are created. The standard account is used to log on to the test01
database.
Go to the Instances page.
In the top navigation bar, select the China (Hangzhou) region.
Find and click the ID of the RDS instance that you created.
Create a database on the RDS instance.
In the left-side navigation pane of the Basic Information page, click Databases and then click Create Database.
In the Create Database panel, 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.
Create a standard account.
In the left-side navigation pane, click Accounts. On the User Account tab, click Create Account.
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.
Authorize Database: After you select Standard Account, the Authorize Database section appears. You must grant the read and write permissions on the
test01
database to thetestuser01
account.New Password and Confirm Password: Specify a password. To improve data security, we recommend that you specify a complex password. 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, check whether the ECS instance and the RDS instance meet the conditions for communication over the internal network. For more information, see Use a client or the CLI to connect to an ApsaraDB RDS for MySQL instance. In this example, the ECS instance and the RDS instance belong to the same VPC and can communicate with each other over the internal network.
Go to the Instances page.
In the top navigation bar, select the China (Hangzhou) region.
Find and click the ID of the RDS instance that you created.
Configure a whitelist for the RDS instance
In the left-side navigation pane of the Basic Information page, click Whitelist and SecGroup.
On the Whitelist Settings tab, find the whitelist named default and click Modify on the right side.
In the Edit Whitelist dialog box, remove the default IP address
127.0.0.1
from the IP Addresses field and add <Internal IP address of the ECS instance>.For information about how to view the IP address of an ECS instance, see the View the detailed information about a single instance section of the "View instance information" topic.
Click OK.
Obtain the internal endpoint and internal port number of the RDS instance.
In the left-side navigation pane of the Basic Information page, click Database Connection.
On the Database Connection page, view the values of the Internal Endpoint and Internal Port parameters.
Save the internal endpoint and internal port number for subsequent connections to the database.
Step 4: Connect to the ApsaraDB RDS for MySQL database from the ECS instance
Log on to the Linux ECS instance.
For more information, see Connection method overview.
(Optional) Install MySQL on the ECS instance.
If MySQL is not installed on your ECS instance, you must install MySQL. 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
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.
In the Enter password: command line, enter the password of the
testuser01
account.NoteFor data security purposes, no output is returned when you enter a password. 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 of the "Use a client or the CLI to connect to an ApsaraDB RDS for MySQL instance" topic.
(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.
References
For information about how to use ApsaraDB RDS, see User Guide.
You can create other types of ApsaraDB RDS instances. For more information, see the following topics: