Home > Mobile >  MariaDB node.js script throws ER_LOCAL_INFILE_WRONG_FILENAME error when trying to load data into tab
MariaDB node.js script throws ER_LOCAL_INFILE_WRONG_FILENAME error when trying to load data into tab

Time:11-24

I have a node.js script which is connecting to a local MariaDB server, creating some tables, and then attempting to load some data into them by executing a SQL query like this:

LOAD XML LOCAL INFILE 'C:\\path\\to\\some_data.xml'
INTO TABLE some_data
ROWS IDENTIFIED BY '<data>';

The script fails with an error like:

SqlError: (conn=60, no: 45034, SQLState: 45034) LOCAL INFILE wrong filename. 'C:\path\to\some_data.xml' doesn't correspond to query LOAD XML LOCAL INFILE 'C:\\path\\to\\some_data.xml'
INTO TABLE some_data
ROWS IDENTIFIED BY '<data>';. Query cancelled. Check for malicious server / proxy

The node.js script code is like:

const pathToXmlFile = path.resolve(__dirname, '../some_data.xml')
   .replace(/\\/g, '\\\\');
   // .replace(/\\/g, '/'); 
// none of the above work, and neither does no escaping at all
query = `LOAD XML LOCAL INFILE '${pathToXmlFile}'
         INTO TABLE some_data
         ROWS IDENTIFIED BY '<data>';`;

I tried escaping the path with forward slashes (I'm running the script through Git Bash), the error remains the same, only the way the path is displayed changes. I have set permitLocalInfile: 'true' on the MariaDB client connection and local_infile=1 on both the [mysqld] and [client] sections of my.ini. The node.js connection user has the FILE privilege.

Node version is 16.13.0, MariaDB npm module version is 2.5.5, all running on the same machine with Windows 10 (MariaDB server too, on localhost).

The above query works perfectly fine when executed from HeidiSQL.

Any ideas on what I'm doing wrong?

CodePudding user response:

I've create a mariadb bug for that : https://jira.mariadb.org/browse/CONJS-181

As a workaround, you might use "/" in place of "\": using "C:/path/to/some_data.xml" won't be escaped avoiding this bug

CodePudding user response:

It seems that it's a bug with the mariadb node connector, I have opened an issue here:

https://github.com/mariadb-corporation/mariadb-connector-nodejs/issues/183

In the meantime, I have tried the mysql2 and mysql connectors, the first doesn't seem to support the permitLocalInfile: 'true' connection option, so I switch to the standard mysql connector and everything works fine (with a few changes in the code because of slight differences in method calling).

If the bug in the mariadb connector is fixed, I will update the answer here.

  • Related