How to copy a database in MySQL?

To copy a MySQL database, you can use the following method:

  1. Export a backup file of the original database using the mysqldump command.
mysqldump -u [用户名] -p [密码] [原数据库名] > [备份文件名].sql

For example:

mysqldump -u root -p mydatabase > mydatabase_backup.sql
  1. Create a new database.
mysql -u [用户名] -p [密码] -e "CREATE DATABASE [新数据库名]"

For example:

mysql -u root -p -e "CREATE DATABASE mydatabase_copy"
  1. Import the backup file into the new database.
mysql -u [用户名] -p [密码] [新数据库名] < [备份文件名].sql

“For example”

mysql -u root -p mydatabase_copy < mydatabase_backup.sql

This completes the process of copying the original database to the new database.

bannerAds