MySQL transfer
MySQL is an open source relational database management system that is widely used in various Internet applications, enterprise-level applications, and personal websites. When using MySQL, sometimes you need to transfer or copy a database or table to another server. This article will introduce the methods and steps for MySQL migration.
1. Back up data
Before transferring MySQL, you must first back up the data to be transferred. There are many ways to back up data. Here are two common methods:
The syntax format for backing up data through the mysqldump command is as follows:
mysqldump -u root -p --databases database_name > backup.sql
Among them, -u root -p is the user name and password for logging into MySQL; --databases database_name is the name of the database to be backed up; > backup.sql is the storage location of the backup file.
After the backup is completed, you can view the backup file through the following command:
cat backup.sql
phpMyAdmin is a web-based MySQL administrator tool that can easily back up and restore MySQL databases. After opening phpMyAdmin, select the database to be backed up on the left, then click the "Export" tab, select the "Custom" method for backup, set the backup format and options, and finally click "Start Export".
After the backup data is completed, you can proceed to MySQL transfer.
2. Transfer the database
If MySQL is not installed on the target server, you need to install the MySQL server and client . You can install the MySQL server on the Ubuntu server through the following command:
sudo apt-get install mysql-server-5.7
After the installation is complete, you can check the status of the MySQL service:
systemctl status mysql
You can use the scp command to copy the backup file from the original server to the target server. The syntax format is as follows :
scp /path/to/backup.sql user@newserver:/path/to/backup.sql
Where, /path/to/backup.sql is the path of the backup file, user is the user name of the new server, newserver is the IP address or domain name of the new server, and /path/to/backup.sql is the storage location of the backup file.
Use the MySQL command line tool or the phpMyAdmin tool to restore the backup file.
The command to use the MySQL command line tool to restore the backup file is as follows:
mysql -u root -p
Among them, -u root -p is the user name and password for logging in to MySQL, < /path/to/backup.sql means importing the backup file into MySQL.
The method to use the phpMyAdmin tool to restore the backup file is: open the phpMyAdmin tool, select the database to be restored, select the "Import" tab in the left menu bar, select the backup file, and set the import options and format. Then click "Start Import".
3. Transfer tables
If you only need to transfer a specific table in a database, you can use the following method.
Create the corresponding database and tables on the new server. If the table to be transferred already exists in the original database, you can skip this step. Otherwise, you can use the following statement to create a table:
mysql -u root -p -e "use database_name; create table table_name like old_table_name;"
Where database_name is the name of the new database, old_table_name is the name of the table to be transferred in the original database, table_name is the name of the table to be created.
Use the following command to copy the table data in the original database to the new database:
mysqldump -u root - p database_name old_table_name | mysql -u root -p database_name -C new_table_name
Among them, -u is the MySQL login user name, -p is the MySQL login password, database_name is the name of the database to be transferred, and old_table_name is the original table name , new_table_name is the name of the new table.
Through this method, the table to be transferred can be copied from the original database to the new database.
Summary
MySQL transfer requires steps such as backing up data, installing MySQL, copying backup files to the new server, and restoring backup files. For different transfer needs, different technical means and methods can be adopted. During the transfer process, data integrity and accuracy need to be maintained to avoid anomalies or data loss.
The above is the detailed content of mysql transfer. For more information, please follow other related articles on the PHP Chinese website!