All Products
Search
Document Center

Data Management:Access a database instance over the MySQL protocol

Last Updated:Apr 18, 2024

This topic describes how to access a database instance over the MySQL protocol after the secure access proxy feature is enabled for the database instance.

Prerequisites

Limits

  • The idle timeout period configured for the MySQL client cannot exceed 900 seconds.

  • If you use the database connection pool feature, the interval between two consecutive connection pool failure detections cannot exceed 900 seconds.

Note

We recommend that you use the database connection pool feature and set the interval to 750 seconds.

Examples

You can use commands, an SQL client, or program code to access a database instance for which the secure access proxy feature is enabled.

Use MySQL commands

The MySQL commands must use the following syntax:

mysql -h<host> -P<port> -u<user_name> -p<password> <database> -e '<sql_statements>'

The following table describes the parameters in the syntax.

Parameter

Description

host

The domain name of the instance. You can view the domain name in the public or internal proxy endpoint used to access the instance over the MySQL protocol on the Secure Access Proxy tab.

port

The port number of the instance. Example: 3306. You can view the port number in the public or internal proxy endpoint used to access the instance over the MySQL protocol on the Secure Access Proxy tab.

user_name

The AccessKey ID that Data Management (DMS) allocates to you after the authorization. You can view your AccessKey ID on the Secure Access Proxy tab.

password

The AccessKey Secret that DMS allocates to you after the authorization. You can view your AccessKey Secret on the Secure Access Proxy tab.

database

The name of the database instance that you want to access.

sql_statements

The SQL statement that you want to execute. Example: SHOW DATABASES.

Example:

mysql -hdpxxxx-xxxxxxxx.proxy.dms.aliyuncs.com -P3306 -uAccessID -pAccessSecret Schema -e 'SHOW DATABASES'

Use program code

Note

In this example, Python 2 is used.

// dpxxxx-xxxxxxxx.proxy.dms.aliyuncs.com:3306: the domain name and port number used to connect to the instance. You can view the domain name and port number in the proxy endpoint used to access the instance over the MySQL protocol on the Secure Access Proxy tab. 
// schema: the name of the database instance that you want to access. 
String url = "jdbc:mysql://dpxxxx-xxxxxxxx.proxy.dms.aliyuncs.com:3306/schema";
Properties properties = new Properties();
// AccessID: the AccessKey ID used to access the instance. You can view your AccessKey ID in the Authorization Information section on the Details page of the instance. 
properties.setProperty("user", "AccessID");
// AccessSecret: the AccessKey secret used to access the instance. You can view your AccessKey secret in the Authorization Information section on the Details page of the instance. 
properties.setProperty("password", "AccessSecret");
try (Connection connection = DriverManager.getConnection(url, properties)) {
    try (Statement statement = connection.createStatement()) {
        // Use the execute() method to execute an SQL statement. In this example, the SHOW DATABASES statement is executed. You can also execute other SQL statements. 
        statement.execute("SHOW DATABASES");
        ResultSet resultSet = statement.getResultSet();
        while (resultSet.next()) {
            System.out.println(resultSet.getString(1));
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}
import pymysql

try:
    # host: the domain name of the instance. 
    # port: the port number used to connect to the instance. 
    # user: the AccessKey ID used to access the instance. You can view your AccessKey secret in the Authorization Information section on the Details page of the instance. 
    # password: the AccessKey secret used to access the instance. You can view your AccessKey secret in the Authorization Information section on the Details page of the instance. 
    # database: the name of the database instance that you want to access. 
    conn = pymysql.connect(host='dpxxxx-xxxxxxxx.proxy.dms.aliyuncs.com', port=3306, user='AccessID', password="AccessSecret",database ='schema')  
    cur = conn.cursor(pymysql.cursors.DictCursor)
    # Use the execute() method to execute an SQL statement. In this example, the SHOW DATABASES statement is executed. You can also execute other SQL statements. 
    cur.execute('SHOW DATABASES')
    rs = cur.fetchall()
    print rs
finally:
    cur.close()
    conn.close()
var mysql  = require('mysql');  
 
var connection = mysql.createConnection({
    // host: the domain name of the instance.  
    host     : 'dpxxxx-xxxxxxxx.proxy.dms.aliyuncs.com',  
    // user: the AccessKey ID used to access the instance. You can view your AccessKey secret in the Authorization Information section on the Details page of the instance.      
    user     : 'AccessID', 
    // password: the AccessKey secret used to access the instance. You can view your AccessKey secret in the Authorization Information section on the Details page of the instance.              
    password : 'AccessSecret', 
    // port: the port number used to connect to the instance.       
    port     : '3306',  
    // database: the name of the database instance that you want to access.                  
    database : 'schema' 
}); 
 
connection.connect();

// Use the execute() method to execute an SQL statement. In this example, the SHOW DATABASES statement is executed. You can also execute other SQL statements.  
connection.query('SHOW DATABASES', function(err, result) {
    console.log(result);
});
 
connection.end();

Use an SQL client

In this example, the Navicat client is used. Set the following parameters:

  • Host: the domain name of the instance.

  • Port: the port number used to connect to the instance.

  • User Name: the AccessKey ID used to access the instance.

  • Password: the AccessKey secret used to access the instance.

navicat