Search code examples
c++mysqldatabasewindowslibmysql

Creating triggers using embedded mysql C++


I am working with the libmysqld c library in a C++ application on windows in order to interface with an embedded mysql server i.e. a mysql server that is online for the lifetime of the process that embeds it. The application that creates the database uses a mysql .ini file for creating the datadir relative to the application directory instead of in the global mysql install folder e.g.

[libmysqld_server]
basedir=./
datadir=./Database

I can programatically create a trigger with no problem e.g.

status = mysql_query(mysql,
        "CREATE TRIGGER del_trigger AFTER DELETE ON table FOR EACH ROW\
        INSERT INTO otherTable (col1, col2) VALUES (OLD.col1, OLD.col2)\
        ");

    if (status == 0) {
        Log(DEBUG, "Initialize():  <%p> Delete Trigger creation passed ...", this);
    }
    else {
        Log(DEBUG, "Initialize():  <%p> Delete Trigger creation failed with error %s...", this, mysql_error(mysql));
    }

The problem I run into however is that when the trigger gets called, mysql will complain that the mysql.proc table does not exist because I do not have a mysql database inside my application specific datadir. I have tried copying the mysql folder from the installation directory in C:\Program Files\MySQL... but then I run into issues where mysql reports

Error:Cannot load from mysql.proc. The table is probably corrupted

The only advice I have seen related to the above error is to run the 'mysql_upgrade' command which does not seem to work for the case of an embedded database using its own datadir. I'm at the point where all of the tables are created and their respective triggers are setup but just can't get around this mysql.proc error.

UPDATE:

I am also seeing some inconsistent behavior here. My version of MySQL is "mysql-5.5.16-win32" and it comes with a mysql_embedded.exe binary that I can use to open up a console and point to the database files generated by my application when it isn't running. When I perform operations in the mysql_embedded.exe, the triggers work without issue (no 'mysql.proc is probably corrupted' errors). So it seems like only the libmysqld c api is having an issue with the mysql system tables.


Solution

  • The solution was as simple as verifying that the "mysql" database was the same version as mysql version embedded in libmysqld. I verified my client version info via the following:

    const char * version = mysql_get_client_info();
    

    This returned "5.1.44" instead of the "5.5.16" that I was expecting. Downloading the mysql ZIP archive for 5.1.44 and using the mysql database in the datadir fixed the issue that I was experiencing.