Insert CSV to MySQL using Ubuntu Terminal via shell script - mysql

Is it possible to insert a CSV file into MySQL using a shell script in Ubuntu?
Here's what I tried :
mysql -uroot -proot mysfdb < /home/sf/data.csv
But I am given an error
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
Here's a sample content from the CSV file:
showinventory_SST312V8N4615041313_1366009574txt_,800-200002.A0,00007985
Any ideas?

Maksym Polshcha's answer is correct but is only missing a few things. Apparently, since it's a local file, I have to declare it as a local file in the mysql command. The final command should be something like this:
mysql -uroot -proot --local_infile=1 3parsfdb -e "LOAD DATA LOCAL INFILE '/logfiles/Bat_res.csv' INTO TABLE Bat_res FIELDS TERMINATED BY ','"
Also I made sure that the /logfiles directory and the Bat_res.csv are world readable.
Thank you for the great answers.

Try this:
mysql -uroot -proot mysfdb -e "LOAD DATA INFILE '/home/sf/data.csv' INTO TABLE mytable"
where mytable is your table for the data. If you have non-standard field/line separators in your CSV file use FIELDS TERMINATED BY and LINES TERMINATED BY
See http://dev.mysql.com/doc/refman/5.1/en/load-data.html

I used this and it worked.
Login in mysql using `mysql -uroot -ppassword --local-infile`
Then in terminal:
LOAD DATA LOCAL INFILE '.csv path' INTO TABLE table_name FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';

Open Ubuntu Terminal and just run the following command
# mysql -u admin -p --local_infile=1 DATABASE_NAME -e "LOAD DATA LOCAL INFILE 'students.csv' INTO TABLE TABLE_NAME FIELDS TERMINATED BY ',' enclosed by '\"'"
Here,
DATABASE_NAME = The name of your database
students.csv = The CSV file directory, that you want to upload in the database
TABLE_NAME = in which table you want to upload your data
admin = Database user name
After run this command system asked for the password of the admin user.
Write the password and Enjoy.

Related

Uploading data from csv to MYSQL Server using MYSQLSH

I need to upload data from CSV to my MYSQL Server, I've used mysqlsh to do it using jobs:
"C:\Program Files (x86)\MySQL\MySQL Shell\bin\mysqlsh.exe" --sql -h x.x.x.x -u user -password -D database -e "LOAD DATA LOCAL INFILE 'file.csv' INTO TABLE table FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n' IGNORE 1 ROWS (field1, field2)
But when i execute the command i got this error:
The used command is not allowed with this MySQL version
I readed that i need to set local_infile to TRUE, i've made and i can't do it
What I'm doing wrong?
You need to enable local_infile option on client side, as well. The only way to do that in MySQL Shell is to pass that option as connection option.
mysqlsh.exe mysql://user#x.x.x.x/database?local-infile=1 -e "LOAD DATA..."
You can get more information about connection options by calling mysqlsh -i -e "\? connection".
If you want to load big input CSV file, you can use MySQL Shell's Parallel data import.

How to export mysql table from the terminal

I'm trying to bypass the -secure-file-priv option that MySQL has enabled to export a table into a .csv file.
I've been running SELECT * FROM final_table INTO OUTFILE 'test.csv' FIELDS ENCLOSED BY '"' TERMINATED BY ';' ESCAPED BY '"' LINES TERMINATED BY '\r\n'; into the terminal, but it has been returning The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
Previously, I bypassed this error when importing files by running LOAD DATA LOCAL INFILE instead of LOAD DATA INFILE which had the same error and was wondering if there was a way to do it with exporting as well.
Thanks!
Answering my own question since I found a solution that might help out other people.
mysql -u root -p [database] -e '[SELECT * FROM TABLE_NAME]' > data_export.csv
got me exactly what I needed without disabling secure-file-priv!

import 500mb csv file into database using MySQL terminal

I am trying to import a csv file into mysql database using mysql terminal, nothing seems to work I either get error 1064 or nothing happens after I hit enter.
So far I have tried:
mysql -uusername -ppassword dbname -e "LOAD DATA INFILE 'C:/Users/Administrator/Downloads/file.csv' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' (field1, field2, field3)";
which does nothing if I hit enter without the ; at the end, and with the ; I get error 1064 incorrect syntax.
I have also tried:
mysql -u username -p password -h localhost dbname
use dbname
LOAD DATA INFILE 'C:/Users/Administrator/Downloads/file.csv' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '"' (field1, field2, field3);
is this correct?
I am using the statements below, and this is the error message I am receiving:
mysql->mysql -u username -ppassword
->use database
->LOAD DATA LOCAL INFILE 'C:/datafeed.csv'
->INTO TABLE table_name
->FIELDS TERMINATED BY ','
->LINES TERMINATED BY '\n'
->(field1, field2, field3);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql -u username -ppassword use database LOAD DATA LOCAL INFILE 'C:/datafe' at line 1
The syntax for the LOAD DATA statement itself looks correct...
it looks suitable if the MySQL server is installed on a Windows machine, and the specified file exists and is readable on the server. (There's no LOCAL keyword before INFILE, so that's saying that the file is on the server machine, not the client machine.)
The first thing you show you have tried, I think the double quote character within the LOAD DATA statement is actually terminating the string, that is, it's being "matched" to the double quote before the LOAD DATA.
The fact you are reporting that you are getting a 1064 error makes it appear you are able to connect to the MySQL server.
The form I use for running the mysql command line client on the server machine:
mysql -u user -ppassword database
(Note that there's no space between -p and the password.)
I'm a little confused, whether the MySQL server is actually running on a Windows machine, and if you're running in a CMD.EXE window... there's not enough information provided for me to determine that.

Exporting comma delimited data from a very large table

I am trying to get all the data from a very large table from a remote host having around 13million entries into a text file. I have tried the following command but after sometime process gets killed and shows a message called "Killed." in the console.
mysql --user=username --password -h host -e "select * from db.table_name" >> output_file.txt
My primary goal is to copy data from mysql to redshift, which I am doing it by getting all the data with "," delimited int a text file uploading it on s3 and executing COPY query on redshift.
P.S for small tables the above command is working properly but not for large tables.
You could try mysqldump instead. It can be parameterized to output CSV if I recall correctly. I haven't tried this myself, so you might want to check the docs, but this should work:
mysqldump --user=username --password -h host \
--fields-terminated-by="," --fields-enclosed-by="\"" --lines-terminated-by="\n" \
dbname tablename > output_file.txt
If that does not work, you could try SELECT INTO OUTFILE. You will need to do that directly on the MySQL host like this:
SELECT * INTO OUTFILE '/tmp/data.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\' LINES TERMINATED BY '\n'
FROM db.table_name

Mysql export issue?

I am using the following command to export thedabase,however i can't find the FILE.sql file after executing the command.Where is it stored?
mysqldump -u username -ppassword database_name > FILE.sql
Also how can i check my home directory , I have checked the program files(x86) mysql and respective bin folder in it.
The other way to export data to CSV file is by using "OUTFILE" syntax provided by MySQL like shown below.
[Usual MySQL Query]
INTO OUTFILE '/var/data_exports/huge_data.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
Read practical usage and more here. ResolveBug.com
It should be at either the root dir or in current dir where you run the dump command.