QLands
QLands

Reputation: 2586

Use mysql embedded and --local-infile=1 with c++?

I am connecting to a mysql database using the embedding server (linking against mysqld) in c++. I have the following code:

static char *server_options[] = \
        { (char *)"mysql_test",
          (char *)"--datadir=/home/cquiros/temp/mysql/db2",
          (char *)"--default-storage-engine=MyISAM",
          (char *)"--loose-innodb=0",
          (char *)"--local-infile=1",
          (char *)"--skip-grant-tables=1",
          (char *)"--myisam-recover=FORCE",
          (char *)"--key_buffer_size=16777216",
          (char *)"--character-set-server=utf8",
          (char *)"--collation-server=utf8_bin",
          NULL };

        int num_elements = (sizeof(server_options) / sizeof(char *)) - 1;

        mysql_library_init(num_elements, server_options, NULL);
        m_mysql = mysql_init(NULL);

        char enable_load_infile = 1;
        if (mysql_options(m_mysql,MYSQL_OPT_LOCAL_INFILE, (const char *)&(enable_load_infile)))
            qDebug() << "Error setting option";

        mysql_real_connect(m_mysql, NULL,NULL,NULL, "database1", 0,NULL,0);

The connection works and I can query and create tables however, when I try to execute "load data local infile ..." I always get "The used command is not allowed with this MySQL version" even though I am setting --local-infile=1 in the server options or setting it in code in:

char enable_load_infile = 1;
        if (mysql_options(m_mysql,MYSQL_OPT_LOCAL_INFILE, (const char *)&(enable_load_infile)))
            qDebug() << "Error setting option";

Any idea what I am doing wrong and how to fix it?

Many thanks for your help. Carlos.

Upvotes: 1

Views: 825

Answers (1)

hypervisor666
hypervisor666

Reputation: 1275

@QLands I realize its over a year since you've asked this question, but I figured I'd reply just for posterity in case others like me are googling for solutions.

I'm having the same issue, I can get LOAD DATA LOCAL INFILE statements to work from the Linux mysql CLI after I explicitly enabled it in the /etc/mysql/my.cfg file. However I CANNOT get it to work with the MySQL C++ connector -- I also get the error "The used command is not allowed with this MySQL version" when I try and run a LOAD DATA LOCAL INFILE command through the MySQL C++ connector. wtf right?

After much diligent googling and finding some back alley tech support posts I've come to conclude that the MySQL C++ connector did not (for whatever reason) decide to implement the ability for developers to be able to allow the local-infile=1 option. Apparently some people have been able to hack/fork the MySQL C++ connector to expose the functionality, but no one posted their source code -- only said it worked. Apparently there is a workaround in the MySQL C API after you initialize the connection you would use this:

mysql_options( &mysql, MYSQL_OPT_LOCAL_INFILE, 1 );

Here are some reference articles that lead me to this conclusion:

1.) How can I get the native C API connection structure from MySQL Connector/C++?

2.) Mysql 5.5 LOAD DATA INFILE Permissions

3.) http://osdir.com/ml/db.mysql.c++/2004-04/msg00097.html

Essentially if you want the ability to use the LOAD DATA LOCAL INFILE functionality -- you have to use the mysql C API or execute it from the command line or hack/fork the existing mysql C++ api to expose the connection structure :(

Upvotes: 1

Related Questions