The separate read and write to achieve mysql (mysql-proxy) ____ 1 (from the master copy mysql, based on the master copy from gtid, semi-synchronous replication, genome replication)

Copy the master-slave principle:

 

 


Library is generated from two threads, one I / O thread, a thread SQL;

I / O request binlog thread to the main library, and writes the obtained log binlog relay log (relay log) file;
primary library is generated a log dump thread, from a library used to binlog i / o threads pass;
the SQL thread reads the relay log log file, and parsed into specific operation to achieve consistent master-slave operation, and consistent final data;

mysql type of log:
Error log Error log
General query log general query log
Slow query log slow query log (recording what queries are slower)
Binary log Binary log file (1, .2 for incremental backups, master-slave)
to implement MySQL the master-slave replication, you must first open the master end of Binlog recording function, it can not be achieved otherwise. Because the entire replication process is actually acquired BInlog logs from Master Slave end, various SQL operations then performed on the acquired Slave the binlog log recorded in the same order.

1. The conventional implementations database replication from the master
copy way, only the master slave copy, i.e., read-only slave to the master library

base2 172.25.78.12 master
base3 172.25.78.13 slaver
# 在master上
[root@base2 ~]# tar xf mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar
[root@base2 ~]# ls
mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar
mysql-community-client-5.7.24-1.el7.x86_64.rpm
mysql-community-common-5.7.24-1.el7.x86_64.rpm
mysql-community-devel-5.7.24-1.el7.x86_64.rpm
mysql-community-embedded-5.7.24-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.24-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.24-1.el7.x86_64.rpm
mysql-community-libs-5.7.24-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.24-1.el7.x86_64.rpm
mysql-community-minimal-debuginfo-5.7.24-1.el7.x86_64.rpm
Community-Server--MySQL 5.7.24-1.el7.x86_64.rpm
MySQL-Community-Server-minimal-5.7.24-1.el7.x86_64.rpm
MySQL-Community-Test-5.7.24-1.el7. . x86_64.rpm
[Base2 the root @ ~] # yum the install MySQL -Y-Community-Client-5.7.24-1.el7.x86_64.rpm \
MySQL-Community-Common-5.7.24-1.el7.x86_64.rpm \
Community Community-libs--MySQL 5.7.24-1.el7.x86_64.rpm \
MySQL-Community Community-libs-compat-5.7.24-1.el7.x86_64.rpm \
MySQL-Community Community-Server-5.7.24-1. el7.x86_64.rpm # only need five to
[root @ Base2 ~] # vim /etc/my.cnf
log-bin = MySQL-bin # enable binary function, master-slave replication infrastructure
server-id = 1 # id only

 

 


[root @ Base2 ~] # systemctl Start mysqld
[root @ Base2 ~] # CAT /var/log/mysqld.log | grep view temporary password password #

 

 


[root @ base2 ~] # mysql -p # we can use a temporary password database, but can not do anything
the Enter password:
MySQL> Show Databases;

 

 

[Root @ base2 ~] # mysql_secure_installation # initialization code

 

 

 

 

 

 

 

[root @ base2 ~] # mysql -p # to log in again, you can normally use
the Enter password:
MySQL> Show Databases;

 

 


mysql> grant replication slave on * * to repl@'172.25.78.% 'identified by' Ting @ 666 ';.. # Create a connection account and grant permission for the server, * * means all rights;. 172.25.78% this means that all network users have the authority
mysql> show master status;

 

 


# On the slave
to the master database to test whether remote login
[root @ Base2 ~] # MySQL 172.25.78.12 -u -h -p repl
the Enter password:
MySQL> Show Databases; # successful test

 

 


MySQL> quit
. 1
starts the slave database configuration
[Base3 the root @ ~] # 5.7.24-1.el7.x86_64.rpm the tar-XF-bundle.tar MySQL
[Base3 the root @ ~] # yum the install MySQL -Y-community- 5.7.24-1.el7.x86_64.rpm-Client \
MySQL-Community Community-the Common-5.7.24-1.el7.x86_64.rpm \
MySQL-Community Community-libs-5.7.24-1.el7.x86_64.rpm \
Community-libs--MySQL compat-5.7.24-1.el7.x86_64.rpm \
MySQL-Community-Server-5.7.24-1.el7.x86_64.rpm
[Base3 the root @ ~] # Vim / etc / My. CNF
server-id = 2
Note: from the server ID number, and the primary ID is not the same, if the service provided from the plurality of
devices, each must have a unique server-id value from the server, and must master server
service and other devices not from the same server. Server-id value may be considered
similar to IP Address: The ID value uniquely identifying the server cluster replicate
each server instance.
[root @ base3 ~] # systemctl start mysqld
[root@base3 ~]# cat /var/log/mysqld.log | grep password

 

 


