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

Parameter description

The following table describes the parameters in the sample code.

Parameter Description
Host The internal or public endpoint of the RDS instance.
  • If the application runs 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 both reside in virtual private clouds (VPCs) of the China (Hangzhou) region, you can use the internal endpoint to establish a secure and efficient connection.
  • In the 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 The port number of the RDS instance. If you want to connect to the RDS instance over an internal network, enter the internal port number of the RDS instance. If you want to connect to the RDS instance over the Internet, enter the public port number of the RDS instance.
myDatabase The name of the destination database on the RDS instance.
myUsername The username of the account that is used to connect to the RDS instance.
myPassword The password of the preceding account.

Sample code

  • Java sample code
    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 (resultSet.next()) {
                    System.out.println(resultSet.getString("name"));
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
  • Python sample code
    import pymysql
    
    connection = pymysql.connect(host='<Host>',     
                           port=<Port>,
                           user='<myUsername>',
                           passwd='<myPassword>',
                           db='<myDatabase>')
    
    try:
        with connection.cursor() as cursor:
            sql = "SELECT * FROM `courses`"        //Enter the SQL statement that you want to execute.
            cursor.execute(sql)
            for result in cursor:
                 print(result)
    finally:
        connection.close()
  • 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]);
            }
            printf("\n");
        }
        mysql_close(t_mysql);
    
    }
                        

Troubleshooting

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.