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

  • The secure access proxy feature is enabled for the database instance.
    Note To check whether the secure access proxy feature is enabled for a database instance, perform the following steps: Log on to the Data Management (DMS) console. In the top navigation bar, click Security and Specifications. In the left-side navigation pane, click Secure Access Proxy. On the Created tab, view database instances for which the secure access proxy feature is enabled. For more information about how to enable this feature for a database instance, see Enable the data security protection feature.
  • You are authorized to access the database instance by using proxy endpoints.
    Note On the Secure Access Proxy/Details page of a database instance, check whether you are an authorized user of the database instance. If you are not an authorized user, apply for the permissions to access the database instance by using proxy endpoints. For more information, see Apply for the permissions to access a database instance by using proxy endpoints.

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 syntax contains the following parameters:

    • host: the domain name of the instance. You can view the domain name in the public or internal proxy endpoint that is used to access the instance over the MySQL protocol on the tab.
    • port: the port number of the instance. Example: 3306. You can view the port number in the public or internal proxy endpoint that is used to access the instance over the MySQL protocol on the tab.
    • user_name: the AccessKey ID that DMS allocates to you after the authorization. You can view your AccessKey ID on the tab.
    • password: the AccessKey secret that DMS allocates to you after the authorization. You can view your AccessKey secret on the tab.
    • database: the name of the database that you want to access in the instance.
    • 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

    // dpxxxx-xxxxxxxx.proxy.dms.aliyuncs.com:3306: the domain name and port number of the instance. You can view the domain name and port number in the proxy endpoint that is used to access the instance over the MySQL protocol on the Secure Access Proxy page. 
    // schema: the name of the database that you want to access in the instance. 
    String url = "jdbc:mysql://dpxxxx-xxxxxxxx.proxy.dms.aliyuncs.com:3306/schema";
    Properties properties = new Properties();
    // AccessID: the AccessKey ID that is 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 that is 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 of the instance. 
        # user: the AccessKey ID that is 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. 
        # password: the AccessKey secret that is 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 that you want to access in the instance. 
        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 that is 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.      
        user     : 'AccessID', 
        // password: the AccessKey secret that is 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 of the instance.       
        port     : '3306',  
        // database: the name of the database that you want to access in the instance.                  
        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 of the instance.
    • User Name: the AccessKey ID that is used to access the instance.
    • Password: the AccessKey secret that is used to access the instance.
    navicat