Connect with MariaDB Connector/C
From; https://mariadb.com/docs/server/connect/programming -languages/c/connect/





Install MariaDB Connector/C



Overview MariaDB Connector/C supports several Linux distributions and Microsoft Windows.
Configure Package Repository (Linux) To install MariaDB Connector/C on Linux using APT, YUM, or ZYpp, you must configure your system to use either the ES Package Repository or the CS Package Repository. If your system is already configured to use one of these package repositories, you can skip to install MariaDB Connector/C. Choose a package repository to configure:
Package RepositoryDescription
ES Package Repository MariaDB Enterprise Server package repository Available to customers of MariaDB Corporation Available for APT, YUM, and ZYpp on supported Linux distributions Configured with the mariadb_es_repo_setup utility
CS Package Repository MariaDB Community Server package repository Publicly available Available for APT, YUM, and ZYpp on supported Linux distributions Configured with the mariadb_repo_setup utility

ES Package Repository MariaDB Connector/C is available from the same package repository as MariaDB Enterprise Server. To configure the ES package repository:
  1. Install wget. Install via APT on Debian, Ubuntu: $ sudo apt install wget Install via YUM on CentOS, RHEL, Rocky Linux: $ sudo yum install wget Install via ZYpp on SLES: $ sudo zypper install wget
  2. Download the mariadb_es_repo_setup utility, validate its checksum, and ensure that its permissions allow it to be executed: $ wget https://dlm.mariadb.com/enterprise-release- helpers/mariadb_es_repo_setup $ echo "f8eb9c1b59ccfd979d27e39798d2f2a98447dd29e2149ce92bf606aab4493ad9 \ mariadb_es_repo_setup" | sha256sum -c - $ chmod +x mariadb_es_repo_setup
  3. Retrieve your Customer Download Token at https://cl oud.mariadb.com/csm?id=my_customer_token and substitute your token for CUSTOMER_DOWNLOAD_TOKEN in the following step.
  4. Configure the ES package repository using the mariadb_es_repo_setup utility: $ sudo ./mariadb_es_repo_setup --token="CUSTOMER_DOWNLOAD_TOKEN" --apply \ --mariadb-server-version="10.6"
    • All major releases of ES contain the same version of MariaDB Connector/C.
    • By default, the mariadb_es_repo_setup utility will configure your system to use the package repository for ES 10.6.
    • To configure your system to use the ES package repository for a specific major release, use the --mariadb-server-version option.
  5. Install MariaDB Connector/C using the package repository.

