This topic describes how to enable secure access from programs to an instance for which the data security protection feature is enabled over the MySQL protocol.

Prerequisites

  • The data security protection feature is enabled for the instance. For more information about how to enable the feature, see Enable the data security protection feature.
  • The certification information and proxy endpoint are prepared. You can view the previous information on the tab.

Examples

You can use commands, an SQL client, or program code to access an instance.

  • 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 AccessID that Data Management (DMS) allocates to you after the authorization. You can view your AccessID on the tab.
    • password: the AccessSecret that DMS allocates to you after the authorization. You can view your AccessSecret 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 Data security protection tab. 
    // 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 AccessID that is used to access the instance. You can view your AccessID on the Data security protection tab. 
    properties.setProperty("user", "AccessID");
    // AccessSecret: the AccessSecret that is used to access the instance. You can view your AccessSecret on the Data security protection tab. 
    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 AccessID that is used to access the instance. You can view your AccessID on the Data security protection tab. 
        # password: the AccessSecret that is used to access the instance. You can view your AccessSecret on the Data security protection tab. 
        # 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 AccessID that is used to access the instance. You can view your AccessID on the Data security protection tab.      
        user     : 'AccessID', 
        // password: the AccessSecret that is used to access the instance. You can view your AccessSecret on the Data security protection tab.              
        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 AccessID that is used to access the instance.
    • Password: the AccessSecret that is used to access the instance.
    navicat