Mostly, a dump of a db is wanted in SQL. In case of emergency import file. But some people do not comprehend SQL or their SQL doesn’t like your SQL and everything goes down the drain. So there is the CSV or Comma-separated values file. As the name says, it separates the values by commas (and more if needed). Since it’s so dead simple, you will get a lot of different tools and program languages that will make life easy for you to re-import or just search in it. Microsoft Excel and Openoffice Calc both can handle the format as well, so for a quick look, this will do very nicely.
But there is no simple –csv switch in mysqldump, your weapon of choice for these tasks. So here the command that will allow you to do what you are after:
mysqldump -p -u USER -T DIRECTORY --fields-enclosed-by=/" --fields-terminated-by=, DATABASE
So this is the short version, and here what it all means:
- -p : Asks for a password, as most users have one. If you don’t specify this on a database with a user/password, it will error
- -u USER : Replace USER with your actual username to connect to the database.
- -T DIRECTORY : This creates a tab delimited file. Not what we wanted but it’s the base we need.
- –fields-enclosed-by=/” : Will add ” characters around the fields. This will allow CSV implementations to find everything that fits together. You will need that backslash or it wont run.
- –fields-terminated-by=, : The so much sought after comma. This replaces the tab and puts a comma in its place, which, you guessed it, creates the CSV file.
- DATABASE : Well you know, the thing this is all about…
To actually be able to do it though, you will need the FILE privilege on this database. Armed with this, you should be able to do your CSV exports easy now.
使用MySQL导出CSV文件
本文介绍了一种通过MySQL命令行工具将数据库内容导出为CSV格式的方法。具体步骤包括设置必要的参数,如使用-p选项请求密码输入、指定用户名连接数据库、选择输出目录等,并通过特定的字段限定符和终止符确保CSV文件格式正确。
4945

被折叠的 条评论
为什么被折叠?



