Qt World Summit: Register Today!

Embedded MySQL server

  • Hey guy, does somebody have experience in setting up an embedded MySQL server with Qt and can show some code how its done?

  • As far as I understood you just link to the libmysqld and start it by calling mysql_library_init() (end it before application exit with mysql_library_end()). For every thread that likes to access any mysql-function (so any QSql* function) call the corresponding thread init mysql_thread_init() (and mysql_thread_end()).

    The rest should behave as it has been with a default mysql server.

  • I tried this code from the MySQL reference but it crashes when it comes to the point mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client");. Does somebody has a working exmaple for a embedded server?

    #include <mysql.h>
    static char *server_options[] = {"mysql_test", "--defaults-file=my.ini", NULL};
    int num_elements = (sizeof(server_options) / sizeof(char *)) - 1;
    static char *server_groups[] = {"libmysqld_server", "libmysqld_client", NULL};
    int main(int argc, char *argv[])
        QSqlDatabase mydb;
        MYSQL *mysql;
        mysql_library_init(num_elements, server_options, server_groups);
        mysql = mysql_init(NULL);
        mysql_options(mysql, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client");
        mysql_options(mysql, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);
        mysql_real_connect(mysql, NULL, NULL, NULL, "database1", 0, NULL, 0);
        mydb = QSqlDatabase::addDatabase("QMYSQL", "con1");
            qDebug() << mydb.lastError();
        return 0;

  • Is really nobody out there who got this working?

  • Lifetime Qt Champion


    Seems that not that many try the MYSQL embedded.
    Even on
    No ones answers.

    And you did link with the libmysqld ?

  • Hi,

    Yes I did. As I figured out using the embedded server doesn't make sense anyway in my application as it is multithreaded and the embedded server supports only one connection at a time. I don't mark it as solved since I am never the less interested how this works out.

  • Lifetime Qt Champion

    Strange it would just crash then.
    mysql_init did return "im happy" etc?

    Well multi threaded could still work using a semaphore to control write access but
    if you need concurrent access its of no use.

    Sadly I think the question will just slowly drift to other pages and no one will ever see it again.

  • Lifetime Qt Champion


    AFAIK, you don't have to fiddle with raw MySQL c++ calls. You should be able to use Qt the exact same way as you would with a classic client server setup

  • Note on https://dev.mysql.com/doc/refman/5.7/en/libmysqld.html says: “The libmysqld embedded server library is deprecated as of MySQL 5.7.17 and will be removed in MySQL 8.0.“

Log in to reply