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.
Related
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!
I have created a batch script which is supposed to execute the SQL file in the same directory and then pause, where I connect to MYSQL and issue the commands inside of the SQL file. For some reason when I directly paste these lines into a command window, it works fine. When I have it setup as follows, I receive an Access Denied error. Can anyone please point out what I am doing wrong?
BAT File:
cmd < script.sql
pause
Script.SQL file
#echo off
mysql --host=localhost --user=dbuser --password=dbpassword --database=dbname
SELECT `SERVER_ID`
FROM tc_services
WHERE `GAME_ID` LIKE '%TC510254610%'
INTO OUTFILE 'D:\\Program Files (x86)\\TCAdmin Control Panel\\ScheduledTasks\\GAME\\ServerID.txt'
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n';
exit
You have the files organized incorrectly. Please redo the file as follows:
BAT File:
#echo off
mysql --host=localhost --user=dbuser --password=dbpassword --database=dbname < script.sql
pause
script.sql file
SELECT `SERVER_ID`
FROM tc_services
WHERE `GAME_ID` LIKE '%TC510254610%'
INTO OUTFILE 'D:\\Program Files (x86)\\TCAdmin Control Panel\\ScheduledTasks\\GAME\\ServerID.txt'
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n';
Give it a Try !!!
You did not say what operating system you are using. It looks like you are trying to write your output file to the "\Program Files (x86)" directory. As a security measure Windows does not allow a user to write to that file area. You will need to go to the security settings of the "D:\Program Files (x86)\TCAdmin Control Panel\ScheduledTasks\GAME\" folder and make sure the user running the bat/script file has write access to that folder.
I am trying to import a CSV file from to a mysql database from the command line. This will be later incorporated into a Windows batch file.
mysqlimport -u user -puserpw --columns=ID,CID,Alerted --fields-terminated-by=',' --local School Customer.csv
All the data loads into the first column in the Customer table.
I want to correctly import data from the CSV to the appropriate column.
CSV Data format:
ID,CID,Alerted
1,CS,N
2,CS,N
3,CS,N
I would like to use mysqlimport since this will be easier to add to a Windows batch file.
How can I do this please help?
I guess you have to add --lines-terminated-by='\n' with mysqlimport. I run a quick test here and worked.
mysqlimport --local --ignore-lines=1 --fields-terminated-by=',' --lines-terminated-by='\n' db_name table_name.csv
I had to enclose the values in double quotes "
mysqlimport -u user -puserpw --columns=ID,CID,Alerted --ignore-lines=1 --fields-terminated-by="," --lines-terminated-by="\n" --local School Customer.csv
That fixed it.
Try to use the complete description for importing the csv file like :
fields terminated by ','
enclosed by '"'
lines terminated by '\n'
Thanks
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
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.