MySQL 在mysql中复制没有数据的数据库结构(有空表)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14107919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:00:17  来源:igfitidea点击:

copy database structure without data in mysql (with empty tables)

mysqlcopydatabase-schema

提问by Pramod

Is there any way to copy database structure without data in MySQL, so the new database will be the same as it is copied from, but with empty tables.

有没有什么方法可以在MySQL中复制没有数据的数据库结构,所以新数据库将与复制的相同,但表为空。

After getting some suggestions I tried the command, but I am getting syntax error, my username = rootand password = nothing. I guess the default one. I am trying following command,

得到一些建议后,我尝试了该命令,但出现语法错误,我的username = rootpassword = nothing. 我猜是默认的。我正在尝试以下命令,

mysqldump -u root -p -d xyz_db | mysql -u root -p -Dnew_db

what I am missing or misplacing in command?

我在命令中遗漏了什么或放错了地方?

回答by Raab

mysqldump -u user -ppass -d olddb | mysql -u user -ppass -D newdb

The new database must already exist. The -dflag in the mysqldump command prevents copying of data.

新数据库必须已经存在。-dmysqldump 命令中的标志可防止复制数据。

There's no space between the flag -pand the password.

标志-p和密码之间没有空格。

回答by Saharsh Shah

You can take backup using mysqldumpand restore with mysqlusing commandline.

您可以使用mysqldump进行备份,并使用命令行使用mysql进行恢复。

For backup database

用于备份数据库

$ mysqldump -u root-pPassword -P3309 --routines --no-data testdb > "d:\dbwithnodata.sql"

For restoration of database

用于恢复数据库

$ mysql -u root-pPassword -P3309 newdb < "d:\dbwithnodata.sql"

回答by Vinod Kumar

You can backup you MYSQL database structure with

你可以备份你的 MYSQL 数据库结构

mysqldump -u username –p  -d database_name > backup.sql

(You should not supply password at command line as it leads to security risks.MYSQL will ask for password by default.) And you can create create tables in database with

(你不应该在命令行提供密码,因为它会导致安全风险。默认情况下MYSQL会要求密码。)并且你可以在数据库中创建表

mysql -u username -p new_database < backup.sql

Now you can use pipe to give the output of first command as output for second one and you will no longer need backup.sql

现在您可以使用管道将第一个命令的输出作为第二个命令的输出,您将不再需要 backup.sql

mysqldump -u username –p  -d database_name|mysql -u username -p new_database

All tables in will be created in new_databasewithout data.

将在new_database没有数据的情况下创建所有表。

回答by Phoenix

Try this one:

试试这个:

$ mysqldump --no-data -h localhost -u root -p database_name > imported_db_name.sql

回答by lampwins

From electrictboolbox.com/mysqldump-schema-only:

electrictboolbox.com/mysqldump-schema-only

Dumping the database structure for all tables with no data Add the -d flag to signify that no data should be included in the output like so, where "mydatabase" is the name of the database to dump, and "someuser" is the login name used to connect to the database. The following command will dump the table structure for all tables in the specified MySQL database:

为所有没有数据的表转储数据库结构添加 -d 标志以表示不应在输出中包含任何数据,其中“mydatabase”是要转储的数据库名称,“someuser”是登录名用于连接数据库。以下命令将转储指定 MySQL 数据库中所有表的表结构:

$ mysqldump -d -u someuser -p mydatabase

The -d flag says not to include data in the dump. Alternatively you can use --no-data instead if you find that easier to remember:

-d 标志表示不在转储中包含数据。或者,如果您发现更容易记住,您可以改用 --no-data :

$ mysqldump --no-data -u someuser -p mydatabase

The -u flag indicates the username and the -p flag that a password will be supplied. After pressing you will be prompted for the password.

-u 标志表示用户名,-p 标志表示将提供密码。按下后,系统会提示您输入密码。

Alternatively, the password can be supplied on the command line, but there must be no space between the -p flag and the password. For example, if the password was "apples" do this:

或者,可以在命令行上提供密码,但 -p 标志和密码之间不能有空格。例如,如果密码是“apples”,请执行以下操作:

$ mysqldump --no-data -u someuser -papples mydatabase
$ mysqldump -d -u someuser -p mydatabase > mydatabase.sql # This will output to a sql file