Dumping All MySQL Tables into CSV Format with Mysqldump
When faced with the task of extracting all database tables into CSV format, mysqldump offers a convenient solution. However, the default functionality only allows you to dump individual tables at a time.
To dump all tables, a comprehensive approach is required. Here's how to do it:
Convert Individual Table Data to CSV Format:
mysql -B -u username -p password database -h dbhost -e "SELECT * FROM tablename;" \ | sed "s/\"/\"\"/g;s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g"
This command extracts data from a specific table named 'tablename' using MySQL and formats it into CSV.
Generate a List of All Tables:
mysql -u username -ppassword dbname -sN -e "SHOW TABLES;"
This command retrieves a list of all tables in the database and stores it in a variable.
Iterate Over Tables and Export CSV Data:
Using a loop, iterate over the list of tables and append the previously mentioned 'Convert Table Data to CSV Format' command.
for tb in $(mysql -u username -ppassword dbname -sN -e "SHOW TABLES;"); do echo .....; done
Replace '.....' with the 'Convert Table Data to CSV Format' command, ensuring to substitute 'tablename' with '$tb'.
Redirect Output to a CSV File:
Append ' > outfile.csv' to the end of the loop command to direct the output into a CSV file.
By executing this comprehensive solution, you can efficiently dump all MySQL tables into CSV format, providing a convenient export option for your data analysis and processing needs.
The above is the detailed content of How Can I Efficiently Dump All MySQL Tables into CSV Format Using `mysqldump`?. For more information, please follow other related articles on the PHP Chinese website!