MySQL Connector/C, also known as the MySQL C API, provides a set of C language functions and data structures that allow developers to connect to, query, and manage MySQL databases using C/C++ languages. This API enables developers to directly use MySQL databases in their C/C++ applications.
This topic describes how to build an application using the MySQL Connector/C (libmysqlclient) driver and OceanBase AI Database to perform basic operations such as creating tables, inserting data, and querying data.
Prerequisites
Before installing and using MySQL Connector/C (libmysqlclient), ensure you have set up a basic database development environment with the following requirements:
- GCC version 3.4.6 or later. Version 4.8.5 is recommended.
- CMake version 2.8.12 or later.
- You have installed OceanBase AI Database and created a user tenant.
Procedure
- Obtain the connection information for OceanBase AI Database.
- Install the MySQL Connector/C driver.
- Write the application.
- Run the application.
Step 1: Obtain the database connection string
Contact the deployment personnel or administrator of OceanBase AI Database to obtain the corresponding database connection string, for example:
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,DROP, andSELECTprivileges on this database. For more information about user privileges, see Privilege types.$user_name: The connection account for 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 account password.
For more information about the connection string, see Connect to OceanBase AI Database by using OBClient.
Example:
obclient -hxxx.xxx.xxx.xxx -P2881 -utest_user001@mysql001 -p****** -Dtest
Step 2: Install the MySQL Connector/C driver
## To install on Ubuntu, use the following command:
sudo apt-get install libmysqlclient-dev
## To install on a CentOS system, use the following code:
sudo yum install mysql-devel
## Confirm whether the installation was successful
mysql_config --version
Step 3: Write an application
Open your text editor, edit the sample test.cc file, and save it. The code is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
int main() {
MYSQL *conn = mysql_init(NULL); // Initialize a MySQL connection.
if (conn == NULL) {
fprintf(stderr, "mysql_init() failed\n");
return 1;
}
// Connect to the MySQL database server.
if (mysql_real_connect(conn, "host", "user", "passwd", "db", port, NULL, 0) == NULL) {
fprintf(stderr, "mysql_real_connect() failed\n");
mysql_close(conn);
return 1;
}
// Create a user table.
if (mysql_query(conn, "CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT, name VARCHAR(255), email VARCHAR(255), PRIMARY KEY(id))") != 0) {
fprintf(stderr, "Error in creating table: %s\n", mysql_error(conn));
mysql_close(conn);
return 1;
}
// Insert data.
if (mysql_query(conn, "INSERT INTO users (name, email) VALUES ('Xiao Ming', 'xiaoming@example.com')") != 0) {
fprintf(stderr, "Error in inserting data: %s\n", mysql_error(conn));
mysql_close(conn);
return 1;
}
// Query data
if (mysql_query(conn, "SELECT * FROM users") == 0) {
MYSQL_RES *result = mysql_store_result(conn);
if (result != NULL) {
int num_fields = mysql_num_fields(result);
MYSQL_ROW row;
while ((row = mysql_fetch_row(result))) {
for (int i = 0; i < num_fields; i++) {
printf("%s ", row[i] ? row[i] : "NULL");
}
printf("\n");
}
mysql_free_result(result);
} else {
fprintf(stderr, "Error in retrieving result: %s\n", mysql_error(conn));
}
} else {
fprintf(stderr, "Error in selecting data: %s\n", mysql_error(conn));
}
// Drop the table
if (mysql_query(conn, "DROP TABLE IF EXISTS users") != 0) {
fprintf(stderr, "Error in dropping table: %s\n", mysql_error(conn));
mysql_close(conn);
return 1;
}
mysql_close(conn); // Close the connection.
return 0;
}
Modify the database connection information in the test.cc project file based on the information obtained in Step 1: Obtain OceanBase AI Database connection information.
- In a Linux environment, use the
vi test.ccorvim test.cccommand to edit thetest.ccfile and modify the database connection information to match your actual configuration.
The following example shows the database connection information in the test.cc file:
# Modify this part of the connection information with the actual connection string you obtained. Example:
if (mysql_real_connect(conn, "10.10.10.1", "test_user001@mysql001", "test", "db", 2881, NULL, 0) == NULL) {
fprintf(stderr, "mysql_real_connect() failed\n");
mysql_close(conn);
return 1;
}
Step 4: Compile the application
After editing the code, compile it using the following command.
gcc -o test test.cc `mysql_config --cflags --libs`
After successful compilation, an executable file named test is generated.
Step 5: Run the application
Run the application using the following command.
./test
The output result is as follows. If this result is displayed, it indicates a successful database connection and correct execution of the sample statement.
1 Xiaoming xiaoming@example.com