The following articles mainly introduce the basic knowledge of MySQL database backup. We all know that MySQL database is widely applied, so the practical application of MySQL database backup becomes extensive, the following is a description of the specific content. I hope it will be helpful for your future study.
When using MySQL databases, data is often lost due to operation errors. MySQL database backup can help us avoid data loss or other database problems caused by various reasons.
I. Data Backup shortcuts
Because this method has not been verified in the official documentation, we call it a test.
Objective: To back up a MySQL database in the hostA host and restore it to the hostB
Test environment:
Operating System: WinNT4.0, Mysql3.22.34, and phpMyAdmin 2.1.0
Install MySQL database backup in hostA and create TestA Database
MySQL database backup is installed on the hostB, and no TestA database exists.
Procedure:
Start phpMyAdmin to check the database list in HostA and HostB. No TestA database exists in HostB.
Find the installation directory of MySQL database backup in HostA, and find the database directory data
In my test environment, the directory is C: mysqldata
Find the subdirectory C: mysqldataTestA of the corresponding database name
Paste and copy the file to the Data directory of HostB, which is the same as that of the backup Data directory of the HostB MySQL database.
Refresh the phpMyAdmin of HostB and check the database list. We can see that TestA has appeared, and the query and modification operations are normal. The Backup recovery is successful.
Test conclusion: MySQL databases can be stored in the form of files, backup, recovery as long as the corresponding file directory can be restored, no need to use other tools to back up.
Ii. Formal method (officially recommended ):
Export the mysqldump tool that uses MySQL database backup. The basic usage is:
- mysqldump [OPTIONS] database [tables]
If you do not specify any tables, the entire database will be exported.
Run mysqldump -- help to obtain the option table supported by your mysqldump version.
Note: If you run mysqldump without the -- quick or -- opt option, mysqldump loads the entire result set to the memory before the export result. If you are exporting a large database, this may be a problem.
Mysqldump supports the following options:
-- Add-locks
Add lock tables and unlock table before each TABLE is exported. (To make it faster to insert data to the MySQL database for backup ).
Step 3: Create a master-slave relationship
First, my. in the cnf file, add log-bin to the [mysqld] section, restart mysqld, and create a user account on which the copy function can be executed. Use:
Grant file on *. * TO replicate@10.1.1.1 identified by password;
Run the flush privileges command on machine B to load the new authorization table after the user is copied. Then, return to machine A and add the following lines to its my. cnf:
- master-host=10.1.1.2
- master-user=replicate
- master-password=password
After restarting the service program of host A, we now have A master-slave relationship between host A and host B. No matter which server updates a record or inserts a record, it will be copied to another server. Note: I am not sure how fast a slave machine can merge binary logs. Therefore, using this method for load balancing of insert or update statements may not be a good solution.