Detailed the Replication MySQL from MySQL database is provided a method of synchronizing the main

MySQL synchronization process is as follows: 

     1, the main server (master) will change events (updates, deletes, changes in the table structure, etc.) written to the binary log (master log). 

     2, obtained from the server (slave) IO thread from the primary server (binlog dump thread) binary log, and save a copy of your binary log (relay log) in the local 

     3, read the local log (relay log) from the SQL Server thread, and a repeat of change events.

MySQL master-slave synchronization of action: 

     1, can be used as a backup mechanism, it corresponds to hot backup (from backup, to avoid impacting the primary service during backup) 

     2, can be used for separate read and write, load balancing database (master write from read) 

     3, there is a problem if the primary server, the server can be switched from.

MySQL master-slave synchronization steps:

First, the preparatory actions: 

1, from the same master database version, version 5.5 is recommended 

2, the data from the database main coincidence 

 

Second, the primary master database modifications: 

1, modify the MySQL configuration:

Copy the code
 1 # /etc/my.cnf 
 2 log-bin = mysql-bin 
 3  
 4 # 主数据库端ID号 
 5 server-id = 1 
 6  
 7 log-bin=/home/mysql/logs/binlog/bin-log
 8 max_binlog_size = 500M
 9 binlog_cache_size = 128K
10 binlog-do-db = adb
11 binlog-ignore-db = mysql
12 log-slave-updates
13 expire_logs_day=2
14 binlog_format="MIXED"
Copy the code

The configuration of each parameter in the above meaning and items related note:

Copy the code
Server flag number # 1, note that a plurality of such identification can not appear in the configuration file, if a plurality of first words appear mysql to prevail, from which a set of main identification number can not be repeated here. 
 Server-ID. 1 = 2 
 . 3   
 . 4 = log-bin / Home / MySQL / logs / the binlog / open the binlog # binlog, and specify the file directory and file name prefix. 
 . 5   
 6 # The maximum size of each bin-log, when this size is equal to 500M automatically generates a new log file. Not write a record in two log files, so sometimes the log file exceeds this size. 
 Max_binlog_size = 500M 7 
 8   
 9 binlog_cache_size log cache size = 128K # 
10   
11 binlog-do-db = database name adb # need to be synchronized, if it is more, you can write a line in this format. 
12   
13 binlog-the ignore-db = # MySQL database does not require synchronization name, if it is more, you can write a line in this format. 
14   
15 # updated when Slave reads the log from the new Master database written to the log, if only start log-bin but does not start log-slave-updates the Slave records only for their own database update operations. 
Updates log-Slave-16 
. 17   
18 expire_logs_day = number of days to keep the bin-log log file # 2 is set, this version does not support the following parameters mysql5.0.
. 19   
20 is binlog_format = "MIXED" Set # bin-log log file format: MIXED, duplicate primary key can be prevented.
Copy the code

 

2, restart mysql, create an account for synchronization:
# 1 Create a slave account slave, password 111111 
. 2 MySQL> Grant Replication slave ON * * to 'slave' @ '%' IDENTIFIED by '111111'; 
3   
4 # update the database permissions 
5 mysql> flush privileges;

3, query the status of master

Copy the code
1 mysql> show master status; 
2 +------------------+----------+--------------+------------------+ 
3 | File    | Position | Binlog_Do_DB | Binlog_Ignore_DB | 
4 +------------------+----------+--------------+------------------+ 
5 | mysql-bin.000009 |  196 |    |     | 
6 +------------------+----------+--------------+------------------+ 
7 1 row in set
Copy the code

NOTE: After this step is not to the operation of the master database, the master database to prevent the state value

Third, modify the database slave:

1, modify the MySQL configuration:

# 1 terminal ID number from the database 
2 server-id = 2

2, synchronous command execution

# 1 for performing synchronous commands, IP, the master database, account password synchronization, the synchronization position 
2 mysql> change master to master_host = '192.168.1.2', master_user = 'slave', master_password = '111111', master_log_file = 'mysql-bin. 000009 ', MASTER_LOG_POS = 196; 
. 3   
. 4 # open sync 
5 mysql> start slave;

3, check the state from the database:

Copy the code
 1 mysql> show slave status\G; 
 2 *************************** 1. row *************************** 
 3     Slave_IO_State: Waiting for master to send event 
 4      Master_Host: 192.168.1.2 
 5      Master_User: slave_account 
 6      Master_Port: 3306 
 7     Connect_Retry: 60 
 8     Master_Log_File: mysql-bin.000009 
 9    Read_Master_Log_Pos: 196 
10     Relay_Log_File: vicky-relay-bin.000002 
11     Relay_Log_Pos: 253 
12   Relay_Master_Log_File: mysql-bin.000009 
13     Slave_IO_Running: Yes 
14    Slave_SQL_Running: Yes 
15     Replicate_Do_DB: 
16    Replicate_Ignore_DB: 
17    ... 
18    ...
Copy the code

Note: Slave_IO_Running and Slave_SQL_Running process must run properly, that is YES status, otherwise your synchronization failed. Both can be used to determine whether to hang from the server

Here, the main work from the database setup has been completed, they can create a new database and tables, insert and modify data, test to see if successful

Four other relevant parameters may be used:

1, master side:

Copy the code
# 1 which is not synchronized database 
 2 the binlog-DB = the ignore-MySQL 
 . 3-DB = the ignore the binlog-Test 
 . 4 the binlog-INFORMATION_SCHEMA the ignore-DB = 
 . 5   
 . 6 # which database synchronization only, in addition, other sync 
 7 binlog-do Game = -db 
 . 8   
 . 9 # log retention time 
10 expire_logs_days = 10 
. 11   
12 is controlled # binlog writing frequency. How many times have each executed transactions into a 
13 # This performance parameters consume a lot, but can reduce the losses caused by the collapse of MySQL 
14 sync_binlog = 5 
15   
16 # log format, the proposed Mixed 
17 # of Statement save SQL statements 
18 # row KEPT record data 
19 # mixed combination of the first two 
20 binlog_format = mixed
Copy the code

2、slave端

Copy the code
A master-slave synchronization stop # 
2 MySQL> STOP Slave; 
. 3   
when No.4 disconnection, reconnection timeout 
. 5 MySQL> Change Master to master_connect_retry = 50; 
. 6   
. 7 open master-slave synchronization # 
8 mysql> start slave;
Copy the code

Connection timeout provided above, can be used in a similar manner, the master database IP, account password synchronization, synchronization position

Analyzing the primary server is running the code

Copy the code
// In the following statements from the server 
$ SQL = "Show Slave Status"; 
$ Result = the mysql_query (SQL $, $ slave_link); 
$ Row = mysql_fetch_assoc ($ Result); 
 
$ Slave_IO_Running Row = $ [ 'Slave_IO_Running']; 
Row Slave_SQL_Running = $ $ [ 'Slave_SQL_Running']; 
 
// the following two criteria for the 
IF ( 'Yes' == $ Slave_IO_Running && 'Yes' == $ Slave_SQL_Running) { 
 
} the else { 
 $ Content = "from the database ($. host) a hung up !!! "; 
}
Copy the code

to sum up

That's all for this article, I hope the contents of this paper, we study or work can bring some help, if in doubt you can leave a message exchange

Guess you like

Origin www.cnblogs.com/HKROnline-SyncNavigator/p/10972572.html