[root @ base3 ~] # mysql_secure_installation # initialization code
[Base3 the root @ ~] # -p MySQL
MySQL> Change Master to MASTER_HOST = '172.25.78.12', MASTER_USER = 'the repl', master_password = '@ 666 Ting', MASTER_LOG_FILE = 'mysql-bin.000002', master_log_pos = 1003; # establish a connection from the library and the master library, must review the data in the master server and consistent
MySQL> Slave Start;
MySQL> Show Slave Status \ G;

 

 


#在master上创建数据库
[root@base2 ~]# mysql -p
Enter password:
mysql> create database test;
mysql> use test;
mysql> create table userlist(
-> username varchar(15) not null,
-> password varchar(25) not null);
mysql> desc userlist;

 

 


mysql> insert into userlist values ('userq','111');
mysql> insert into userlist values ('user1','222');
mysql> select * from userlist; # 建立成功

 

 


# See if sync on Slave
[root @ Base3 ~] # MySQL -p
the Enter password:
MySQL> Show Databases;

 

 


mysql> use test;
mysql> show tables;

 

 


mysql> select * from userlist; # successful synchronization

 

 


2.gtid Master-slave replication
compared to traditional master-slave replication advantages: no need to know which files to copy, do not need to know the number from which to start copying

# On the Master
[Base2 the root @ ~] # Vim /etc/my.cnf
log-bin bin = MySQL-
Server-ID. 1 =
gtid_mode the ON =
Enforce-gtid to true-Consistency =
[Base2 the root @ ~] # systemctl the restart mysqld
[root @ Base2 ~] # MySQL -p
the Enter password:
. MySQL> grant Slave Replication ON * * to repl@'172.25.78.% 'IDENTIFIED by' Ting @ 666 '; # create an account to connect to the server and grant privileges ., * denotes all permissions;. 172.25.78% means that all the network users have this privilege

# on Slave
[Base3 the root @ ~] # Vim /etc/my.cn
Server-ID = 2
gtid_mode the oN =
Consistency =-gtid-Enforce to true
[the root Base3 @ ~] # systemctl the restart mysqld
[Base3 the root @ ~] # -p MySQL
the Enter password:
MySQL> STOP Slave;
mysql> change master to master_host = ' 172.25.78.12', master_user = 'repl', master_password = 'Ting @ 666', MASTER_AUTO_POSITION = 1; # slave server to change the parameters used to connect master server, where the value is set to MASTER dynamic
MySQL> Slave Start;
MySQL> Show Slave Status \ G;

 

 


# Create a database in the Master
MySQL> Show Master Status; # data has changed

 

 


mysql> create database westos;
mysql> show databas

 

 

 

 

# See if the synchronization slave end
mysql> show databases;

 

 

 

 

[root@base3 ~]# mysql -p
Enter password:
mysql> use mysql;
mysql> show tables;

 

 


mysql> select * from gtid_executed;

 

 


[root@base3 ~]# cd /var/lib/mysql
[root@base3 mysql]# ls

 

 


mysql master-slave replication problems:

1. After the main library downtime, data may be lost
2. From the library only a sql Thread, the main library to write pressure, copying is likely to delay

# Solution:
1. semi-synchronous replication ---- solve the problem of data loss
2 parallel copy ---- solve the problem of delayed copy from the library

4. based gtid achieve semi-synchronous
About half sync:

Be between asynchronous replication and fully synchronous replication, the master database After you perform a transaction submitted by the client
does not immediately returned to the client, but wait at least one received from the library to the relay log in and write it
back to the client. With respect to the asynchronous replication, semi-synchronous replication improves data security, but
it also caused some delay, the delay is at least a TCP / IP round trip time.
Therefore, semi-sync is preferably used in low-latency network.

 

 


# In the Master
MySQL> install plugin rpl_semi_sync_master the soname 'semisync_master.so'; # download the plug
mysql> SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME LIKE '% semi%'; # View plug-in is loaded successfully

 

 