CS Package Repository MariaDB Connector/C is available from the same package repository as MariaDB Community Server. To configure the CS package repository:
  1. Install wget. Install via APT on Debian, Ubuntu: $ sudo apt install wget Install via YUM on CentOS, RHEL, Rocky Linux: $ sudo yum install wget Install via ZYpp on SLES: $ sudo zypper install wget
  2. Download the mariadb_repo_setup utility, validate its checksum, and ensure that its permissions allow it to be executed: $ wget https://r.mariadb.com/downloads/mariadb_repo_setup $ echo "30d2a05509d1c129dd7dd8430507e6a7729a4854ea10c9dcf6be88964f3fdc25 \ mariadb_repo_setup" | sha256sum -c - $ chmod +x mariadb_repo_setup
  3. Configure the CS package repository using the mariadb_repo_setup utility: $ sudo ./mariadb_repo_setup \ --mariadb-server-version="mariadb-10.6"
    • All major releases of CS contain the same version of MariaDB Connector/C.
    • By default, the mariadb_repo_setup utility will configure your system to use the package repository for CS 10.6.
    • To configure your system to use the CS package repository for a specific major release, use the --mariadb-server-version option.
  4. Install MariaDB Connector/C using the package repository.
    Installation via Package Repository (Linux) On supported Linux distributions, MariaDB Connector/C can be installed using APT, YUM, or ZYpp if the system is configured to use the ES Package Repository or the CS Package Repository.
    Install on CentOS, RHEL, Rocky Linux To install MariaDB Connector/C on CentOS, RHEL, and Rocky Linux, you can use YUM if you have the ES Package Repository or CS Package Repository configured. Install MariaDB Connector/C and package dependencies: $ sudo yum install MariaDB-shared MariaDB-devel
    Install on Debian, Ubuntu To install MariaDB Connector/C on Debian and Ubuntu, you can use APT if you have the ES Package Repository or CS Package Repository configured. Install MariaDB Connector/C and package dependencies: $ sudo apt install libmariadb3 libmariadb-dev
    Install on SLES To install MariaDB Connector/C on SLES, you can use ZYpp if you have the ES Package Repository or CS Package Repository configured. Install MariaDB Connector/C and package dependencies: $ sudo zypper install MariaDB-shared MariaDB-devel
    Install via Binary Tarball (Linux) MariaDB Connector/C can be installed on supported Linux distributions via a binary tarball package:
    1. Go to the MariaDB Connector/C download page
    2. Ensure the "Product" dropdown reads "C connector".
    3. In the "Version" dropdown, select the version you want to download.
    4. In the "OS" dropdown, select your Linux distribution.
    5. Click on the "Download" button to download the binary tarball package.

    Install via MSI (Windows) MariaDB Connector/C can be installed on Microsoft Windows via an MSI package:
    1. Go to the MariaDB Connector/C download page
    2. Ensure the "Product" dropdown reads "C connector".
    3. In the "Version" dropdown, select the version you want to download.
    4. In the "OS" dropdown, select either "MS Windows (64-bit)" or "MS Windows (32-bit)", depending on whether you need a 64 -bit or 32-bit connector.
    5. Click on the "Download" button to download the MSI package.
    6. When the MSI package finishes downloading, run it and follow the on-screen instructions

    Connect Connect with MariaDB Connector/C
    Overview MariaDB Connector/C enables C and C++ applications to establish client connections to MariaDB database products over TLS. Additional information on MariaDB Connector/C is available in the MariaDB Knowledge Base. Connection Info The connection is configured via the information that is initially acquired from the SkySQL Portal pages:
    Function Option/Argument Where to find it
    mysql_optionsv() MYSQL_OPT_SSL_CA option The path to the skysql_chain.pem file containing the "Certificate Authority Chain"
    • Download For GCP services
    • Download For AWS services
    mysql_real_connect() host argument The fully Qualified Domain Name in the Service Details view
    mysql_real_connect() user argument The desired username, which might be the default username in the Service Credentials view
    mysql_real_connect() passwd argument The user's password, which might be the default password in the Service Credentials view if it was not yet customized
    mysql_real_connect() port argument The Read-Write Port or Read-Only Port in the Service Details view

    Code Example: Connect The following code demonstrates how to use MariaDB Connector/C to connect to MariaDB database products. This example uses the example database and user account: /*H***************************************************** * *******************************************************/ #include <stdio.h> #include <stdlib.h> #include <mysql.h> /*H***************************************************** * *******************************************************/ int main ( int argc, char *argv[] ) { MYSQL *conn; // Initialize Connection if( !(conn = mysql_init(0))) { fprintf(stderr, "unable to initialize connection struct\n"); exit(1); } char* cafile = "/path/to/skysql_chain.pem"; // Configure TLS Certificate Authority. may vary by hosting provider! mysql_optionsv( conn, MYSQL_OPT_SSL_CA, cafile ); if( !mysql_real_connect( // Connect to the database conn, // Connection "example.skysql.net", // Host "db_user", // User account "db_user_password", // User password "test", // Default database 5009, // Port number NULL, // Path to socket file 0 // Additional options )) { // Report failed-connection error & close the handle fprintf( stderr, "Error connecting to Server: %s\n", mysql_error(conn)); mysql_close( conn ); exit( 1 ); } // Use Connection // ... // Close Connection mysql_close( conn ); return( 0 ); }
    Example 2 Connect with MariaDB Connector/C Overview MariaDB Connector/C enables C and C++ applications to establish client connections to MariaDB database products over TLS. Additional information on MariaDB Connector/C is available in the MariaDB Knowledge Base. Code Example: Connect The following code demonstrates how to use MariaDB Connector/C to connect to MariaDB database products. This example uses the example database and user account:
    Example 2 Code #include <stdio.h> #include <stdlib.h> #include <mysql.h> int main (int argc, char* argv[]) { // Initialize Connection MYSQL *conn; if (!(conn = mysql_init(0))) { fprintf(stderr, "unable to initialize connection struct\n"); exit(1); } // Connect to the database if (!mysql_real_connect( conn, // Connection "mariadb.example.net",// Host "db_user", // User account "db_user_password", // User password "test", // Default database 3306, // Port number NULL, // Path to socket file 0 // Additional options )) { // Report the failed-connection error & close the handle fprintf(stderr, "Error connecting to Server: %s\n", mysql_error(conn)); mysql_close(conn); exit(1); } // Use the Connection // ... // Close the Connection mysql_close(conn); return 0; }
    Setup for Examples
    Overview Examples in this MariaDB Connector/C documentation depend on a database test and table contacts.
    Create the Schema Create the example database and table: CREATE DATABASE IF NOT EXISTS test; CREATE TABLE test.contacts ( id INT PRIMARY KEY AUTO_INCREMENT, first_name VARCHAR(25), last_name VARCHAR(25), email VARCHAR(100)) ENGINE=InnoDB;
    Create the User Create a user db_user with privileges to execute the examples: CREATE USER IF NOT EXISTS db_user@192.0.2.1 IDENTIFIED BY 'db_user_password'; GRANT ALL PRIVILEGES ON test.* TO db_user@192.0.2.1;
    Password Guidance Passwords should meet your organization's password policies. If your MariaDB Enterprise Server instance has a password validation plugin installed, the password must also meet the configured requirements. By default, MariaDB Enterprise Server installs the simple_password_check plugin, configured with system variables:
    Application Development with MariaDB Connector/C
    Overview MariaDB Connector/C enables C and C++ applications to establish client connections to MariaDB database products over TLS.
    Build Your Application with Connector/C When you build a C application, your compiler must link your application with the MariaDB Connector/C shared library. The following gcc (GNU GCC) command demonstrates how to link an application with the MariaDB Connector/C shared library using the mariadb_config utility to determine the compiler arguments: $ gcc -o example example.c $(mariadb_config --include --libs) If you are not using the gcc compiler, please consult your compiler's manual.
    Header Files MariaDB Connector/C includes several header files. In some cases, developers might find it useful to inspect the MariaDB Connector/C header files to view the definitions of structures, functions, and constants. The header files:
    • Contain the definitions of structures, functions, and constants.
    • Are installed to the /usr/include/mariadb/ directory by default on Linux.
    C applications developed using MariaDB Connector/C must include the mysql.h header file.