How to migrate the MySQL database?
To migrate a MySQL database, you can follow these steps:
- Back up the original database: Before migrating, it is essential to back up the original database to prevent data loss. You can use the mysqldump command to export the database as an SQL file with the following command:
- Create a backup of the database named “数据库名” using the username “用户名” and password “密码”, saving it to a file named “备份文件.sql” using mysqldump.
- Replace the placeholders “username”, “password”, and “database name” with actual values, and replace “backup file.sql” with the specified backup file name.
- Install MySQL on the target server: Make sure that MySQL is already installed on the target server and that the version matches the original database.
- Transfer the backup files to the target server: You can use the scp command to transfer the backup files from the original server to the target server, the command is:
- Copy the backup file.sql to the target server’s file path with the target server’s username and IP address.
- Replace “backupfile.sql” with the actual backup file name, “target server username” with the username on the target server, “target server IP address” with the IP address of the target server, and “target server file path” with the path of the backup file on the target server.
- Restore the database on the target server: Log in to the MySQL database on the target server and create a new database using the following command:
- Create a new database with the name 新数据库名;
- Then use the following command to import the backup file into the new database:
- mysql -u username -p password new_database_name < backup_file.sql
- Replace the “username”, “password”, and “new database name” with the actual values, and replace “backup file.sql” with the name of the backup file transferred to the target server.
- Verify migration success: log into the MySQL database on the target server and check if the new database contains the data and table structure from the original database.
This completes the migration of the MySQL database. It is important to ensure that the versions of the source and target databases are compatible during the migration process, and to back up the data before migrating to prevent any loss of data.