MySQL> Show, Ltd. Free Join the Variables like '% the SEMI%';
MySQL> the SET, Ltd. Free Join rpl_semi_sync_master_enabled = 1; # Set variable, enable the semi-synchronous replication, which is a temporary setting, you need to edit the file permanently set /etc/my.cnf written rpl_semi_sync_master_enabled = 1
mysql> show global variables like '% semi%';

 

 


mysql> show global status like '% semi%'; # These parameters can be monitored

 

 


mysql> show status like 'Rpl_semi_sync_master_status'; # View semisynchronous is running

 

 

 


# Configure slave server
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so '; # download the plug
mysql> set global rpl_semi_sync_slave_enabled = 1; # Set variable, enable the semi-synchronous replication
mysql> stop slave IO_THREAD; # restart the slave IO thread, restart after, will be registered on the slave master to the slave role semi-synchronous replication, asynchronous replication or otherwise
MySQL> Start slave IO_THREAD;
MySQL> Show, Ltd. Free Join the Variables like '% the sEMI%';

 

 


mysql> show status like 'Rpl_semi_sync_slave_status'; # View semisynchronous is running

 

 


# Test:
(1) After the Slave execution stop slave, create a master operation requires only return 10.76s, which coincides with the time rpl_semi_sync_master_timeout parameters.

在slave上
mysql> stop slave;
mysql> show status like 'Rpl_semi_sync_slave_status';

 

 


On the Master
MySQL> Create Database Student;

 

 


mysql> show global status like '%semi%';

 

 


(2) Slave performed before stop slave, master drop action soon return.

On the slave
MySQL> MySQL> Start slave IO_THREAD;
MySQL> Show Databases; # when the slave starts, data is automatically synchronized, without losing

 

 


mysql> show status like 'Rpl_semi_sync_slave_status';

 

 

 


On the Master
MySQL> Database Student drop;

 

 


In fact, the semi-synchronous replication is not half the strict sense of synchronous replication, when the semi-synchronous replication timeout occurs (controlled by rpl_semi_sync_master_timeout parameters, in milliseconds, default is 10000, ie 10s), will temporarily close the semi-synchronous replication, in turn, using asynchronous replication. After all the events master dump thread after sending a transaction, if in rpl_semi_sync_master_timeout, received a response from the library, the main semi-synchronous replication from again restored.

5. Based on gtid for parallel copy
Why is there a parallel copy of it?

Because mysql 5.7 in production environments, slave replication end time, IO thread and the SQL thread is single-threaded, and the master terminal
is multi-threaded, multi-Metro is certainly faster than single-threaded, which appeared in issues of replication latency we use the parallel replication can solve
this problem, the purpose is to make a parallel copy as slave multi-threaded run up, of course, based on the level of multithreading library is also
a way (different library affairs, there is no lock conflict)

# in slave terminal
[Base3 the root @ ~] # Vim /etc/my.cnf
slave-parallel-type = type LOGICAL_CLOCK # parallel
slave-parallel-workers = 5 # number of threads open
master_info_repository = TABLE # maste.info and relay.info in the form of a table stored
relay_log_info_repository = tABLE # store logs in the form of a table
relay_log_recovery = ON # support recovery
[root @ Base3 ~] # systemctl restart mysqld
[root @ Base3 ~] # MySQL -p
the Enter password:
MySQL> Show processlist ;

 

 


6. Copy the group
MySQL replication to achieve a set of updated based multi-master replication agreement.
1) replication server group consists of multiple members, each member in the group server of a transaction may be performed independently. But all read
write (RW) the transaction will only be submitted after the collision detection is successful. Read-only (RO) transactions do not require conflict detection can be submitted immediately.

A Features:.
● High consistency
-based group of native replication technology to copy and paxos agreements, and to provide a plug-in, providing a consistent data security assurance;
● high fault tolerance
if not most broken node can continue to work with automatic detection mechanism, when different nodes generate resource contention conflict, without error, in accordance with the principle of giving priority to process first come first served, and built-in automation split-brain protection mechanism;
● high scalability
adding and removing nodes is automatic after a new node is added automatically from the other node synchronization status until a new node and other nodes is consistent, if a node is removed, the other nodes automatically update the group information, automatic maintenance of the new group information;
● high flexibility of
a single master and multi-master mode, the single master mode, will automatically select the master, all the updates are performed on the master;
multi-master mode, the server can handle all update operations simultaneously.

