PolarDB-X supports connections through Data Management (DMS), MySQL command line, third-party clients, and third-party program code that complies with the official MySQL interaction protocol.
Preparations
Before connecting to the PolarDB-X database, you need to perform the following operations:
Connect to a database
There are many ways to connect to a database instance. You can choose the appropriate connection method based on your actual business needs. The following are some examples of connecting to a database instance:
Use DMS to connect to a database
DMS is a graphical data management tool provided by Alibaba Cloud. It is a data management service that integrates data management, structure management, user authorization, security audit, data trend, data tracking, BI chart, performance and optimization, and server management. You can manage your PolarDB-X instance directly on DMS without using other tools.
-
Go to the PolarDB Distributed Edition Console, click the target instance ID in the Instance List to enter the instance product page. Click Log On To Database in the upper right corner of the page.
-
In the pop-up dialog box, enter the Database Account and Database Password created in the PolarDB-X instance, and click Log On.
Note-
When logging on through DMS for the first time, the control mode defaults to Flexible Management. After successful login, you can also modify the control mode through the edit instance function. For more information, see Edit Instance Information and Control Mode.
-
After configuring the login parameters, you can click Test Connection in the lower left corner. If the test connection fails, check the entered instance information according to the error prompt, such as whether the account or password is correct.
-
The system will automatically try to add the server access address of DMS to the whitelist of the cloud database. If the automatic addition fails, please add it manually.
-
-
After logging in, you can view the PolarDB-X instance you are logged into within the Logged-in Instances section of the left-side navigation pane and carry out the necessary management operations.
Use a client to connect to a database
PolarDB-X supports connections through the following third-party clients. You can download the client from the corresponding official website.
MySQL Workbench (recommended)
SQLyog
Sequel Pro
Navicat for MySQL
Third-party GUI clients can perform basic database operations, including data addition, deletion, modification, query, and DDL operations. For advanced features of the tool, PolarDB-X may not support them.
The following uses MySQL Workbench version 8.0.29 as an example. The operations of other clients are similar.
Install MySQL Workbench. For the official download address, see MySQL Workbench Download Page.
Open MySQL Workbench, select
.Enter the connection information and click OK.
Parameter
Description
Example
Hostname
pxc-xxx.polarx.rds.aliyuncs.com
Port
The port number of the database connection address, corresponding to the database connection address.
NoteThe default port is 3306.
3306
Username
polardb_x_user
Password
The password of the database account.
Pass***233
Use the MySQL command line to connect to a database
If your server has the MySQL client installed, you can connect to the PolarDB-X instance through the command line.
Syntax:
mysql -h<connection address> -P<port> -u<database username> -p<database user password> -D<database name>
Example:
mysql -hpxc-xxx.polarx.rds.aliyuncs.com -P3306 -upolardb_mysql_user -pPass***233 -Dtest_db
Parameter | Description | Example |
-h | pxc-xxx.polarx.rds.aliyuncs.com | |
-P | The port number of the database connection address, corresponding to the database connection address. Note
| 3306 |
-u | polardb_x_user | |
-p | The password of the database account. Note This parameter is required.
| Pass***233 |
-D | The name of the database to log on to. Note This parameter is not required. | test_db |
Use an application to connect to a database
The method of connecting to a PolarDB-X instance is the same as connecting to other MySQL databases. You only need to replace the database connection address, port, account, and password. The following lists how to access the PolarDB database through applications in some development languages:
Java
This example uses a Maven project to connect to a PolarDB-X instance using the MySQL JDBC driver.
-
First, you need to add the MySQL JDBC driver dependency in the
pom.xml
file. 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 instance connection address, port, and database name to connect to String url = "jdbc:mysql://<HOST>:3306/<DATABASE>?useSSL=false&serverTimezone=UTC"; // Database account String user = "<USER>"; // 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(); // Name of the data table to retrieve ResultSet rs = stmt.executeQuery("SELECT * FROM `<YOUR_TABLE_NAME>`"); while(rs.next()) { // Name of the column in the data table 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 Python3 to connect to a PolarDB-X instance using the PyMySQL library.
-
First, you need to install the PyMySQL library. If you have not installed it, you can install it using 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 # Database connection parameters host = '<HOST>' # PolarDB instance connection address port = 3306 # Default port 3306 user = '<USER>' # Database account password = '<PASSWORD>' # Password of the database account database = '<DATABASE>' # Database name to connect to try: # Create a database connection connection = pymysql.connect( host=host, port=port, user=user, passwd=password, db=database ) # Obtain the operation cursor with connection.cursor() as cursor: # Execute SQL query sql = "SELECT * FROM `<YOUR_TABLE_NAME>`" # Name of the data table to retrieve cursor.execute(sql) # Obtain 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 go1.23.0 to connect to a PolarDB-X instance using the database/sql
package and the go-sql-driver/mysql
driver.
-
First, you need to install the
go-sql-driver/mysql
driver. You can install it using 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() { // Database connection parameters dbHost := "<HOST>" // PolarDB instance connection address dbPort := "3306" // Default port 3306 dbUser := "<USER>" // Database account dbPass := "<PASSWORD>" // Password of the database account dbName := "<DATABASE>" // Database name to connect to // Construct 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() // Test the connection err = db.Ping() if err != nil { log.Fatalf("Failed to ping database: %v", err) } // Create an operation cursor var result string err = db.QueryRow("SELECT VERSION()").Scan(&result) if err != nil { log.Fatalf("Failed to execute query: %v", err) } // Output the database version fmt.Printf("Connected to database, version: %s\n", result) // Execute SQL query rows, err := db.Query("SELECT * FROM `<YOUR_TABLE_NAME>`") // Name of the data table to retrieve if err != nil { log.Fatalf("Failed to execute query: %v", err) } defer rows.Close() // Process 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 if err := rows.Err(); err != nil { log.Fatalf("Error during iteration: %v", err) } }