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

本秂侑毒 提交于 2019-12-03 03:29:20

问题


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.

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

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

what I am missing or misplacing in command?


回答1:


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

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

There's no space between the flag -p and the password.




回答2:


You can take backup using mysqldump and restore with mysql using commandline.

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"



回答3:


You can backup you MYSQL database structure with

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 -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

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

All tables in will be created in new_database without data.




回答4:


Try this one:

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



回答5:


From 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:

$ 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:

$ 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.

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:

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


来源:https://stackoverflow.com/questions/14107919/copy-database-structure-without-data-in-mysql-with-empty-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!