Second, what kind of scenarios suitable for group copy?
1, flexible database replication environment
group replication can increase flexibility and reduce the cluster database instances
2, highly available database environment
group replication allows the database instance is down, as long as the majority of the cluster server is available, the service is available throughout the database
3, instead of the traditional master-slave configuration of replicated database environment

Base2 172.25.78.12 Master1
Base3 172.25.78.13 Master2
base4 172.25.78.14 Master3
recovery environment, ready to re-initialize
the Base2 #
[@ Base2 the root ~] STOP # systemctl mysqld
[Base2 the root @ ~] # cd / var / lib / MySQL
[root @ Base2 MySQL] # RM -fr *
[root @ Base2 MySQL] # PS AX | grep mysqld to ensure that no mysqld process #

 

 


[root @ base2 mysql] # uuidgen # randomly generated UUID
e3c782ed-8970-4905-a7f8-1d0547d81f0c
[@ Base2 the root MySQL] # Vim /etc/my.cnf
the server_id. 1 =
gtid_mode the ON # = enable binary logging
enforce_gtid_consistency the ON =
master_info_repository = TABLE
relay_log_info_repository = TABLE
binlog_checksum = NONE
log_slave_updates the ON # = log synchronous opening
log_bin the binlog =
binlog_format the ROW # = log binary format

= XXHASH64 transaction_write_set_extraction
Loose-group_replication_group_name = "e3c782ed-8970-4905-a7f8-1d0547d81f0c" # which is randomly generated, but once set, is to determine the UUID of
loose-group_replication_start_on_boot = off # boot does not start
loose-group_replication_local_address = "172.25. 78.12: 24901 "# bind the local port of 172.25.78.12 and 24901 other group members to accept connections
loose-group_replication_group_seeds =" 172.25.78.12:24901,172.25.78.13:24901,172.25.78.14:24901 "# copy group members
loose -group_replication_bootstrap_group = off # boot configuration is not automatically set
loose-group_replication_ip_whitelist = "127.0.0.1,172.25.78.0 / 24 " # on the segment
loose-group_replication_enforce_update_everywhere_checks = ON # update detection
loose-group_replication_single_primary_mode = OFF # set server to automatically select a group process read / write job.

 

 


[root @ base2 mysql] # systemctl start mysqld # open database
[root @ base2 mysql] # cat /var/log/mysqld.log | grep password # to obtain the initial password

 

 


[root @ base2 mysql] # mysql -p # initial password database with
the Enter password:
MySQL> User ALTER localhost IDENTIFIED by the root @ '@ 666 Ting'; # modify local user password
mysql> SET SQL_LOG_BIN = 0; # Close binary log avoid copying from the service
mysql> CREATE uSER rpl_user @ '% ' IDENTIFIED BY 'Ting @ 666'; # give the user a password
mysql> GRANT rEPLICATION SLAVE oN * * TO rpl_user @ '%';. # authorized
mysql> FLUSH PRIVILEGES ; # refresh
mysql> sET SQL_LOG_BIN = 1; # open log
mysql> CHANGE MASTER TO MASTER_USER = ' rpl_user', MASTER_PASSWORD = 'Ting @ 666' FOR CHANNEL 'group_replication_recovery'; # arranged set of user
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication . so '; # installed plugin plugin
mysql> SET GLOBAL group_replication_bootstrap_group = ON; # phrase only base2,Execute the first time to perform guided group
mysql> START GROUP_REPLICATION; # open group copy
MySQL> = OFF the SET GLOBAL group_replication_bootstrap_group;
MySQL> the FROM performance_schema.replication_group_members the SELECT *; # View Copy group members

 

 


mysql> CREATE DATABASE test: # Create Database
MySQL> use Test;
Database changed
MySQL> the CREATE TABLE T1 (C1 the INT a PRIMARY KEY, C2 the TEXT the NOT NULL); # Create table
mysql> INSERT INTO t1 VALUES (1 , 'Luis');
mysql> SELECT * FROM t1;

 

 


# ###### on Base3
[Base3 the root @ ~] STOP # systemctl mysqld
[Base3 the root @ ~] # CD / var / lib / MySQL
[@ Base3 the root MySQL] -fr RM # *
[@ Base3 the root MySQL ] # Vim /etc/my.cnf
the server_id = 2
gtid_mode the ON # = enable binary logging
enforce_gtid_consistency the ON =
master_info_repository = TABLE
relay_log_info_repository = TABLE
binlog_checksum = NONE
log_slave_updates the ON # = log synchronous opening
log_bin the binlog =
binlog_format the ROW # = log binary format

