You can connect to a PolarDB-X instance by using Data Management (DMS), the MySQL CLI, third-party MySQL-compatible clients, or your application code.
Prerequisites
Before you connect to a PolarDB-X database, complete the following steps:
Connect to the database
You can connect to a database instance in several ways, depending on your workload. The following sections show common examples.
DMS
Data Management (DMS) is a graphical data management tool provided by Alibaba Cloud. It integrates a suite of services, including data management, schema management, user authorization, security auditing, data trending, data tracing, BI charts, performance optimization, and server management. You can use DMS to manage your PolarDB-X instance directly without needing other tools.
-
Go to the PolarDB for Distributed console. In the Instances list, click the ID of the target instance to open its details page. In the upper-right corner of the page, click Log On to Database.
-
In the dialog box, enter the Database Account and Database Password for the PolarDB-X instance, and then click Search.
Note-
The first time you log on by using DMS, the control mode is Flexible Management by default. After you log on, you can change the control mode by editing the instance. For more information, see Edit instance information and Control modes.
-
After you configure the logon parameters, you can click Test Connectivity in the lower-left corner. If the connection fails, review the error message and check the information you entered, such as the account or password.
-
The system automatically attempts to add the IP addresses of the DMS servers to the whitelist of the PolarDB-X instance. If this attempt fails, you must add the IP addresses manually.
-
-
After you log on, the PolarDB-X instance appears in the Connected Instances section of the left-side navigation pane. You can then manage the instance.
GUI client
PolarDB-X supports connections from the following third-party clients. You can download these clients from their official websites.
-
MySQL Workbench (Recommended)
-
SQLyog
-
Sequel Pro
-
Navicat for MySQL
Third-party GUI clients support basic database operations, including CRUD and DDL. Advanced, client-specific features may not be supported by PolarDB-X.
The following steps use MySQL Workbench 8.0.29 as an example. Other clients follow a similar procedure.
-
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<database_username> -p<database_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 from an application is similar to connecting to a standard MySQL database, requiring only the database endpoint, port, account, and password. The following examples show how to access a PolarDB-X instance using common programming languages:
Java
This example uses a Maven project and the MySQL JDBC driver to connect to a PolarDB-X instance.
-
First, you need to add the MySQL JDBC driver dependency to the
pom.xmlfile. The following is a code example:<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> -
Connect to the instance. Replace the parameters
<HOST>, port number,<USER>,<PASSWORD>,<DATABASE>,<YOUR_TABLE_NAME>, and<YOUR_TABLE_COLUMN_NAME>.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>"; // Database password 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 retrieve System.out.println(rs.getString("<YOUR_TABLE_COLUMN_NAME>")); } rs.close(); stmt.close(); conn.close(); } catch (Exception var7) { var7.printStackTrace(); } } }
Python
This example uses Python 3 and the PyMySQL library to connect to a PolarDB-X instance.
-
Install the PyMySQL library. If it is not installed, run the following command:
pip3 install PyMySQL -
Connect to the instance. Replace the parameters
<HOST>, port number,<USER>,<PASSWORD>,<DATABASE>, and<YOUR_TABLE_NAME>.import pymysql # Connection parameters host = '<HOST>' # PolarDB-X instance endpoint port = 3306 # Default port is 3306 user = '<USER>' # Database account password = '<PASSWORD>' # Database password database = '<DATABASE>' # Target database name try: # Create a database connection connection = pymysql.connect( host=host, port=port, user=user, passwd=password, db=database ) # Create a cursor with connection.cursor() as cursor: # Run an SQL query sql = "SELECT * FROM `<YOUR_TABLE_NAME>`" # Table to query cursor.execute(sql) # Fetch 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.23.0, the database/sql package, and the go-sql-driver/mysql driver to connect to a PolarDB-X instance.
-
First, you need to install the
go-sql-driver/mysqldriver. You can install it by running the following command:go get -u github.com/go-sql-driver/mysql -
Connect to the instance. Replace the parameters
<HOST>, port number,<USER>,<PASSWORD>,<DATABASE>, and<YOUR_TABLE_NAME>.package main import ( "database/sql" "fmt" "log" _ "github.com/go-sql-driver/mysql" ) func main() { // Connection parameters dbHost := "<HOST>" // PolarDB-X instance endpoint dbPort := "3306" // Default port is 3306 dbUser := "<USER>" // Database account dbPass := "<PASSWORD>" // Database password dbName := "<DATABASE>" // Target database name // 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 database connection db, err := sql.Open("mysql", dsn) if err != nil { log.Fatalf("Failed to connect to database: %v", err) } defer db.Close() // Ping the server to test the connection err = db.Ping() if err != nil { log.Fatalf("Failed to ping database: %v", err) } // Query the server 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) // Run an SQL 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() // 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 errors during row iteration if err := rows.Err(); err != nil { log.Fatalf("Error during iteration: %v", err) } }