Last active
October 30, 2018 15:23
-
-
Save turboBasic/96497e158972476b8a00a0010466f1b2 to your computer and use it in GitHub Desktop.
Export table from MySQL database to Tab-separated value (TSV) file (bash/sh one-liner)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
DB_HOST=localhost | |
DB_NAME=database | |
DB_USER=user | |
DB_PASSWORD=p@ssw0rd | |
DB_TABLE=some_table | |
mysql --host=$DB_HOST --user=$DB_USER --password=$DB_PASSWORD --execute="select * from $DB_NAME.$DB_TABLE;" > sql_dump_$DB_NAME_$DB_TABLE.tsv | |
exit | |
# | |
# Using mysqldump: in case of error writing files, check the directory where MySQL is allowed to export files by | |
# SHOW VARIABLES LIKE "secure_file_priv"; | |
# and use it instead of /path/to/the/dir in the command below | |
# | |
# Both MySQL and user running command should be allowed to write to the directory /path/to/the/dir | |
# | |
mysqldump --host=$DB_HOST --user=$DB_USER --password=$DB_PASSWORD --tab=/path/to/the/dir \ | |
--fields-enclosed-by='"' --fields-terminated-by=',' --lines-terminated-by='\n' \ | |
$DB_NAME $DB_TABLE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment