1

can someone please assist, I am new to mysql, and i have noticed that "LOAD DATA LOCAL INFILE" does not work in mysql event scheduler to update my databases from a normal .csv

So I'm trying to setup a "cron job" in linux to run a shell script to do the LOAD DATA INFILE to my databases, but im getting errors on the following shell script, please help correct it, see my script layout below...

#!/bin/bash
mysql -u root -p xxxxxxx testdb --local_infile=1 -e"LOAD DATA LOCAL INFILE '/mnt/mysqldb/mysqldb-new/mysql/CK-BATCH-FTP/Acelity/activity.csv' 
INTO TABLE acelity_activity
FIELDS TERMINATED BY ';'
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

Thank you for helping me

Graham Jansen
  • 25
  • 1
  • 7

3 Answers3

2

You must enclose the Double quotes in ENCLOSED BY and i think that you have a finalizing Double quote in you original code

#!/bin/bash 
mysql -u root -p xxxxxxx testdb --local_infile=1 -e"LOAD DATA LOCAL INFILE '/mnt/mysqldb/mysqldb-new/mysql/CK-BATCH-FTP/Acelity/activity.csv' 
INTO TABLE acelity_activity
FIELDS TERMINATED BY ';'
ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;"

And you should test the command, in wokbench or phpmyadmin

nbk
  • 45,398
  • 8
  • 30
  • 47
  • Thanks for the correction on the script, so i run it with no errors though but it did not import anything, after i run the script i got the following it just gave me the complete "help" guide for scripting features. I also did test the query (LOAD DATA LOCAL INFILE....) in phpmyadmin and it works. Ar you sure there should be a '\"' (back slash) at the ENCLOSED BY ? – Graham Jansen Jun 29 '20 at 06:55
  • yes pretty sure see https://unix.stackexchange.com/questions/30903/how-to-escape-quotes-in-shell and https://stackoverflow.com/questions/22137390/bash-script-for-load-data-infile-mysql – nbk Jun 29 '20 at 08:14
  • Thanks got it. So why would I get no error this time but the "help feature guide" after I run the script ? let me know if you need an upload of it ? – Graham Jansen Jun 29 '20 at 08:33
  • try to move --local_infile=1 to set global before the command, second, login(mysql.exe) and run the command, if that runs and the error log shows nothing, enable general log and see what the command actually looks like that reaches mysql server. you to check all the bases – nbk Jun 29 '20 at 09:11
0

Thanks for the help @nbk

So i modified the script above a little, and the following worked for me: (I removed the db name in the mysql syntax and add it in the query)

mysql -u root -pxxxxxxx -e"LOAD DATA LOCAL INFILE '/mnt/mysqldb/mysqldb-new/mysql/CK-BATCH-FTP/File/XXXX.csv' 
INTO TABLE <DB NAME>.<TABLE NAME>
FIELDS TERMINATED BY ';'
ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;"

Graham Jansen
  • 25
  • 1
  • 7
0

If you are using Mysql8 then this will help you.

mysql -udbuser -pXXXXX -h host-pc test1 --local_infile=1 -e "LOAD DATA LOCAL INFILE '/home/abc/data.csv' INTO TABLE tempTable FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' (col1, col2);";

Make sure there is no space in username and password i.e. it should be like this as mentioned in the above command.

Also ENCLOSED BY needs to write in the mentioned format only.

Atul
  • 3,043
  • 27
  • 39