All Products
Search
Document Center

Data Management:Access a database instance over the MySQL protocol

Last Updated:May 22, 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

Usage notes

If your database instance is managed in Security Collaboration mode, it is affected by security rules. A maximum of 100,000 rows can be returned for each query.

Set the maximum number of rows that can be returned per query.

Log on to the Data Management (DMS) console. In the top navigation bar, choose Security and Specifications > Security Rules. Find the MySQL engine type and click Edit in the Actions column. In the left-side navigation pane, choose Security and Specifications > Secure Access Proxy. Then, modify the maximum number of rows that can be returned per query.

image

Note
  • If you do not want your database instance to be affected by security rules, access your database instance by using the connection address that is provided by the database console, or contact DMS technical support to evaluate specific scenarios.

  • If your database instance is not managed in Security Collaboration mode, you cannot customize the maximum number of returned rows per query. By default, the maximum number of rows that can be returned for each query is 3000.

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

Syntax:

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

The following table describes the parameters.

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 details page of the instance.

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 details page of the instance.

user_name

The AccessKey ID that DMS allocates to you after the authorization. You can view your AccessKey ID in the Authorization Information section of the Secure Access Proxy details page of the instance.

password

The AccessKey Secret that DMS allocates to you after the authorization. You can view your AccessKey Secret in the Authorization Information section of the Secure Access Proxy details page of the instance.

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.

Sample code:

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 details page of the instance. 
// 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 Secure Access Proxy 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 Secure Access Proxy 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 Secure Access Proxy 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 Secure Access Proxy 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 Secure Access Proxy 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 Secure Access Proxy 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