= XXHASH64 transaction_write_set_extraction
Loose-group_replication_group_name = "e3c782ed-8970-4905-a7f8-1d0547d81f0c" # which is randomly generated, but once set, is to determine the UUID of
loose-group_replication_start_on_boot = off # boot does not start
loose-group_replication_local_address = "172.25. 78.13: 24901 "
Loose-group_replication_group_seeds =" 172.25.78.12:24901,172.25.78.13:24901,172.25.78.14:24901 "# copy group members
Loose-group_replication_bootstrap_group = OFF
Loose-group_replication_ip_whitelist =" 127.0.0.1,172.25.78.0 / 24 "on the segment #
loose-group_replication_enforce_update_everywhere_checks = ON # update detection
loose-group_replication_single_primary_mode = OFF

 

 


[root@base3 mysql]# systemctl start mysqld
[root@base3 mysql]# cat /var/log/mysqld.log | grep password

 

 


[root@base3 mysql]# mysql -p
Enter password:
mysql> alter user root@localhost identified by 'Ting@666';
mysql> SET SQL_LOG_BIN=0;
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'Ting@666';
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
mysql> FLUSH PRIVILEGES;
mysql> SET SQL_LOG_BIN=1;
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='Ting@666' FOR CHANNEL 'group_replication_recovery';
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
mysql> START GROUP_REPLICATION;

 

 


mysql> STOP GROUP_REPLICATION;
mysql> reset master;
mysql> START GROUP_REPLICATION;

# base4上
[root@base4 ~]# ls
mysql-community-client-5.7.24-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.24-1.el7.x86_64.rpm
mysql-community-common-5.7.24-1.el7.x86_64.rpm
mysql-community-server-5.7.24-1.el7.x86_64.rpm
mysql-community-libs-5.7.24-1.el7.x86_64.rpm
[root@base4 ~]# yum install -y *
[root@base4 ~]# vim /etc/my.cnf
server_id=3
gtid_mode=ON # 启用二进制日志
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON # 开启日志同步
log_bin=binlog
binlog_format = ROW # binary log format

= XXHASH64 transaction_write_set_extraction
Loose-group_replication_group_name = "e3c782ed-8970-4905-a7f8-1d0547d81f0c" # which is randomly generated, but once set, is to determine the UUID of
loose-group_replication_start_on_boot = off # boot does not start
loose-group_replication_local_address = "172.25. 78.14: 24901 "
Loose-group_replication_group_seeds =" 172.25.78.12:24901,172.25.78.13:24901,172.25.78.14:24901 "# copy group members
Loose-group_replication_bootstrap_group = OFF
Loose-group_replication_ip_whitelist =" 127.0.0.1,172.25.78.0 / 24 "on the segment #
loose-group_replication_enforce_update_everywhere_checks = ON # update detection
Loose-group_replication_single_primary_mode = OFF
[the root base4 @ ~] # systemctl Start mysqld
[base4 the root @ ~] # CAT /var/log/mysqld.log | grep password

 

 


[root@base4 ~]# mysql -p
Enter password:
mysql> alter user root@localhost identified by 'Ting@666';
mysql> SET SQL_LOG_BIN=0;
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'Ting@666';
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
mysql> FLUSH PRIVILEGES;
mysql> SET SQL_LOG_BIN=1;
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='Ting@666' FOR CHANNEL 'group_replication_recovery';
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
mysql> reset master;
mysql> START GROUP_REPLICATION; # 开启组复制
mysql> SELECT * FROM performance_schema.replication_group_members; # 查看组成员成员状态

 

 


mysql> show databases; # direct viewing, the data on the successful synchronization master1

 

 


mysql> use test;
Database changed
mysql> show tables;

 

 


# View on master2, also a successful synchronization
MySQL> use the Test;
Database changed
MySQL> the SELECT * from T1;

 

 


# Can be successfully inserted in either one of the data, but also can view the inserted data
MySQL> the INSERT the VALUES the INTO T1 (2, 'Lala');
MySQL> the INSERT the VALUES the INTO T1 (. 3, 'haha');
MySQL> SELECT * from t1;

 

 

 

Guess you like

Origin www.cnblogs.com/guarderming/p/11856071.html