This topic describes how to connect to and use OceanBase AI Database by using the MySQL Connector/Python driver.
Prerequisites
- You have installed Python 2.7 or Python 3.x and pip.
- You have installed OceanBase AI Database and created a user tenant.
Procedure
- Obtain the connection string of OceanBase AI Database.
- Install the MySQL Connector/Python driver.
- Create a
test.pyfile and specify the database connection information. - Run the
test.pyfile.
Step 1: Obtain the connection string of OceanBase AI Database
Contact the deployment personnel or administrator of OceanBase AI Database to obtain the corresponding database connection string.
obclient -h$host -P$port -u$user_name -p$password -D$database_name
Parameter description:
$host: The IP address for connecting to OceanBase AI Database. For connections through OceanBase Database Proxy (ODP), use an ODP address; for direct connections, use the IP address of an OBServer node.$port: The port for connecting to OceanBase AI Database. For ODP connections, the default port is2883, which can be customized during ODP deployment. For direct connections, the default port is2881, which can be customized during OceanBase AI Database deployment.$database_name: The name of the database to access.Notice
The user connecting to the tenant must have the
CREATE,INSERT,UPDATE, andSELECTprivileges on this database. For more information about user privileges, see Privilege types.$user_name: The account for connecting to the tenant. For ODP connections, the common formats areusername@tenant_name#cluster_nameorcluster_name:tenant_name:username. For direct connections, the format isusername@tenant_name.$password: The password of the account.
For more information about the connection string, see Overview of connection methods.
Example:
obclient -hxxx.xxx.xxx.xxx -P2881 -utest_user001@mysql001 -p****** -Dtest
Step 2: Install the MySQL Connector/Python driver
MySQL Connector/Python is an official driver provided by MySQL for connecting to MySQL databases. It is written in Python and is used to connect to and operate MySQL databases in Python. MySQL Connector/Python supports Python 2.7 and Python 3.x.
Open the command prompt or PowerShell terminal and run the following command to install the MySQL Connector/Python driver.
pip3 install mysql-connector-python
After installation, you can verify the installation by running the following command:
pip3 list | grep mysql-connector-python
Step 3: Write an application
Step 3: Write the test.py file and fill in the database connection information
Write the test.py file based on the information obtained in Step 1: Obtain the OceanBase AI Database connection string and fill in the database connection information.
Create a file named
test.py.Fill in the following content in the
test.pyfile and modify the database connection information as needed.The following example shows the content of the
test.pyfile:import mysql.connector # Database connection information config = { 'host': 'xxx.xxx.xxx.xxx', 'port': 2881, 'user': 'test_user001@mysql001', 'password': '******', 'database': 'test' } # Create a database connection. conn = mysql.connector.connect(**config) cursor = conn.cursor() # Create a database. cursor.execute("CREATE DATABASE IF NOT EXISTS test") # Use the database cursor.execute("USE test") # Create Table cursor.execute(""" CREATE TABLE IF NOT EXISTS test_table ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, age INT ) """) # Insert data cursor.execute(""" INSERT INTO test_table (name, age) VALUES ('John', 20), ('Lucy', 25), ('Tom', 30) """) # Update data cursor.execute(""" UPDATE test_table SET age = 26 WHERE name = 'Lucy' """) # Query data cursor.execute("SELECT * FROM test_table") rows = cursor.fetchall() for row in rows: print(row) # Close Connection cursor.close() conn.close()
Step 4: Run the test.py file
Open the command prompt or PowerShell terminal and run the test.py file to query data and output the results.
Navigate to the directory where the
test.pyfile is located.Example:
cd D:\demo\demoRun the
test.pyfile.Example:
python test.pyThe result is as follows:
(1, 'John', 20) (2, 'Lucy', 26) (3, 'Tom', 30)
Error handling
When using MySQL-connector-python to connect to OceanBase AI Database, you may encounter various errors. The following are some common errors and their handling methods:
Connection error: If you cannot connect to the database, check whether the connection parameters, including the hostname, port, username, password, and database name, are correct.
Permission error: If you encounter a permission-related error, ensure that the user has sufficient permissions to perform the required operation.
SQL syntax error: If an SQL statement contains a syntax error, check whether the SQL statement syntax is correct.
Data type error: If the data type being inserted does not match the table definition, ensure the inserted data type is correct.
In your code, you can use try-except statements to catch and handle these errors, ensuring the program handles them gracefully instead of crashing directly. Additionally, you can use the logging module to record error information, which facilitates debugging and troubleshooting.
References
For more information about connecting to OceanBase AI Database, see Overview of connection methods.
For more information about creating a database, see CREATE DATABASE.