Connect to a database
You can connect to a PolarDB-X instance by using Data Management (DMS), the MySQL command-line interface (CLI), third-party clients, or applications that are compatible with the official MySQL protocol.
Prerequisites
Before connecting to a PolarDB-X instance, complete the following tasks:
Connect to the database
You can connect to your database instance using various methods. Choose the one that best suits your workload. The following sections demonstrate common connection methods:
Connect with DMS
Data Management (DMS) is a browser-based data management tool provided by Alibaba Cloud. It integrates various services, including data management, schema management, user authorization, security audit, data trending, data tracking, BI charts, performance optimization, and server management. You can use DMS to manage your PolarDB-X instance without using other tools.
In the PolarDB for Distributed console, on the Instances page, click the ID of the 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 the PolarDB-X instance, and then click Search.
NoteThe first time you log on to a database using DMS, it defaults to the Flexible Management control mode. After you log on, you can change the control mode by editing the instance. For more information, see Modify instance information and Control modes.
After you configure the logon parameters, you can click Test Connectivity in the lower-left corner. If the test fails, review the error message and check the information you entered, such as the account and password.
The system automatically adds the IP addresses of the DMS servers to the IP whitelist of the cloud database. If this process fails, you must add them manually.
After logging on, the connected PolarDB-X instance appears in the Instances Connected section on the left, where you can begin managing it.

Connect with a GUI client
PolarDB-X supports connections from the following third-party clients. You can download a client from its official website.
MySQL Workbench (recommended)
SQLyog
Sequel Pro
Navicat for MySQL
Third-party GUI clients can be used to perform basic database operations, including CRUD and DDL operations. However, advanced features of the clients may not be supported by PolarDB-X.
The following steps use MySQL Workbench 8.0.29 as an example. The process is similar for other clients.
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 of the database endpoint.
NoteThe default port is 3306.
3306
Username
The database account.
polardb_x_user
Password
The password of the database account.
Pass***233
Connect with the MySQL CLI
If a MySQL client is installed on your server, you can use the command-line tool to connect to the PolarDB-X instance.
Syntax
mysql -h<endpoint> -P<port> -u<username> -p<password> -D<database>Example
mysql -hpxc-xxx.polarx.rds.aliyuncs.com -P3306 -upolardb_mysql_user -pPass***233 -Dtest_dbFlag | Description | Example |
-h | The database endpoint. | pxc-xxx.polarx.rds.aliyuncs.com |
-P | The port number of the database endpoint. Note
| 3306 |
-u | The database account. | polardb_x_user |
-p | The password of 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 |
Connect with an application
Connecting to a PolarDB-X instance is the same as connecting to any other MySQL database. Simply use the database endpoint, port, account, and password for the instance in your client library's connection string. The following examples show how to access a PolarDB-X database from common programming languages:
Java
This example uses a Maven project and the MySQL JDBC driver 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
<HOST>, the port number,<USER>,<PASSWORD>,<DATABASE>,<YOUR_TABLE_NAME>, and<YOUR_TABLE_COLUMN_NAME>with your actual information.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) { // The endpoint, port, and name of the PolarDB-X instance. String url = "jdbc:mysql://<HOST>:3306/<DATABASE>?useSSL=false&serverTimezone=UTC"; // The database account. String user = "<USER>"; // The password of 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(); // The data table to be queried. ResultSet rs = stmt.executeQuery("SELECT * FROM `<YOUR_TABLE_NAME>`"); while(rs.next()) { // The column to be retrieved. System.out.println(rs.getString("<YOUR_TABLE_COLUMN_NAME>")); } rs.close(); stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } }
Python
This example uses Python 3 and the PyMySQL library to connect to a PolarDB-X instance.
Install the PyMySQL library.
pip3 install PyMySQLConnect to the instance. Replace
<HOST>, the port number,<USER>,<PASSWORD>,<DATABASE>, and<YOUR_TABLE_NAME>with your actual information.import pymysql # Database connection parameters host = '<HOST>' # The endpoint of the PolarDB-X instance. port = 3306 # The default port is 3306. user = '<USER>' # The database account. password = '<PASSWORD>' # The password of the database account. database = '<DATABASE>' # The name of the database to which you want to connect. try: # Create a database connection. connection = pymysql.connect( host=host, port=port, user=user, passwd=password, db=database ) # Get a cursor. with connection.cursor() as cursor: # Execute an SQL query. sql = "SELECT * FROM `<YOUR_TABLE_NAME>`" # The data table to be queried. cursor.execute(sql) # Fetch the query results. results = cursor.fetchall() for row in results: print(row) finally: # Close the database connection. if 'connection' in locals() and connection.open: connection.close()
Go
This example uses Go 1.22, the database/sql package, and the go-sql-driver/mysql driver to connect to a PolarDB-X instance.
Install the
go-sql-driver/mysqldriver.go get -u github.com/go-sql-driver/mysqlConnect to the instance. Replace
<HOST>, the port number,<USER>,<PASSWORD>,<DATABASE>, and<YOUR_TABLE_NAME>with your actual information.package main import ( "database/sql" "fmt" "log" _ "github.com/go-sql-driver/mysql" ) func main() { // Database connection parameters dbHost := "<HOST>" // The endpoint of the PolarDB-X instance. dbPort := "3306" // The default port is 3306. dbUser := "<USER>" // The database account. dbPass := "<PASSWORD>" // The password of the database account. dbName := "<DATABASE>" // The name of the database to which you want to connect. // Build the Data Source Name (DSN). dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", dbUser, dbPass, dbHost, dbPort, dbName) // Open a database connection. db, err := sql.Open("mysql", dsn) if err != nil { log.Fatalf("Failed to connect to database: %v", err) } defer db.Close() // Test the connection. err = db.Ping() if err != nil { log.Fatalf("Failed to ping database: %v", err) } // Query the database version. 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) // Execute an SQL query. rows, err := db.Query("SELECT * FROM `<YOUR_TABLE_NAME>`") // The data table to be queried. if err != nil { log.Fatalf("Failed to execute query: %v", err) } defer rows.Close() // Process the query 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 for iteration errors. if err := rows.Err(); err != nil { log.Fatalf("Error during iteration: %v", err) } }




