MariaDB Connector/C

Overview

MariaDB Connectors enable high-performance data access by applications.

MariaDB Connectors are compatible with MariaDB SkySQL DBaaS.

MariaDB Connector/C can connect to MariaDB SkySQL from applications written in C or C++.

Install MariaDB Connector/C

Installation for MariaDB Client varies by operating system.

Installing MariaDB Connector/C via YUM (CentOS/RHEL)

  1. Configure YUM package repositories:

    $ sudo yum install wget
    
    $ wget https://downloads.mariadb.com/MariaDB/mariadb_repo_setup
    
    $ echo "fc84b8954141ed3c59ac7a1adfc8051c93171bae7ba34d7f9aeecd3b148f1527 mariadb_repo_setup" \
        | sha256sum -c -
    
    $ chmod +x mariadb_repo_setup
    
    $ sudo ./mariadb_repo_setup \
       --mariadb-server-version="mariadb-10.5"
    
  2. Install MariaDB Connector/C and package dependencies:

    $ sudo yum install MariaDB-shared MariaDB-devel
    

Installing MariaDB Connector/C via APT (Debian/Ubuntu)

  1. Configure APT package repositories:

    $ sudo apt install wget
    
    $ wget https://downloads.mariadb.com/MariaDB/mariadb_repo_setup
    
    $ echo "fc84b8954141ed3c59ac7a1adfc8051c93171bae7ba34d7f9aeecd3b148f1527 mariadb_repo_setup" \
        | sha256sum -c -
    
    $ chmod +x mariadb_repo_setup
    
    $ sudo ./mariadb_repo_setup \
       --mariadb-server-version="mariadb-10.5"
    
    $ sudo apt update
    
  2. Install MariaDB Connector/C and package dependencies:

    $ sudo apt install libmariadb3 libmariadb-dev
    

Installing MariaDB Connector/C via ZYpp (SLES)

  1. Configure ZYpp package repositories:

    $ sudo zypper install wget
    
    $ wget https://downloads.mariadb.com/MariaDB/mariadb_repo_setup
    
    $ echo "fc84b8954141ed3c59ac7a1adfc8051c93171bae7ba34d7f9aeecd3b148f1527 mariadb_repo_setup" \
        | sha256sum -c -
    
    $ chmod +x mariadb_repo_setup
    
    $ sudo ./mariadb_repo_setup \
       --mariadb-server-version="mariadb-10.5"
    
  2. Install MariaDB Connector/C and package dependencies:

    $ sudo zypper install MariaDB-shared MariaDB-devel
    

Installing MariaDB Connector/C via MSI (Windows)

  1. Go to the MariaDB Connector/C download page

  2. Ensure the "Product" drop-down reads "C connector".

  3. In the "Version" drop-down, select the version you want to download.

  4. In the "OS" drop-down, 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.

Connecting with MariaDB Connector/C

  1. Determine the connection information for your MariaDB SkySQL database service:

    Function

    Option/Argument

    Where to find it

    mysql_optionsv()

    MYSQL_OPT_SSL_CA option

    Path to the skysql_chain.pem file containing the "Certificate Authority Chain" (Download)

    mysql_real_connect()

    host argument

    Fully Qualified Domain Name in the Service Details view

    mysql_real_connect()

    user argument

    Default username in the Service Credentials view, or the username you created

    mysql_real_connect()

    passwd argument

    Default password in the Service Credentials view, the password you set on the default user, or the password for the user you created

    mysql_real_connect()

    port argument

    Read-Write Port or Read-Only Port in the Service Details view

  2. Enable TLS by calling the mysql_optionsv() function and setting the MYSQL_OPT_SSL_CA option.

    • The MYSQL_OPT_SSL_CA option should be set to the path to the skysql_chain.pem file containing the "Certificate Authority Chain" (Download)

  3. Connect to the database service by calling the mysql_real_connect() function.

    #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);
       }
    
       char* cafile = "/path/to/skysql_chain.pem";
    
       /* Configure TLS Connection */
       mysql_optionsv(conn, MYSQL_OPT_SSL_CA, cafile);
    
       /* Connect to MariaDB SkySQL */
       if (!mysql_real_connect(
             conn,                 /* Connection */
             "example.skysql.net", /* Host */
             "db_user",            /* User account */
             "db_user_passwd",     /* User password */
             "accounts",           /* Default database */
             5009,                 /* Port number */
             NULL,                 /* Path to socket file */
             0                     /* Additional options */
          ))
       {
    
          /* Report Error and Exit if connection fails */
          fprintf(stderr, "Error connecting to Server: %s\n", mysql_error(conn));
          exit(1);
    
       }
    
       return 0;
    }
    
  4. Compile the program.

    For example, to compile with gcc on Linux:

    $ gcc -o mariadb_connection_test mariadb_connection_test.c $(mariadb_config --include --libs)
    

API Reference

For an API reference, see "Connector/C reference".