You can connect to PolarDB-X databases by using Data Management (DMS), the MySQL command-line client mysql, third-party clients, and third-party drivers for MySQL. This topic describes how to use mysql to connect to a PolarDB-X database.

Prerequisites

The first time you use PolarDB-X, perform the operations that are described in Get started. For example, you must create a PolarDB-X instance, create an account, and create a database.

If mysql is not installed on your server, go to the MySQL website to download and install mysql.

Use mysql to connect to a database

  1. Log on to the PolarDB-X console.
  2. In the top navigation bar, select the region where the target instance is located.
  3. On the Instance List page, click the PolarDB-X 2.0 tab.
  4. Find the target instance and click its ID.
  5. In the Connection information section, view the private and public endpoints of the instance.
    Note If you need to connect to the instance over the Internet for trial access, use the External address and Extranet Port to connect to the instance. To ensure database security, we recommend that you use the private endpoint for formal access.
  6. After you obtain the connection information in the Connection information section, run the following command to obtain the local public IP address. Then, add the IP address to a whitelist of the instance. For more information about how to configure a whitelist, see Configure whitelists.
     curl 'https://api.ipify.org'
  7. Run the following command to connect to your database:
    mysql -h<Domain name> -P<Port number> -u<Username> -p -D<Database name>
    Parameter Description Example
    -h The private or public domain name that can be used to connect to the instance. pxc-******************.public.polarx.rds.aliyuncs.com
    -P The port number of the instance.
    • If you connect to the instance over an internal network, specify the private port number of the instance.
    • If you connect to the instance over the Internet, specify the public port number of the instance.
    Note
    • Take note that the letter p in -P must be in uppercase.
    • The default port number is 3306.
    3306
    -u The username of the account that is used to connect to the instance. For more information about how to create an account, see Create an account. testuser
    -p The password of the account.
    Note
    • To ensure password security, we recommend that you do not specify the password at the end of -p. After the command is run, you are prompted to enter the password. After you enter the password, press Enter to connect to the database.
    • If you want to specify the password in the command, do not include spaces between -p and the password.
    passWord123
    -D The name of the database to which you want to connect.
    Note
    • This parameter is optional.
    • If you need to specify a database, you can omit -D and directly specify the database name.
    mysql

Use a third-party client to connect to a database

You can connect to PolarDB-X databases by using the following clients. You can go to the corresponding official websites to download the clients.

  • MySQL Workbench (Recommended)
  • SQLyog
  • Sequel Pro
  • Navicat for MySQL
Note On a third-party GUI client, you can perform basic database operations. For example, you can insert, delete, modify, and query data and perform DDL operations. PolarDB-X may not support the advanced features of the client.

Use third-party drivers for MySQL to connect to a database

You can connect to PolarDB-X databases by using the following third-party drivers for MySQL:

  • JDBC Driver for MySQL (Connector/J)
  • Python Driver for MySQL (Connector/Python)
  • C++ Driver for MySQL (Connector/C++)
  • C Driver for MySQL (Connector/C)
  • ADO.NET Driver for MySQL (Connector/NET)
  • ODBC Driver for MySQL (Connector/ODBC)
  • PHP Drivers for MySQL (mysqli, ext/mysqli, PDO_MYSQL, PHP_MYSQLND)
  • Perl Driver for MySQL (DBD::mysql)
  • Ruby Driver for MySQL (ruby-mysql)

The following code provides an example on how to use JDBC Driver for MySQL (Connector/J) to connect to a PolarDB-X database:

//JDBC
Class.forName("com.mysql.jdbc.Driver"); 
Connection conn = DriverManager.getConnection("jdbc:mysql://pxc-******************.public.polarx.rds.aliyuncs.com:3306/doc_test","doc_test","doc_test_password");
//...
conn.close();    

The following code provides an example on how to use a connection pool to connect to a PolarDB-X database from your application:

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
<property name="url" value="jdbc:mysql://pxc-******************.public.polarx.rds.aliyuncs.com:3306/doc_test" />
<property name="username" value="doc_test" />
<property name="password" value="doc_test_password" />
<property name="filters" value="stat" />
<property name="maxActive" value="100" />
<property name="initialSize" value="20" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="20" />
<property name="asyncInit" value="true" />
</bean>
Note We recommend that you use the Druid connection pool to connect to PolarDB-X databases. For more information about Druid, see Druid in GitHub.