Connect to PolarDB-X 1.0 database

Updated at:
Copy as MD

PolarDB-X 1.0 is MySQL-compatible, so you can connect using any MySQL client, driver, or connection pool that follows the MySQL protocol.

Prerequisites

Before you begin, ensure that you have:

  • A PolarDB-X 1.0 instance with at least one database created

  • Network access from your client to the instance

Get the connection string

  1. Log on to the PolarDB for Xscale console.

  2. In the top navigation bar, select the region where your instance is located.

  3. Click the instance ID to go to the Basic Information page.

  4. In the left-side navigation pane, click Database Management.

  5. On the Databases page, click the database ID to go to its Basic Information page.

  6. In the VPC Address area, find the Command Line Link Address. This is your database connection string, containing the host, port, username, password, and database name.

Choose a connection method

Pick the method that matches your use case:

Connect using the MySQL command line

If MySQL is installed on your Elastic Compute Service (ECS) instance, run the following command:

mysql -h <host> -P <port> -u <username> -p<password> -D <database-name>

Replace the placeholders with your actual connection details:

PlaceholderDescriptionExample
<host>The host from the Command Line Link Addressdrdsxxxxx.drds.aliyuncs.com
<port>The port number3306
<username>Your database usernamedoc_test
<password>Your database password (no space between -p and the password)doc_test_password
<database-name>The name of the database to connect todoc_test

Connect using a GUI client

The following graphical user interface (GUI) clients support PolarDB-X 1.0. Download the client from its official website, then use the host, port, username, and password from your connection string to configure the connection.

ClientNotes
MySQL Workbench (recommended)
SQLyog
Sequel Pro
Navicat for MySQL
Note GUI clients support basic operations including data queries, insertions, updates, deletions, and DDL operations. PolarDB-X 1.0 may not support the advanced features of third-party GUI clients.

Connect using a driver or SDK

PolarDB-X 1.0 supports all MySQL-compatible drivers. Use the driver for your programming language:

LanguageDriver
JavaJDBC Driver for MySQL (Connector/J)
PythonPython Driver for MySQL (Connector/Python)
C++C++ Driver for MySQL (Connector/C++)
CC Driver for MySQL (Connector/C)
.NETADO.NET Driver for MySQL (Connector/NET)
ODBCODBC Driver for MySQL (Connector/ODBC)
PHPPHP Drivers for MySQL (mysqli, ext/mysqli, PDO_MYSQL, PHP_MYSQLND)
PerlPerl Driver for MySQL (DBD::mysql)
RubyRuby Driver for MySQL (ruby-mysql)

JDBC example

The following example connects to PolarDB-X 1.0 using the JDBC driver:

// Load the MySQL JDBC driver
Class.forName("com.mysql.jdbc.Driver");

// Connect using your database credentials
Connection conn = DriverManager.getConnection(
    "jdbc:mysql://<host>:<port>/<database-name>",
    "<username>",
    "<password>"
);

// ... perform database operations ...

conn.close();

Connection pool example (Druid)

For production applications, use the Druid connection pool. The following Spring Bean configuration connects to PolarDB-X 1.0 using Druid:

<bean id="dataSource"
      class="com.alibaba.druid.pool.DruidDataSource"
      init-method="init"
      destroy-method="close">
    <property name="url"      value="jdbc:mysql://<host>:<port>/<database-name>" />
    <property name="username" value="<username>" />
    <property name="password" value="<password>" />

    <!-- Connection pool sizing -->
    <property name="initialSize" value="20" />    <!-- Connections created on startup -->
    <property name="maxActive"   value="100" />   <!-- Maximum concurrent connections -->
    <property name="minIdle"     value="1" />     <!-- Minimum idle connections kept alive -->

    <!-- Timeout settings (milliseconds) -->
    <property name="maxWait" value="60000" />     <!-- Max wait time for a connection from the pool -->
    <property name="timeBetweenEvictionRunsMillis" value="60000" />  <!-- How often idle connections are checked -->
    <property name="minEvictableIdleTimeMillis"    value="300000" /> <!-- Minimum idle time before a connection is evicted -->

    <!-- Connection validation -->
    <property name="testWhileIdle"  value="true" />
    <property name="testOnBorrow"   value="false" />
    <property name="testOnReturn"   value="false" />

    <!-- Prepared statement caching -->
    <property name="poolPreparedStatements"    value="true" />
    <property name="maxOpenPreparedStatements" value="20" />

    <property name="asyncInit" value="true" />
    <property name="filters"   value="stat" />
</bean>

For more information about Druid, see the Druid GitHub repository.