MariaDB Connector/C

C developers can connect to MariaDB SkySQL from their application using MariaDB Connector/C.

MariaDB Connector/C can be used with:

  • MariaDB Platform for Transactions

  • MariaDB Platform for Analytics

  • MariaDB Platform for Smart Transactions

  • MariaDB Platform for Scale-Out Transactions

Connector

MariaDB Connector/C

Supported Versions

3.1

Programming Language

C

Programming Language Version

N/A

API

libmariadb

Supports TLS

Yes

Supports Connection Pools

No

License

GNU Lesser General Public License v2.1

MariaDB Connector/C installation and connection instructions follow. Additional information on MariaDB Connector/C can be found in the Connector/C reference.

Installing MariaDB Connector/C

Installation for MariaDB Client varies by operating system.

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

  1. Configure YUM package repositories:

    $ sudo yum install wget
    
    $ wget https://downloads.mariadb.com/MariaDB/mariadb_repo_setup
    
    $ echo "2de6253842f230bc554d3f5ab0c0dbf717caffbf45ae6893740707961c8407b7 mariadb_repo_setup" \
        | sha256sum -c -
    
    $ chmod +x mariadb_repo_setup
    
    $ sudo ./mariadb_repo_setup \
       --mariadb-server-version="mariadb-10.4"
    
  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 "2de6253842f230bc554d3f5ab0c0dbf717caffbf45ae6893740707961c8407b7 mariadb_repo_setup" \
        | sha256sum -c -
    
    $ chmod +x mariadb_repo_setup
    
    $ sudo ./mariadb_repo_setup \
       --mariadb-server-version="mariadb-10.4"
    
    $ 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 "2de6253842f230bc554d3f5ab0c0dbf717caffbf45ae6893740707961c8407b7 mariadb_repo_setup" \
        | sha256sum -c -
    
    $ chmod +x mariadb_repo_setup
    
    $ sudo ./mariadb_repo_setup \
       --mariadb-server-version="mariadb-10.4"
    
  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 download page:

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

  3. 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.

  4. Click on the "Download" button to download the MSI package.

  5. When the MSI package finishes downloading, run it.

  6. On the first screen, click "Next" to start the Setup Wizard.

  7. On the second screen, click the license agreement checkbox, and then click "Next".

  8. On the third screen, select the components you want to install. If you only want MariaDB Connector/C:

    • Deselect "MariaDB Server".

    • Deselect "Third party tools".

    • When only "Development components" is selected, click "Next".

  9. On the next screen, click "Install".

  10. When the installation process completes, click "Finish".

Connecting with MariaDB Connector/C

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

    What

    Where to find it

    mysql_optionsv()
    MYSQL_OPT_SSL_CA option

    Path to the skysql_chain.pem file containing the "Certificate Authenticity 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 Authenticity 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)