Connect to PolarDB-X 1.0 database
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
Log on to the PolarDB for Xscale console.
In the top navigation bar, select the region where your instance is located.
Click the instance ID to go to the Basic Information page.
In the left-side navigation pane, click Database Management.
On the Databases page, click the database ID to go to its Basic Information page.
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:
Testing connectivity or running one-off queries: use the MySQL command line.
Managing the database visually: use a GUI client.
Connecting from application code: use a MySQL-compatible driver.
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:
| Placeholder | Description | Example |
|---|---|---|
<host> | The host from the Command Line Link Address | drdsxxxxx.drds.aliyuncs.com |
<port> | The port number | 3306 |
<username> | Your database username | doc_test |
<password> | Your database password (no space between -p and the password) | doc_test_password |
<database-name> | The name of the database to connect to | doc_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.
| Client | Notes |
|---|---|
| MySQL Workbench (recommended) | |
| SQLyog | |
| Sequel Pro | |
| Navicat for MySQL |
Connect using a driver or SDK
PolarDB-X 1.0 supports all MySQL-compatible drivers. Use the driver for your programming language:
| Language | Driver |
|---|---|
| Java | JDBC Driver for MySQL (Connector/J) |
| Python | Python Driver for MySQL (Connector/Python) |
| C++ | C++ Driver for MySQL (Connector/C++) |
| C | C Driver for MySQL (Connector/C) |
| .NET | ADO.NET Driver for MySQL (Connector/NET) |
| ODBC | ODBC Driver for MySQL (Connector/ODBC) |
| PHP | PHP Drivers for MySQL (mysqli, ext/mysqli, PDO_MYSQL, PHP_MYSQLND) |
| Perl | Perl Driver for MySQL (DBD::mysql) |
| Ruby | Ruby 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.