Brief introduction
MySQL master-slave synchronization is more widely used database architecture, technology is relatively mature, the configuration is not complicated, especially for relatively large load site, master-slave synchronization database to read and write can effectively alleviate the pressure.
MySQL master-slave synchronization mechanisms:
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:
# /etc/my.cnf
log-bin = mysql-bin
End of the primary database ID number #
server-id = 1
log-bin=/home/mysql/logs/binlog/bin-log
max_binlog_size = 500M
binlog_cache_size = 128K
binlog-do-db = adb
binlog- ignore -db = mysql
log-slave-updates
expire_logs_day=2
binlog_format= "MIXED"
And the meaning of each parameter related to note that the above configuration items: # server flag number, more attention to such identification can not appear in the configuration file, if a plurality of words appear mysql with the first subject, a group from which this primary identification number It can not be repeated.
server-id = 1
log-bin = / home / mysql / logs / binlog / binlog # open binlog, and specify the file directory and file name prefix.
# Each bin-log maximum size when this size is equal to 500M will automatically generate a new log file. Not write a record in two log files, so sometimes the log file exceeds this size.
max_binlog_size = 500M
binlog_cache_size = 128K # log cache size
binlog-do-db = database name adb # need to be synchronized, if it is more, you can write a line in this format.
binlog-ignore-db = mysql # database name do not need to sync, if it is more, you can write a line in this format.
# Update 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.
log-slave-updates
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.
binlog_format = "MIXED" # Set bin-log log file format: MIXED, duplicate primary key can be prevented.
2, restart mysql, create an account for synchronization:
# Create a slave account slave, password 111111
mysql>grant replication slave on *.* to 'slave'@'%' identified by '111111';
# Update the database permissions
mysql>flush privileges;
3, query the status of master
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000009 | 196 | | |
+------------------+----------+--------------+------------------+
1 row in set
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:
# Terminal ID number from the database
server-id =2
2, synchronous command execution
# Sync command execution, set the primary database ip, account password synchronization, synchronization position
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;
# Enable Sync
mysql>start slave;
3, check the state from the database:
mysql> show slave statusG;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.2
Master_User: slave_account
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 196
Relay_Log_File: vicky-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000009
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
...
...
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:
# Which databases are not synchronized
binlog-ignore-db = mysql
binlog-ignore-db = test
binlog-ignore-db = information_schema
# Only synchronize database which, in addition, other non-synchronous
binlog-do-db = game
# Log retention time
expire_logs_days = 10
# Binlog write control frequency. How many times per execution once the transaction is written
# This parameter consume a great performance, but can reduce the losses caused by the collapse of MySQL
sync_binlog = 5
# Log format, the proposed mixed
# Statement holds the SQL statements
# Row Save influence the recorded data
# Mixed combination of the first two
binlog_format = mixed
2、slave端:
# Stop master-slave synchronization
mysql> stop slave;
# When you disconnect and reconnect timeout
mysql> change master to master_connect_retry=50;
# Enable master-slave synchronization
mysql> start slave;
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:
// In the following statement from the server
$sql = "show slave status";
$result = mysql_query($sql, $slave_link);
$row = mysql_fetch_assoc($result);
$Slave_IO_Running = $row['Slave_IO_Running'];
$Slave_SQL_Running = $row['Slave_SQL_Running'];
// The following two criteria for judging
if ('Yes' == $Slave_IO_Running && 'Yes' == $Slave_SQL_Running) {
} else {
. $ Content = "hung up from a !!! ($ Host) Database";
}
to sum up