创建数据库行的mysqldump [英] mysqldump with create database line
问题描述
我正在将文件移动到另一台计算机上,而我想做的一件事就是在MySQL数据库中传输数据.我想将数据库转储到.sql文件中,但是还要在文件中包含创建数据库db_name,这样,我只需要将文件导入mysql即可,而不必手动创建数据库.有办法吗?
I'm in the process of moving my files onto another computer, and one thing I would like to do is to transfer the data in my MySQL database. I want to dump the databases into .sql files, but also have the create database db_name including in the file, that way I just have to import the file to mysql without having to manually create the databases. Is there a way to do that?
推荐答案
默认情况下, mysqldump
始终在转储文件的开头创建CREATE DATABASE IF NOT EXISTS db_name;
语句.
By default mysqldump
always creates the CREATE DATABASE IF NOT EXISTS db_name;
statement at the beginning of the dump file.
[ EDIT ]关于mysqldump
文件及其选项的一些信息:
[EDIT] Few things about the mysqldump
file and it's options:
--all-databases
,-A
转储所有数据库中的所有表.这与使用--databases
选项并在命令行上命名所有数据库相同.
Dump all tables in all databases. This is the same as using the --databases
option and naming all the databases on the command line.
--add-drop-database
在每个CREATE DATABASE
语句之前添加一个DROP DATABASE
语句.该选项通常与--all-databases
或--databases
选项结合使用,因为除非指定了其中一个选项,否则不会写入CREATE DATABASE
语句.
Add a DROP DATABASE
statement before each CREATE DATABASE
statement. This option is typically used in conjunction with the --all-databases
or --databases
option because no CREATE DATABASE
statements are written unless one of those options is specified.
--databases
,-B
转储几个数据库.通常,mysqldump
将命令行上的名字参数视为数据库名称,并将其后的名字视为表名称.使用此选项,它将所有名称参数视为数据库名称. CREATE DATABASE
和USE
语句包含在每个新数据库之前的输出中.
Dump several databases. Normally, mysqldump
treats the first name argument on the command line as a database name and following names as table names. With this option, it treats all name arguments as database names. CREATE DATABASE
and USE
statements are included in the output before each new database.
--no-create-db
,-n
如果指定了--databases
或--all-databases
选项,此选项将禁止输出中包含的CREATE DATABASE
语句.
This option suppresses the CREATE DATABASE
statements that are otherwise included in the output if the --databases
or --all-databases
option is given.
前一段时间,实际上有一个类似的问题,询问在文件的开头(对于XML文件)是否没有这样的声明.链接到该问题的在这里.
Some time ago, there was similar question actually asking about not having such statement on the beginning of the file (for XML file). Link to that question is here.
所以回答您的问题:
- 如果要转储一个数据库,则应具有
mysqldump
语句中的--add-drop-database
选项. - 如果要转储多个数据库,则应使用该选项
--databases
或--all-databases
以及CREATE DATABASE
语法将被添加 自动
- if you have one database to dump, you should have the
--add-drop-database
option in yourmysqldump
statement. - if you have multiple databases to dump, you should use the option
--databases
or--all-databases
and theCREATE DATABASE
syntax will be added automatically
更多信息,请参见 MySQL参考手册
这篇关于创建数据库行的mysqldump的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!