question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

After update mysql2, LOAD LOCAL file asks to stream

See original GitHub issue

When using mysql2 1.6.5 with TypeORM and running the query bellow, everything goes as expected, but after upgrade to 2.02 version, I get this error.

// CODE
this.connection.query(`
LOAD DATA INFILE '/home/data.csv' INTO TABLE studio_data
  FIELDS TERMINATED BY ',';
`)
// ERROR
As a result of LOCAL INFILE command server wants to read /home/data.csv file, 
but as of v2.0 you must provide 
streamFactory option returning ReadStream.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
calebeairescommented, Apr 18, 2020

Finally ORM: TypeORM - https://typeorm.io/ node-mysql2 VERSION: 2.1.0

Connection

const connection = createConnection({
                    name,
                    type: credentials.client,
                    host: credentials.host,
                    port: Number(credentials.port),
                    username: credentials.user,
                    password: credentials.password,
                    database: credentials.database,
                    logging: true,
                    dateStrings: true,
                    multipleStatements: true,
                    supportBigNumbers: true,
                    bigNumberStrings: false,
                    flags: ['+LOCAL_FILES'] // <======
                });

Query

return connection.query({
            sql: ` SET SESSION sql_mode = '';
                      LOAD DATA LOCAL INFILE "${localFileToImport}"
                      INTO TABLE ${taskData.resultTable}
                      ${characterSet}
                      ${fieldsTerminatedBy}
                      ${optionallyEnclosedBy}
                      ${linesTerminatedBy}
                      ${ignoreFistRow}
                     ${transform.columns}
                     ${transform.replace}`,
           values: [],
           infileStreamFactory: () => fs.createReadStream(localFileToImport)  // <======
        })

I have follow this example: https://github.com/sidorares/node-mysql2/blob/dbf48879f517b8ebefc9fbdc5508a20b84f833ec/test/integration/connection/test-load-infile.js#L26-L38

@scriptsure, maybe my example can help you

2reactions
scriptsurecommented, Apr 23, 2020

@sidorares thanks very much for helping me with this issue. Everything works great. Really appreciate it!!! 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

6.1.6 Security Considerations for LOAD DATA LOCAL
The LOAD DATA statement loads a data file into a table. The statement can load a file located on the server host, or,...
Read more >
Fix: Using Load Data Local Infile - MySqlConnector
MySQL Server supports a LOAD DATA command that can bulk load data from a CSV or TSV file. This normally loads data from...
Read more >
ERROR: Loading local data is disabled - this must be enabled ...
After searching online, I fixed it by these steps: ... mysql> use menagerie Database changed mysql> load data local infile '/path/pet.txt' ...
Read more >
How to Load Data From File to Table in MySQL? 3 Easy ...
mysql > LOAD DATA LOCAL INFILE '/path/product.txt' INTO TABLE ... ENABLE KEYS command after loading the file. ... ON DUPLICATE KEY UPDATE.
Read more >
MySQL load data infile ERROR 1064 - DBA Stack Exchange
Why dows you use LOCAL ? does your client works with remote MySQL, and source file is local for client and remote for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found