Connect to a database instance
You can connect to a PolarDB-X instance by using Data Management (DMS), the MySQL CLI, third-party clients, and application code that is compatible with the official MySQL protocol.
Before you begin
Before you connect to a PolarDB-X instance, complete the following tasks:
Connect to the database instance
The following sections demonstrate several ways to connect to your database instance.
DMS
Data Management (DMS) is an all-in-one graphical tool from Alibaba Cloud for managing your PolarDB-X instance. It provides comprehensive features, including data and schema management, security auditing, and performance optimization, allowing you to manage your instance without other tools.
-
Go to the PolarDB for Distributed console. In the Instances, click the ID of your target instance. In the upper-right corner of the page, click Log On to Database.

-
In the dialog box that appears, enter the Database Account and Database Password for your PolarDB-X instance, and then click Search.
Note-
The first time you log on, the default control mode is Flexible Management. You can change this mode later by editing the instance. For more information, see Edit instance information and Control modes.
-
After you configure the login parameters, you can click Test Connectivity in the lower-left corner. If the connection test fails, check the instance information that you entered according to the error message. For example, make sure that the account or password is correct.
-
DMS attempts to add its server IP addresses to the instance whitelist automatically. If this process fails, you must add the IP addresses manually.
-
-
After you log on, you can find the PolarDB-X instance in the Instances Connected list in the left-side navigation pane, ready for management.

Client
PolarDB-X supports connections from the following third-party clients. You can download the clients from their official websites.
-
MySQL Workbench (Recommended)
-
SQLyog
-
Sequel Pro
-
Navicat for MySQL
Third-party GUI clients can perform basic database operations, including CRUD and DDL operations. However, advanced client-specific features may not be supported by PolarDB-X.
The following steps use MySQL Workbench 8.0.29 as an example. The steps for other clients are similar.
-
Install MySQL Workbench. For the official download link, see the MySQL Workbench download page.
-
Open MySQL Workbench and choose .
-
Enter the connection information and click OK.

Parameter
Description
Example
Hostname
The database endpoint.
pxc-xxx.polarx.rds.aliyuncs.com
Port
The port number that corresponds to the database endpoint.
NoteThe default port is 3306.
3306
Username
The database account.
polardb_x_user
Password
The password for the database account.
Pass***233
MySQL CLI
If a MySQL client is installed on your server, you can use the command line to connect to your PolarDB-X instance.
Syntax:
mysql -h<endpoint> -P<port> -u<username> -p<password> -D<database_name>
Example:
mysql -hpxc-xxx.polarx.rds.aliyuncs.com -P3306 -upolardb_mysql_user -pPass***233 -Dtest_db
|
Parameter |
Description |
Example |
|
-h |
The database endpoint. |
pxc-xxx.polarx.rds.aliyuncs.com |
|
-P |
The port number that corresponds to the database endpoint. Note
|
3306 |
|
-u |
The database account. |
polardb_x_user |
|
-p |
The password for the database account. Note
This parameter is required.
|
Pass***233 |
|
-D |
The name of the database to which you want to connect. Note
This parameter is optional. |
test_db |
Application
Connecting to a PolarDB-X instance is the same as connecting to other MySQL databases. You only need to replace the database endpoint, port, username, and password. The following examples show how to access a PolarDB database from an application by using different programming languages:
Java
This example shows how to use the MySQL JDBC driver in a Maven project to connect to a PolarDB-X instance.
-
Add the MySQL JDBC driver dependency to the
pom.xmlfile.<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> -
Connect to the instance. Replace the
<HOST>, port number,<USER>,<PASSWORD>,<DATABASE>,<YOUR_TABLE_NAME>, and<YOUR_TABLE_COLUMN_NAME>parameters with your actual values.import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class DatabaseConnection { public DatabaseConnection() { } public static void main(String[] args) { // PolarDB-X instance endpoint, port, and database name String url = "jdbc:mysql://<HOST>:3306/<DATABASE>?useSSL=false&serverTimezone=UTC"; // Database account String user = "<USER>"; // Password for the database account String password = "<PASSWORD>"; try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement(); // Table to query ResultSet rs = stmt.executeQuery("SELECT * FROM `<YOUR_TABLE_NAME>`"); while(rs.next()) { // Column to read System.out.println(rs.getString("<YOUR_TABLE_COLUMN_NAME>")); } rs.close(); stmt.close(); conn.close(); } catch (Exception var7) { var7.printStackTrace(); } } }
Python
This example shows how to use the PyMySQL library in Python 3 to connect to a PolarDB-X instance.
-
Install the PyMySQL library. If you have not installed it, run the following command:
pip3 install PyMySQL -
Connect to the instance. Replace the
<HOST>, port number,<USER>,<PASSWORD>,<DATABASE>, and<YOUR_TABLE_NAME>parameters with your actual values.import pymysql # Connection settings host = '<HOST>' # PolarDB-X instance endpoint port = 3306 # Default port is 3306. user = '<USER>' # Database account password = '<PASSWORD>' # Password for the database account database = '<DATABASE>' # Target database try: # Create a connection. connection = pymysql.connect( host=host, port=port, user=user, passwd=password, db=database ) # Create a cursor. with connection.cursor() as cursor: # Run a query. sql = "SELECT * FROM `<YOUR_TABLE_NAME>`" # Table to query cursor.execute(sql) # Read results. results = cursor.fetchall() for row in results: print(row) finally: # Close the connection. if 'connection' in locals() and connection.open: connection.close()
Go
This example shows how to use the database/sql package and the go-sql-driver/mysql driver in Go 1.23.0 to connect to a PolarDB-X instance.
-
Install the
go-sql-driver/mysqldriver by running the following command:go get -u github.com/go-sql-driver/mysql -
Connect to the instance. Replace the
<HOST>, port number,<USER>,<PASSWORD>,<DATABASE>, and<YOUR_TABLE_NAME>parameters with your actual values.package main import ( "database/sql" "fmt" "log" _ "github.com/go-sql-driver/mysql" ) func main() { // Connection settings dbHost := "<HOST>" // PolarDB-X instance endpoint dbPort := "3306" // Default port is 3306. dbUser := "<USER>" // Database account dbPass := "<PASSWORD>" // Password for the database account dbName := "<DATABASE>" // Target database // Build the DSN (Data Source Name). dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", dbUser, dbPass, dbHost, dbPort, dbName) // Open the connection. db, err := sql.Open("mysql", dsn) if err != nil { log.Fatalf("Failed to connect to database: %v", err) } defer db.Close() // Verify the connection. err = db.Ping() if err != nil { log.Fatalf("Failed to ping database: %v", err) } // Create a cursor-like query. var result string err = db.QueryRow("SELECT VERSION()").Scan(&result) if err != nil { log.Fatalf("Failed to execute query: %v", err) } // Print the database version. fmt.Printf("Connected to database, version: %s\n", result) // Run a query. rows, err := db.Query("SELECT * FROM `<YOUR_TABLE_NAME>`") // Table to query if err != nil { log.Fatalf("Failed to execute query: %v", err) } defer rows.Close() // Read results. for rows.Next() { var id int var name string if err := rows.Scan(&id, &name); err != nil { log.Fatalf("Failed to scan row: %v", err) } fmt.Printf("ID: %d, Name: %s\n", id, name) } // Check iteration errors. if err := rows.Err(); err != nil { log.Fatalf("Error during iteration: %v", err) } }




