title: Embedded MariaDB C/C++ API
body: <p>I'm trying to get an embedded MariaDB (i.e. not connecting to running server) setup going but I'm failing to get any of the examples I find to work.</p>
  
  <p>The most recent example I have is from this post <a href="https://stackoverflow.com/a/24548826/400048">https://stackoverflow.com/a/24548826/400048</a></p>
  
  <p>When the app runs it produces:
  <code>Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)</code>
  The docs <a href="https://mariadb.com/kb/en/library/embedded-mariadb-interface/" rel="noreferrer">https://mariadb.com/kb/en/library/embedded-mariadb-interface/</a> isn't much help on this.</p>
  
  <p>For convenience the code from that StackOverflow post is:</p>
  
  <pre><code>#include &lt;my_global.h&gt;
  #include &lt;mysql.h&gt;
  
  int main(int argc, char **argv) {  
      static char *server_options[] = {
                                     "mysql_test", // An unused string 
                                      "--datadir=/tmp/mysql_embedded_data", // Your data dir
                                      NULL };
      int num_elements = (sizeof(server_options) / sizeof(char *)) - 1;
  
      static char *server_groups[] = { "libmysqld_server",
                                       "libmysqld_client", NULL };
  
  
      // Init MySQL lib and connection    
      mysql_library_init(num_elements, server_options, server_groups);
      MYSQL *con = mysql_init(NULL);
  
      if (con == NULL) {
          fprintf(stderr, "%s\n", mysql_error(con));
          exit(1);
      }
  
      mysql_options(con, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client");
      mysql_options(con, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);
  
      // Connect to no host/port -&gt; Embedded mode
      if (mysql_real_connect(con, NULL, NULL, NULL, NULL, 0, NULL, 0) == NULL) {
          fprintf(stderr, "%s\n", mysql_error(con));
          mysql_close(con);
          exit(1);
      }
  
      // Create a sample empty DB, named "aNewDatabase"
      if (mysql_query(con, "CREATE DATABASE aNewDatabase")) {
          fprintf(stderr, "%s\n", mysql_error(con));
          mysql_close(con);
          exit(1);
      }
  
      // Close connection
      mysql_close(con);
      exit(0);
  }
  </code></pre>
  
  <p>I had a cursory look at <a href="https://github.com/MariaDB/server" rel="noreferrer">https://github.com/MariaDB/server</a> but didn't know where to really look...or in fact what I was looking for.
  How does one go about getting an embedded mariadb going?
  I'm running on Mac OS High Sierra, MariaDB was installed with <code>brew install mariadb --with-embedded</code>. </p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I'm fairly certain I'm linking to the correct lib.</p>
  
  <p><code>ls /usr/local/lib | grep maria</code></p>
  
  <p><a href="https://i.stack.imgur.com/j4PAN.png" rel="noreferrer"><img src="https://i.stack.imgur.com/j4PAN.png" alt="MariaDB"></a></p>
  
  <pre><code>FIND_LIBRARY(mariadb mariadb)
  MESSAGE(FATAL_ERROR "BOOM ${mariadb}")
  </code></pre>
  
  <p>Output of which is:
  <code>BOOM /usr/local/lib/libmariadb.dylib</code></p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>I'm now linking to the following. Note that I started with just libmysqld and added libraries until all link errors went away. The trouble here is I may not have all the correct libs or versions.</p>
  
  <pre><code>TARGET_LINK_LIBRARIES(sql_fn /usr/local/lib/libmysqld.a)
  TARGET_LINK_LIBRARIES(sql_fn /usr/local/opt/openssl/lib/libcrypto.a)
  TARGET_LINK_LIBRARIES(sql_fn /usr/local/opt/openssl/lib/libssl.a)
  TARGET_LINK_LIBRARIES(sql_fn /usr/local/opt/bzip2/lib/libbz2.a)
  TARGET_LINK_LIBRARIES(sql_fn /usr/local/lib/liblz4.a)
  TARGET_LINK_LIBRARIES(sql_fn /usr/local/opt/zlib/lib/libz.a)
  TARGET_LINK_LIBRARIES(sql_fn /usr/local/opt/xz/lib/liblzma.a)
  TARGET_LINK_LIBRARIES(sql_fn /usr/local/lib/libsnappy.a)
  </code></pre>
  
  <p>It now compiles but exits with code 6
  <a href="https://i.stack.imgur.com/gK3AF.png" rel="noreferrer"><img src="https://i.stack.imgur.com/gK3AF.png" alt="CLion Debugger of exit stack"></a></p>
  
  <p><code>Process finished with exit code 6</code>
  Looking at <a href="https://stackoverflow.com/a/7495907/400048">https://stackoverflow.com/a/7495907/400048</a> if it's points to the same thing/is still true then exit code 6 means <code>EX_ILLEGAL_TABLE 6</code>, unfortunately I don't know what table that would be. The <code>mysql_test</code> and datadir strings passed in are valid identifiers/path. </p>