A, mysql master-slave synchronization
1.1 What is the mysql master-slave synchronization
For data services from the main structure of the synchronization, the structure is divided into two roles
database server receives client access: the master server
from the server: automatically from the primary database server synchronization library server to the machine
1.2 mysql master-slave synchronization works
- Mster, recording data-changing operations
- Enable binlog log
- set binlog log format
- set server_id - Slave run two threads
Slave_IO host binlog master copy of the log file in SQL to relay-log file of the machine in
Slave_sql perform relay-log files in the native SQL statements, reproduce Master data manipulation
Configuring master-slave synchronization mysql
1.3.1 Server Roles
192.168.4.50 client (Client)
192.168.4.51 primary (master) server running a database administrator can root the machine landed
192.168.4.52 from (slave) running the database server administrator root landing 1.3.2 The machine can configure the primary server 192.168. 4.51
1 Enable binlog log file
vim /etc/my.conf
[mysq]
server_id=51
log-bin=master51
:wq
[root@51 ~]# systemctl restart mysqld
[root@51 ~]# ls /var/lib/mysql/master51.*
/var/lib/mysql/master51.000001 /var/lib/mysql/master51.index
[root@51 ~]# mysql -uroot -p123456 -e "show master status"
2 User Authorization
[root@51 ~]# mysql -uroot -p123456
mysql> grant replication slave on *.* to repluser@"%" identified by "123qqq...A";
3 View binlog log information (master status)
mysql> show master status;
1.3.3 configuration from the server 192.168.4.52
Specify the server_id 1 (the same as the primary database allowed the server_id)
vim /etc/mysql
[mysq]server_id=52
Information specify the master server 2
mysql> change master to
—> master_host="192.168.4.51", //指定主库IP
—> master_user="repluser", //指定主库授权用户
—> master_password="123qqq...A", //指定主库授权密码
—> master_log_file="master51.000002", //日志文件
—> master_log_pos=441; //偏移位置
3 start slave process
mysql> start slave;
Master information is automatically saved to /var/lib/mysql/master.info
To change the master information, stop slave STOP;
4 View process information (see slave state, confirmed IO thread, SQL thread is running)
mysql> show slave status\G;
Slave_IO_Running: Yes //查看IO线程是否出错
Slave_SQL_Running: Yes //查看SQL线程是否出错
Relevant documents from the library
[root@52 ~]# cat /var/lib/mysql/
master.info //主库信息
relay-log.info //中继日志信息
主机名-rely-bin.xxxx //中继日志
主机名-rely-bin.index//索引文件
1.4 verify the configuration
1 add user to access data on the master server connection
mysql> create database db7;
mysql> grant all on db7.* to webuser@"%" identified by "123qqq...A";
2 master client connection made to the data operation
[root@50 ~]# mysql -h192.168.4.51 -uwebuser -p"123qqq...A"
mysql> show grants;
mysql> use db7;
mysql> create table db7.t1(id int);
mysql> insert into db7.t1 values(10101);
mysql> insert into db7.t1 values(10101);
3 to check whether the same master server and from the server machine data
[root@52 ~]# mysql -uroot -p"Taren1.com"
mysql> show databases;
mysql> select * from db7.t1;
Two, mysql master-slave synchronization pattern
MYSQL master-slave synchronization pattern structure
A master-slave
A master multi-slave
The main server 51 to the current rearranged from the database server 1 53
1 run on the server 53 mysql service and the administrator can log in the machine
2 in the absence as slaves, to have data on the primary server
[root@51 ~]# mysqldump -uroot -p123456 db7 > /root/db7.sql
[root@51 ~]# scp /root/db7.sql [email protected]:/root
[root@53 ~]# mysql -uroot -pTaren1.com
mysql> create database db7;
mysql> use db7;
mysql> source /root/db7.sql;
3 Set the host server_id 53
vim /etc/my.conf
[mysqld]
server_id=53
[root@53 ~]# systemctl restart mysqld
4 specify the master server information
[root@53 ~]# mysql -uroot -pTaren1.com
mysql> change master to
master_host="192.168.4.51",
master_user="repluser",
master_password="123qqq...A",
master_log_file="master51.000002",
master_log_pos=1821;
5 Start slave process
mysql> start slave;
6 View Process
mysql> show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
7 client test
50 1 client host connection to access data and database services
[root@50 ~]# mysql -h192.168.4.51 -uwebuser -p"123qqq...A"
mysql> insert into db7.t1 values(8888888);
mysql> create table db7.t2(name char(10));
mysql> select * from db7.t1;
mysql> select * from db7.t2;
2, respectively, in the server machine 2 to view the data (as can be seen and master
data)
mysql> select * from db7.t1;
mysql> select * from db7.t2;
From the master-slave
The current from the server to 192.168.4.52, 192.168.4.54 configure a server from
52 1 Configuration
1.1 Enabling log binlog
vim /etc/my.cnf
log-bin=master52
log_slave_updates
[root@52 ~]# systemctl restart mysqld
[root@52 ~]# mysql -uroot -p"Taren1.com"
mysql> show slave status\G;
1.2 User Authorization
mysql> grant replication slave on *.* to repluser@"%" identified by "123qqq...A";
1.3 View binlog log information
mysql> show master status;
Configuration 2 54
52备份数据
[root@52 ~]# mysqldump -uroot -pTaren1.com db7 > /root/db7.sql
[root@52 ~]# scp /root/db7.sql [email protected]:/root/
54使用备份文件恢复数据
[root@54 ~]# mysql -uroot -pTaren1.com
mysql> create database db7;
mysql> use db7;
mysql> source /root/db7.sql
mysql> show tables;
2.1 指定server_id
vim /etc/my.cnf
server_id=54
[root@54 ~]# systemctl restart mysqld
2.2 specify the master server information
[root@52 ~]# mysql -uroot -p"Taren1.com"
mysql> grant all on db7.* to webuser@"%" identified by "123qqq...A";
[root@54 ~]# mysql -uroot -pTaren1.com
mysql> change master to master_host="192.168.4.52",master_user="repluser",master_password="123qqq...A",master_log_file="master52.000002",master_log_pos=732;
2.3 Starting slave process
mysql> start slave;
2.4 View process information
mysql> show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
3 test configuration
3.1 connecting the main server 51 in the client data memory
[root@50 ~]# mysql -h192.168.4.51 -uwebuser -p"123qqq…A"
mysql> insert into db7.t1 values(123456789);
3.2 can be seen from the server 52 and host computer 54 and the main server and data
[root@52 ~]# mysql -uroot -p"Taren1.com" -e “select * from db7.t1;”
[root@54~]# mysql -uroot -p"Taren1.com" -e “select * from db7.t1;”
The Lord structure
MYSQL master-slave synchronization pattern replication
Different synchronous mode
main library After you perform a transaction, immediately returns the results to the client, do not care whether it has received treatment from the library and
fully synchronous replication
after the completion of the main library to perform a transaction, and all from the library are executed the transaction before returning to the client
semi-synchronous replication
between asynchronous replication and fully synchronous replication
after the main library finished execute a transaction, wait at least one received from the library to the relay-log and writes it back to the client in
case: enable the database server 52 semi-synchronous replication mode
step:
1 to see if the module allows for dynamic loading
mysql> Show Variables like "have_dynamic_loading";
2 loading module
user needs to have super authority
mysql> install plugin rpl_sync_master soname "semisync_master.so "; // host library performed
mysql > install plugin rpl_sync_slave soname "semisync_slave.so" ; // execute the library from
3 to view the information load
MySQL> plugin_name the SELECT, the WHERE plugin_status from information_schema.plugins plugin_name like "the SEMI%%";
4 enabled module
mysql> set global rpl_semi_sync_master_enabled = 1 ; // main library on execution
mysql> set global rpl_semi_sync_slave_enabled = 1; // execute from the library
5 Check whether to enable
mysql> show variables like "rpl_semi_sync _ % _ enabled"; // Check whether to enable
6 to modify the configuration file
[root @ 52 ~] # vim / etc / my the .cnf
[mysqld]
plugin-Load = rpl_semi_sync_master = semisync_master.so
rpl_semi_sync_master_enabled = arranged on a main library //
plugin-Load = rpl_semi_sync_slave = semisync = slave.so
rpl_semi_sync_slave_enabled // = 1 from the configuration repository
[root @ 52 ~ ] # systemctl restart mysqld
Main library configuration options (applies to Master server)
/etc/my.conf
Options | use |
---|---|
binlog_do_db = database name | Only allows synchronization library |
binlog_ignore_db = database name | Do not allow synchronization library |
From the Library Configuration Options (for Slave server)
Options | use |
---|---|
log_slave_updates | Records from database updates, allowing the chain to copy (abc) |
relay_log=dbsvr2-relay-bin | Specifies the relay log file name |
replicate_do_db = database name | Copy only designated libraries, other libraries will be ignored, this option can be set multiple (copy all libraries when omitted) |
replicate_ignore_db=test | Does not replicate those libraries, other libraries will be omitted, ignore-db and do-db simply select one |
Fault analysis
Symptom 1
Slave_IO thread is not running
- error: Slave_IO_Running: No
MySQL> Show Slave Status \ G;
Slave_IO_Running: No
Last_IO_Error: error message
Cause Analysis
- connection is not on the master database server
solution
- Check the physical connections (ping), check authorized users
- disable the firewall, close SElinux
-binlog specify the error log file (log name or pos node)
MySQL> STOP Slave;
MySQL> Change to Master option = value;
MySQL> Start Slave;
Symptom 2
Slave_SQL thread is not running
- error: Slave_SQL_Running: No
MySQL> Show Slave Status \ G;
Slave_SQL_Running: No
Last_IO_Error: information error
failure analysis and troubleshooting
- Analysis
- executed when the native relay log of sql command, sql command applicable library, table or record does not exist in the native
settlement
MySQL> STOP Slave;
MySQL> ... // create or restore libraries need to use or table
mysql> start slave;