This topic describes how to connect to an ApsaraDB RDS for MySQL instance by using a Java, Python, or C application.


The following table describes the parameters in the sample code.

Parameter Description
Host The internal or public endpoint of the RDS instance.
  • If your client is deployed on an Elastic Compute Service (ECS) instance that resides in the same region and has the same network type as the RDS instance, use the internal endpoint. For example, if the ECS and RDS instances are both in a VPC located in the China (Hangzhou) region, you can use the internal endpoint of the RDS instance to create a secure connection.
  • In other scenarios, use the public endpoint.

For more information about how to view the internal and public endpoints and port numbers of an RDS instance, see View and change the internal and public endpoints and port numbers of an ApsaraDB RDS for MySQL instance.

Port If you connect to the RDS instance over an internal network, enter the internal port number of the instance. If you connect to the RDS instance over the Internet, enter the public port number of the instance.
myDatabase The name of the database that you want to connect.
myUsername Enter the name of the account that is used to connect to the RDS instance.
myPassword The password of the account.


  • Sample code in Java:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    public class DatabaseConnection
        public static void main(String args[]) {
            String connectionUrl= "jdbc:mysql://<Host>:<Port>/<myDatabase>";    
            ResultSet resultSet;
            try (Connection connection=DriverManager.getConnection(connectionUrl,"<myUsername>","<myPassword>");  
                 Statement statement = connection.createStatement()) {
                String selectSql = "SELECT * FROM `courses`";            //Enter the SQL statement that you want to execute. 
                resultSet = statement.executeQuery(selectSql);
                while ( {
            catch (SQLException e) {
  • Sample code in Python 3:
    Note If you want to use Python 2, run the pip install pymysql==0.9.3 command to install PyMySQL first.
    import pymysql
    connection = pymysql.connect(host='<Host>',     
        with connection.cursor() as cursor:
            sql = "SELECT * FROM `courses`"        //Enter the SQL statement that you want to execute. 
            for result in cursor:
  • C sample code:
    #include <stdio.h>
    #include <mysql.h>
    #include <string.h>
    void main(void)
        MYSQL *t_mysql;
        MYSQL_RES       *res = NULL;
        MYSQL_ROW       row;
        char            *query_str = NULL;
        int             rc, i, fields;
        int             rows;
        char select[] = "select * from courses";    //Enter the SQL statement that you want to execute. 
        t_mysql = mysql_init(NULL);
        if(NULL == t_mysql){
            printf("init failed\n");
        if(NULL == mysql_real_connect(t_mysql, <Host>, <myUsername>, <myPassword>, <myDatabase>,
                <Port>, NULL, 0)){
            printf("connect failed\n");
        if(mysql_real_query(t_mysql, select, strlen(select)) != 0){
            printf("select failed\n");
        res = mysql_store_result(t_mysql);
        if (NULL == res) {
             printf("mysql_restore_result(): %s\n", mysql_error(t_mysql));
             return -1;
        fields = mysql_num_fields(res);
        while ((row = mysql_fetch_row(res))) {
            for (i = 0; i < fields; i++) {
                printf("%s\t", row[i]);


If the connection fails, we recommend that you troubleshoot the failure based on the returned error information. For more information, see Resolve the issue that you cannot connect to an RDS instance.