Built-in MariaDB C / C ++ API

I am trying to run the MariaDB firmware (i.e., not connecting to a running server), but I cannot get any of the examples that I find to work.

The most recent example from this post: stack overflow

When the application starts, it produces: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) Documents https://mariadb.com/kb/en/library/embedded-mariadb-interface/ help a lot with this.

For convenience, the code from this StackOverflow post:

 #include <my_global.h> #include <mysql.h> 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 -> 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); } 

I had a quick look at https://github.com/MariaDB/server , but he didn’t know where to really look ... or actually what I was looking for. How do you go about getting a built-in mariad? I work on Mac OS High Sierra, MariaDB was installed with brew install mariadb --with-embedded .

UPDATE:

I am sure that I am attached to the correct library.

ls /usr/local/lib | grep maria

MariaDB

 FIND_LIBRARY(mariadb mariadb) MESSAGE(FATAL_ERROR "BOOM ${mariadb}") 

Output: BOOM /usr/local/lib/libmariadb.dylib

UPDATE 2

Now I refer to the following. Note that I started with libmysqld and added libraries until all the link errors went away. The problem here is that I may not have all the correct libraries or versions.

 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) 

Now it compiles, but exits with code 6 CLion debugger from exit stack

Process finished with exit code 6 If you look at https://stackoverflow.com/a/16665/ ... if it points to the same / is still true, then exit code 6 means EX_ILLEGAL_TABLE 6 , unfortunately I don't know which the table would be The mysql_test strings mysql_test and datadir are valid / path identifiers.

+7
c mysql mariadb
source share
1 answer

Ok, first explain your example a bit. The user uses gcc to compile, and I see that you are using cmake. First, which means -lz and mysql_config --include --libmysqld-libs . The first is the zlib link for the zlib link in cmake, you can reference this answer , but a short short history:

 find_package( ZLIB REQUIRED ) if ( ZLIB_FOUND ) include_directories( ${ZLIB_INCLUDE_DIRS} ) target_link_libraries( sql_fn ${ZLIB_LIBRARIES} ) endif( ZLIB_FOUND ) 

Then you need the mariaDB library, and this is the second part. mysql_config --include --libmysqld-libs this means running the mysql_config --include --libmysqld-libs command, which will return a string with link parameters, so run the command:

 $mysql_config --include --libmysqld-libs -I/usr/local/mysql/include -L/usr/local/mysql/lib -lmysqld 

And you should get a result similar to the above. -I - look for headers in a given directory, and -L - look for a library in a directory, -l - link this library, it serves the same purpose as -lz, only you add -lmysqld.

Well, now that everything is explained, you only need to include the -I -L and -l options with mysql, but this is not such a standard library, so you need to include directories and libraries through a script as described in this announcement . So, again, a long story, for this there is no bulletproof text, for example, my library is in / usr / local / mysql / lib, and yours is in / usr / local / lib. Since this is the case, it will be easier to use the second method.

 execute_process(COMMAND mysql_config --include OUTPUT_VARIABLE MYSQL_INCLUDE) execute_process(COMMAND mysql_config --libmysqld-libs OUTPUT_VARIABLE MYSQL_LIBS) target_compile_options(sql_fn PUBLIC ${MYSQL_INCLUDE}) target_link_libraries(sql_fn ${MYSQL_LIBS}) 

And that’s all you need. Now we are glad that we have Cmake to make our task easier, isn't it;;) Here is my CMakeLists.txt

 cmake_minimum_required(VERSION 3.6) project(embedded_mysql) set(CMAKE_CXX_STANDARD 14) set(SOURCE_FILES main.cpp) add_executable(embedded_mysql ${SOURCE_FILES}) find_package( ZLIB REQUIRED ) if ( ZLIB_FOUND ) include_directories( ${ZLIB_INCLUDE_DIRS} ) target_link_libraries( embedded_mysql ${ZLIB_LIBRARIES} ) endif( ZLIB_FOUND ) execute_process(COMMAND mysql_config --include OUTPUT_VARIABLE MYSQL_INCLUDE) execute_process(COMMAND mysql_config --libmysqld-libs OUTPUT_VARIABLE MYSQL_LIBS) string(STRIP ${MYSQL_LIBS} MYSQL_LIBS) target_compile_options(embedded_mysql PUBLIC ${MYSQL_INCLUDE}) target_link_libraries(embedded_mysql ${MYSQL_LIBS}) 

You can see the code on github here

+4
source share

All Articles