Mysql Hot Backup --- Master/Slave Mode Note: Please indicate the source for reprint. The mysql hot backup mechanism is used on the four servers of the previous company. One server can work even if it is down. The prevention is summarized as follows: 1. This blog article tests the XP system. Hot Backup mechanism can be used to achieve two Database Synchronization, master-slave mode (A-B), mutual backup mode (AB) function. My
Mysql Hot Backup --- Master/Slave Mode Note: Please indicate the source for reprint. The mysql hot backup mechanism is used on the four servers of the previous company. One server can work even if it is down. The prevention is summarized as follows: 1. This blog article tests the XP system. Hot Backup mechanism can be used to achieve two Database Synchronization, master-slave mode (A-B), mutual backup mode (A = B) function. My
Mysql hot backup-Master/Slave Mode
Note: Please indicate the source for reprinting.
The mysql hot backup mechanism is used on the four servers of the previous company. One server can work even if it is down. The prevention measures are summarized as follows:
1. In this blog article, we will test the XP system. The hot backup mechanism can be used to synchronize two databases, master-slave mode (A-> B), and mutual backup mode (A <=> B.
The settings of the mysql Database Synchronization replication function are reflected in the mysql configuration file.
2. Let's see how to configure data synchronization (A-> B ):
Assume that database A is the host (the synchronization service will be provided to database B, that is, data in database B comes from database ):
Machine:
IP = 192.168.1.101
Machine B:
IP = 192.168.1.102
(1). The database in machine A is as follows:
// Database
Create database backup_db;
USE backup_db;
Create table 'backup _ table '(
'Id' int (11) not null auto_increment,
'Name' varchar (20) character set utf8 not null,
'Sex 'varchar (2) character set utf8 not null,
Primary key ('id ')
) ENGINE = InnoDB default charset = latin1;
# In my. cnf (or my. ini) of machine A, configure:
Server-id = 1
Log-bin = c: \ mysqlback # synchronize the log file of the event
Binlog-do-db = backup_db # database that provides the data synchronization service
(2). The database in machine B is as follows:
// Database B
Create database backup_db;
USE backup_db;
Create table 'backup _ table '(
'Id' int (11) not null auto_increment,
'Name' varchar (20) character set utf8 not null,
'Sex 'varchar (2) character set utf8 not null,
Primary key ('id ')
) ENGINE = InnoDB default charset = latin1;
Note: the database structure of database A and database B must be the same; otherwise, synchronization cannot be formed.
# In machine B's my. cnf (or my. ini), configure:
Server-id = 2
Master-host = 192.168.1.101 # address of host
Master-user = ym # the user that host A provides to B needs to include database backup_db permissions.
Master-password = ym # Access password
Master-port = 3306 # port, MYSQL port of the host
Master-connect-retry = 60 # retry Interval 60 seconds
Replicate-do-db = backup_db # synchronized Database
(3) After completing the preceding configuration, grant the mysql DATA permission of A to B.
Machine:
Mysql> grant file on *. * to identifiedby 'ym ';
(4) restart the AB database, and then:
Machine B:
Mysql> slave start;
View synchronization configurations
Machine:
Mysql> show master status;
Machine B:
Mysql> show slave status;
(5) Insert some data into the backup_db.backup_table table in A to check whether the backup_db.backup_table table in B has synchronized data changes. If the data synchronization result is not displayed, that is, the synchronization fails, check the error (as shown below ).
When an error occurs *. err log file (you can find it in the mysql installation directory), the synchronization thread exits. Repeat Step 4 after the error is corrected ).
3. Bidirectional Hot Standby (A <=> B ):
The above (1)-(5) Steps by A-B bidirectional configuration.
1. Basic principles of asynchronous replication
MySQL supports one-way asynchronous replication after MySQL3.23.15. That is to say, one MySQL server acts as the Master database, one or more MySQL servers act as Slaves (slave database), and data is asynchronously replicated from the Master to Slaves. Note that this replication method is asynchronous, which is different from the synchronous replication Implementation of MySQL (this implementation is called MySQL Cluster and MySQL Cluster ).
When the master database is updated, the master database writes the SQL statement of the update operation to the binary log (Bin log) and maintains the index of a binary log file to facilitate log file rotation ). When the slave database starts asynchronous replication, the slave database starts two I/O threads, one of which connects to the master database and requires the master database to pass the binary log change part to the slave database, and write the returned logs to the local disk. Another thread is responsible for reading binary logs written locally and executing them locally to reflect this change. Earlier versions only enable one I/O thread during replication.
There are several key points to note:
-Bin log must be enabled for the master database. The master database and slave database must have a unique Server Id.
-The slave database must be clear about which Bin log file of the master database starts copying.
-The slave database can copy only the specified database or some data tables of the database from the master database.
-The name of the master database and slave database can be different, but the same name is recommended.
-The MySQL version of the master database and slave database must be consistent.
MySQL Database Data Synchronization Method
Set the IP address of the new master server.
First, run phpmyadmin or mysql command to truncate the database table content on the slave side. Delete the bin-log files. If innodb is used, delete the database files and recreate the table }, then, let the slave start or restart the MySQL service so that the synchronization is performed first, so that the error "Duplicate entry record" will not appear later,
After the synchronization is complete, export the complete data from the master. SQL file (InnoDB uses SQL to export, MyISAM should directly package the Directory), will export. SQL import slave should be OK