Clone MySQL database

前端 未结 7 791
攒了一身酷
攒了一身酷 2020-12-12 13:37

I have database on a server with 120 tables.

I want to clone the whole database with a new db name and the copied data.

Is there an efficient way to do this?

7条回答
  •  再見小時候
    2020-12-12 14:18

    If you want to make sure it is an exact clone, the receiving database needs to be entirely cleared / dropped. This way, the new db only has the tables in your import file and nothing else. Otherwise, your receiving database could retain tables that weren't specified in your import file.
    ex from prior answers:

    DB1 == tableA, tableB
    DB2 == tableB, tableC
    DB1 imported to -> DB2
    DB2 == tableA, tableB, tableC //true clone should not contain tableC
    

    the change is easy with --databases and --add-drop-database (see mysql docs). This adds the drop statement to the sqldump so your new database will be an exact replica:

    $ mysqldump -h $ip -u $user -p$pass --databases $dbname --add-drop-database > $file.sql
    $ mysql -h $ip $dbname -u $user -p$pass < $file.sql
    

    of course replace the $ variables and as always, no space between password and -p. For extra security, strip the -p$pass from your command

提交回复
热议问题