2.22-2.24 mysql------主从复制,gtid,半同步,全同步,读写分离
主从复制,浪费资源,但是保证量数据的安全性一、mysql的安装和主从复制pos实现(只实现,从服务器复制,主服务器)1、server1和server2解压数据库,并安装server1tar xf mysql-5.7.24-1.el7.x86_64.rpm-bundle.taryum install -y mysql-community-client-5.7.24-1.el7.x8...
主从复制,浪费资源,但是保证量数据的安全性
一、mysql的安装和主从复制pos实现(只实现,从服务器复制,主服务器)
1、server1和server2解压数据库,并安装
server1
tar xf mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar
yum install -y mysql-community-client-5.7.24-1.el7.x86_64.rpm mysql-community-common-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-server-5.7.24-1.el7.x86_64.rpm -y
scp mysql-community-client-5.7.24-1.el7.x86_64.rpm mysql-community-common-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-server-5.7.24-1.el7.x86_64.rpm root@172.25.38.2:
server2
yum install -y mysql-community-client-5.7.24-1.el7.x86_64.rpm mysql-community-common-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-server-5.7.24-1.el7.x86_64.rpm -y
2、server1和server2编写配置文件
server1
vim /etc/my.cnf
编辑如下
log-bin=mysql-bin
server-id=1
server2
vim /etc/my.cnf
server-id=2
3、server1和server2开启服务,查看密码,安全初始化
systemctl start mysqld
cat /var/log/mysqld.log |grep password
mysql_secure_installation
4、server1授权主从复制
mysql -p
grant replication slave on *.* to repl@'172.25.38.%' identified by 'CCJloveu.123';
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 1002 | | | |
+------------------+----------+--------------+------------------+-------------------+
5、server2连接主数据库
mysql -p
change master to master_host='172.25.38.1',master_user='repl', master_password='CCJloveu.123',master_log_file='mysql-bin.000002',master_log_pos=1002;
mysql> start slave;
Query OK, 0 rows affected (0.06 sec)
mysql> show slave status\G; ##查看从属服务器的状态
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.25.38.1
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1880
Relay_Log_File: server2-relay-bin.000002
Relay_Log_Pos: 1198
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes ##IO和SQL都yes算成功
Slave_SQL_Running: Yes
测试:
1、server1创建数据库
create database westos;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| westos |
+--------------------+
5 rows in set (0.00 sec)
mysql> use westos;
Database changed
mysql> create table usertb(
-> username varchar(10) not null,
-> password varchar(15) not null);
Query OK, 0 rows affected (0.32 sec)
mysql> desc usertb;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(10) | NO | | NULL | |
| password | varchar(15) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into usertb valuse('user1','123');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'valuse('user1','123')' at line 1
mysql> insert into usertb valuse ('user1','123');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'valuse ('user1','123')' at line 1
mysql> insert into usertb values ('user1','123');
Query OK, 1 row affected (0.11 sec)
mysql> select * from usertb;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 123 |
+----------+----------+
1 row in set (0.00 sec)
2、server2查看数据存在,实现主从复制
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| westos |
+--------------------+
5 rows in set (0.00 sec)
mysql> use westos;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+------------------+
| Tables_in_westos |
+------------------+
| usertb |
+------------------+
1 row in set (0.00 sec)
mysql> select * from usertb;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 123 |
+----------+----------+
1 row in set (0.00 sec)
mysql> desc usertb;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(10) | NO | | NULL | |
| password | varchar(15) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
二、gtid实现主从复制
gtid,设置为自动寻找下一条日志,不像pos模式,需要指定日志和起始位置
gtid。异步。主数据库发送数据后给从数据库后,不管从数据库是否保存好,都不再处理。
1、编辑配置文件
vim /etc/my.cnf
gtid_mode=ON
enforce-gtid-consistency=true
2、重启数据库
systemctl restart mysqld
3、从数据库,关闭从属,添加从属,打开从属
mysql> stop slave;
Query OK, 0 rows affected (0.07 sec)
mysql> change master to master_host='172.25.38.1',master_user='repl', master_password='CCJloveu.123',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.28 sec)
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.25.38.1
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 154
Relay_Log_File: server2-relay-bin.000002
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
测试:
1、server1添加数据
mysql> insert into usertb values ('user3','123');
Query OK, 1 row affected (0.09 sec)
mysql> select * from usertb
-> ;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 123 |
| user3 | 123 |
+----------+----------+
2 rows in set (0.00 sec)
2、server2查看数据存在,实现量主从复制
mysql> use westos;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from usertb;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 123 |
| user3 | 123 |
+----------+----------+
2 rows in set (0.00 sec)
三、半同步
半同步状态下,主服务器会等待一会,如果从服务器回应,说明备份成功。否则不成功。
一般设置等待时间为无限大(默认是10秒)
1、主从服务器分别安装半同步安装包
server1
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (0.08 sec)
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.45 sec)
server2
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.45 sec)
2、主从服务器分别打开半同步
server1
mysql> SET GLOBAL rpl_semi_sync_master_enabled=1;
Query OK, 0 rows affected (0.00 sec)
server2
mysql> SET GLOBAL rpl_semi_sync_slave_enabled=1;
Query OK, 0 rows affected (0.00 sec)
3、从服务器重新启动IO线程
mysql> STOP SLAVE IO_THREAD;
Query OK, 0 rows affected (0.06 sec)
mysql> START SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)
测试:
1、server1添加数据
mysql> insert into usertb values ('user4','123');
Query OK, 1 row affected (0.48 sec)
2、server2查看数据
mysql> select * from usertb;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 123 |
| user3 | 123 |
| user4 | 123 |
+----------+----------+
3 rows in set (0.00 sec)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
查看gtid的操作:在gtid下每进行一次数据库的操作,那么列表就会多一项,与日志同步,这个号会一直延续下去,gtid就是这样工作的,不需要寻找日志文件。
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from gtid_executed;
+--------------------------------------+----------------+--------------+
| source_uuid | interval_start | interval_end |
+--------------------------------------+----------------+--------------+
| 33b36646-37cd-11e9-a46e-525400dc26cb | 1 | 1 |
| 33b36646-37cd-11e9-a46e-525400dc26cb | 2 | 2 |
+--------------------------------------+----------------+--------------+
2 rows in set (0.00 sec)
四、半同步添加线程
5.7之前,从服务器只有单线程,复制比较慢
5.7之前,主服务器完成复制,就返回用户OK
5.7之后,必须从服务器完成复制,才返回用户OK
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
三个表:从服务器状态表,主服务器半同步状态表,线程表
从服务器状态表
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.25.38.1
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 698 ##读取
Relay_Log_File: server2-relay-bin.000005
Relay_Log_Pos: 414
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 698 ##这两个时间不同就会有延迟
Relay_Log_Space: 623
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0 ##延迟时间
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 33b36646-37cd-11e9-a46e-525400dc26cb
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: 33b36646-37cd-11e9-a46e-525400dc26cb:1-2
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
主服务器半同步状态表
mysql> show variables like '%rpl%';
+-------------------------------------------+------------+
| Variable_name | Value |
+-------------------------------------------+------------+
| rpl_semi_sync_master_enabled | ON |
| rpl_semi_sync_master_timeout | 10000 | ##等待时间默认10秒,设无穷大
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_for_slave_count | 1 | ##只有一个从库
| rpl_semi_sync_master_wait_no_slave | ON |
| rpl_semi_sync_master_wait_point | AFTER_SYNC |
| rpl_semi_sync_slave_enabled | OFF |
| rpl_semi_sync_slave_trace_level | 32 |
| rpl_stop_slave_timeout | 31536000 |
+-------------------------------------------+------------+
9 rows in set (0.00 sec)
线程表
show processlist;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
添加线程的过程
1、编辑从服务器的配置文件
vim /etc/my.cnf
添加如下
slave-parallel-type=LOGICAL_CLOCK ##从服务器并发类型为多线程
slave-parallel-workers=10 ##线程数量设置为10
master_info_repository=TABLE ##以表的方式存储
relay_log_info_repository=TABLE ##存储以后不会存在文件,因为不断刷新文件会变慢
relay_log_recovery=ON ##
2、重启mysql
systemctl restart mysqld
3、查看master_info,查看线程的数量。
mysql> use mysql
mysql> select * from slave_master_info;
+-----------------+------------------+----------------+-------------+-----------+---------------+------+---------------+-------------+--------+------------+----------+------------+---------+------------------------+-----------+------+--------------------+--------------------------------------+-------------+---------+-------------+-----------------------+--------------+-------------+
| Number_of_lines | Master_log_name | Master_log_pos | Host | User_name | User_password | Port | Connect_retry | Enabled_ssl | Ssl_ca | Ssl_capath | Ssl_cert | Ssl_cipher | Ssl_key | Ssl_verify_server_cert | Heartbeat | Bind | Ignored_server_ids | Uuid | Retry_count | Ssl_crl | Ssl_crlpath | Enabled_auto_position | Channel_name | Tls_version |
+-----------------+------------------+----------------+-------------+-----------+---------------+------+---------------+-------------+--------+------------+----------+------------+---------+------------------------+-----------+------+--------------------+--------------------------------------+-------------+---------+-------------+-----------------------+--------------+-------------+
| 25 | mysql-bin.000003 | 698 | 172.25.38.1 | repl | CCJloveu.123 | 3306 | 60 | 0 | | | | | | 0 | 30 | | 0 | 33b36646-37cd-11e9-a46e-525400dc26cb | 86400 | | | 1 | | |
+-----------------+------------------+----------------+-------------+-----------+---------------+------+---------------+-------------+--------+------------+----------+------------+---------+------------------------+-----------+------+--------------------+--------------------------------------+-------------+---------+-------------+-----------------------+--------------+-------------+
1 row in set (0.00 sec)
测试:
查看线程,发现有10个等待线程。
mysql> show processlist;
+----+-------------+-----------+-------+---------+------+--------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+-----------+-------+---------+------+--------------------------------------------------------+------------------+
| 1 | system user | | NULL | Connect | 161 | Waiting for master to send event | NULL |
| 2 | system user | | NULL | Connect | 161 | Slave has read all relay log; waiting for more updates | NULL |
| 4 | system user | | NULL | Connect | 161 | Waiting for an event from Coordinator | NULL |
| 5 | system user | | NULL | Connect | 161 | Waiting for an event from Coordinator | NULL |
| 6 | system user | | NULL | Connect | 161 | Waiting for an event from Coordinator | NULL |
| 7 | system user | | NULL | Connect | 161 | Waiting for an event from Coordinator | NULL |
| 8 | system user | | NULL | Connect | 161 | Waiting for an event from Coordinator | NULL |
| 9 | system user | | NULL | Connect | 161 | Waiting for an event from Coordinator | NULL |
GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';| 10 | system user | | NULL | Connect | 161 | Waiting for an event from Coordinator | NULL |
| 11 | system user | | NULL | Connect | 161 | Waiting for an event from Coordinator | NULL |
| 12 | system user | | NULL | Connect | 161 | Waiting for an event from Coordinator | NULL |
| 13 | system user | | NULL | Connect | 161 | Waiting for an event from Coordinator | NULL |
| 14 | root | localhost | mysql | Query | 0 | starting | show processlist |
+----+-------------+-----------+-------+---------+------+--------------------------------------------------------+------------------+
13 rows in set (0.00 sec)
六、延迟复制
七、一主多从
八、链表主从:主 --> 从1 --> 从2 --> 从3
九、全同步
全同步的每个服务器都是主服务器,也是从服务器。
1、server1和server2恢复数据库状态
关闭数据库,删除数据库内容
systemctl stop mysqld
cd /var/lib/mysql
ls
rm -rf *
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
server1的操作
2、生成uuid,编辑数据库配置文件
[root@server1 mysql]# uuidgen ##生成uuid
0bf580d1-1e4e-45a3-ab75-f5ca1c75001f
vim /etc/my.cnf
编辑如下
server_id=1 ##server号
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
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="0bf580d1-1e4e-45a3-ab75-f5ca1c75001f" ##uuid号
loose-group_replication_start_on_boot=off
loose-group_replication_local_address="172.25.38.1:24901" ##本机ip
loose-group_replication_group_seeds="172.25.38.1:24901,172.25.38.2:24901,172.25.38.3:24901"##所有ip
loose-group_replication_bootstrap_group=off
loose-group_replication_ip_whitelist="127.0.0.1,172.25.38.0/24" ##白名单
loose-group_replication_enforce_update_everywhere_checks=ON
loose-group_replication_single_primary_mode=OFF
3、启动服务,配置服务
systemctl start mysqld
cat /var/log/mysqld.log | grep password
mysql -p
mysql> ALTER USER root@localhost identified by 'CCJloveu.123';
Query OK, 0 rows affected (0.18 sec)
mysql> SET SQL_LOG_BIN=0;
Query OK, 0 rows affected (0.11 sec)
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'CCJloveu.123';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.09 sec)
mysql> SET SQL_LOG_BIN=1;
Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='CCJloveu.123' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.53 sec)
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
Query OK, 0 rows affected (1.04 sec)
mysql> SET GLOBAL group_replication_bootstrap_group=ON;
Query OK, 0 rows affected (0.00 sec)
mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected (3.18 sec)
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
Query OK, 0 rows affected (0.00 sec)
4、查看有几个用户绑定
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 20e95d1c-380b-11e9-a675-525400dc26cb | server1 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
1 row in set (0.00 sec)
5、创建数据
mysql> CREATE DATABASE test;
Query OK, 1 row affected (0.45 sec)
mysql> USE test;
Database changed
mysql> CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 TEXT NOT NULL);
Query OK, 0 rows affected (0.36 sec)
mysql> INSERT INTO t1 VALUES ('1','LUIS');
Query OK, 1 row affected (0.53 sec)
mysql> select * from t1;
+----+------+
| c1 | c2 |
+----+------+
| 1 | LUIS |
+----+------+
1 row in set (0.00 sec)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
server2的操作
6、编辑数据库配置文件
vim /etc/my.cnf
编辑如下
server_id=2
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
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="0bf580d1-1e4e-45a3-ab75-f5ca1c75001f"
loose-group_replication_start_on_boot=off
loose-group_replication_local_address="172.25.38.2:24901"
loose-group_replication_group_seeds="172.25.38.1:24901,172.25.38.2:24901,172.25.38.3:24901"
loose-group_replication_bootstrap_group=off
loose-group_replication_ip_whitelist="127.0.0.1,172.25.38.0/24"
loose-group_replication_enforce_update_everywhere_checks=ON
loose-group_replication_single_primary_mode=OFF
7、启动服务,配置服务
systemctl start mysqld
cat /var/log/mysqld.log | grep password
mysql -p
mysql> ALTER USER root@localhost identified by 'CCJloveu.123';
Query OK, 0 rows affected (0.11 sec)
mysql> SET SQL_LOG_BIN=0;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'CCJloveu.123';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> SET SQL_LOG_BIN=1;
Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='CCJloveu.123' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.47 sec)
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
Query OK, 0 rows affected (0.50 sec)
mysql> reset master;
Query OK, 0 rows affected (0.26 sec)
mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected (6.55 sec)
8、查看用户
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 20e95d1c-380b-11e9-a675-525400dc26cb | server1 | 3306 | ONLINE |
| group_replication_applier | 23531111-380e-11e9-83b1-525400a78908 | server2 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
2 rows in set (0.00 sec)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
server3的操作
9、编辑数据库配置文件
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
transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="0bf580d1-1e4e-45a3-ab75-f5ca1c75001f"
loose-group_replication_start_on_boot=off
loose-group_replication_local_address="172.25.38.3:24901"
loose-group_replication_group_seeds="172.25.38.1:24901,172.25.38.2:24901,172.25.38.3:24901"
loose-group_replication_bootstrap_group=off
loose-group_replication_ip_whitelist="127.0.0.1,172.25.38.0/24"
loose-group_replication_enforce_update_everywhere_checks=ON
loose-group_replication_single_primary_mode=OFF
10、启动服务,配置服务
systemctl start mysqld
cat /var/log/mysqld.log | grep password
mysql -p
mysql> ALTER USER root@localhost identified by 'CCJloveu.123';
Query OK, 0 rows affected (0.19 sec)
mysql> SET SQL_LOG_BIN=0;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'CCJloveu.123';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> SET SQL_LOG_BIN=1;
Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO MASTER_USER='rpl_user', MASTER_PASSWORD='CCJloveu.123' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.45 sec)
mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
Query OK, 0 rows affected (0.30 sec)
mysql> reset master;
Query OK, 0 rows affected (0.24 sec)
mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected (3.93 sec)
11、查看用户
查看发现有三个用户
mysql> mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 11528154-380f-11e9-a2fc-525400867d0c | server3 | 3306 | ONLINE |
| group_replication_applier | 20e95d1c-380b-11e9-a675-525400dc26cb | server1 | 3306 | ONLINE |
| group_replication_applier | 23531111-380e-11e9-83b1-525400a78908 | server2 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
3 rows in set (0.00 sec)
测试:
1、server3插入数据
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> INSERT INTO t1 VALUES ('2','Lsd');
Query OK, 1 row affected (0.68 sec)
2、查看那server1和server2的数据,实现了同步
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t1;
+----+------+
| c1 | c2 |
+----+------+
| 1 | LUIS |
| 2 | Lsd |
+----+------+
2 rows in set (0.00 sec)
3、server2 添加数据
mysql> INSERT INTO t1 VALUES ('3','Lsdsd');
Query OK, 1 row affected (0.28 sec)
4、查看server1和server3的数据实现了同步
mysql> select * from t1;
+----+-------+
| c1 | c2 |
+----+-------+
| 1 | LUIS |
| 2 | Lsd |
| 3 | Lsdsd |
+----+-------+
3 rows in set (0.00 sec)
十、读写分离
1、what 读写分离
读写分离,基本的原理是让主数据库处理事务性增、改、删操作(INSERT、UPDATE、DELETE),而从数据库处理SELECT查询操作。数据库复制被用来把事务性操作导致的变更同步到集群中的从数据库。
2、why 那么为什么要读写分离呢?
因为数据库的“写”(写10000条数据到oracle可能要3分钟)操作是比较耗时的。
但是数据库的“读”(从oracle读10000条数据可能只要5秒钟)。
所以读写分离,解决的是,数据库的写入,影响了查询的效率。
3、when 什么时候要读写分离?
数据库不一定要读写分离,如果程序使用数据库较多时,而更新少,查询多的情况下会考虑使用,利用数据库 主从同步 。可以减少数据库压力,提高性能。当然,数据库也有其它优化方案。memcache 或是 表折分,或是搜索引擎。都是解决方法。
MySQLProxy实际上是在客户端请求与MySQLServer之间建立了一个连接池。所有客户端请求都是发向MySQLProxy,然后经由MySQLProxy进行相应的分析,判断出是读操作还是写操作,分发至对应的MySQLServer上。对于多节点Slave集群,也可以起做到负载均衡的效果。MySQL的主从复制和MySQL的读写分离两者有着紧密联系,首先部署主从复制,只有主从复制完了,才能在此基础上进行数据的读写分离。
简单来说,读写分离就是只在主服务器上写,只在从服务器上读,基本的原理是让主数据库处理事务性操作,而从数据库处理非事务性操作,然后再采用主从复制来把master上的事务性操作同步到slave数据库中。
实验环境:
Server1 172.25.85.1 :主数据库
Server2 172.25.85.2 :从数据库
Server3 172.25.85.3 :
1、实现server1和server2的主从复制,这里不显示过程,只显示结果(我们实现的是server1到server2的复制,server2无法复制给server1)
给server1添加数据
查看server2的数据
- 网上下载mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz,进行解压
tar zxf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz -C /usr/local/
- 编写配置文件
cd /usr/local/
ls
ln -s mysql-proxy-0.8.5-linux-el6-x86-64bit/ mysql-proxy
cd mysql-proxy
ls
mkdir conf
cd conf/
vim mysql-proxy.conf
编写如下
[mysql-proxy]
user=root
daemon=true ##打入后台
keepalive=true
log-level=debug ##日志级别(info)
log-file=/usr/local/mysql-proxy/log/mysql-proxy.log ##日志的存放目录
proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua ###lua脚本的存放目录
proxy-address=172.25.85.3:3306 ###调度器的ip地址
proxy-backend-addresses=172.25.85.1:3306 ##只写的(master的ip地址)
proxy-read-only-backend-addresses=172.25.85.2:3306 ## 只读的(slave的ip地址)
- 修改lua脚本
cd /usr/local/mysql-proxy/share/doc/mysql-proxy/
ls
vim rw-splitting.lua
修改如下
if not proxy.global.config.rwsplit then
proxy.global.config.rwsplit = {
min_idle_connections = 1,
max_idle_connections = 2, ##最大连接数为2
is_debug = false
}
- 修改权限
chmod 660 /usr/local/mysql-proxy/conf/mysql-proxy.conf
- 启动服务
/usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf
发现无法启动
删除配置文件里面的 daemon=true
重新启动,将程序打入后台,并启动
查看3306端口
安装lsof监控工具进行监控,查看3306端口被哪个终端占用
测试:
1、打开前检查每个server的状态
Server1
Server2
Server3
2、测试机打开一个shell,连接调度机,
查看server1的状态,发现多了1个连接
3、测试机再打开一个shell,连接调度机,
查看server1的状态,发现多了1个连接
4、测试机再打开一个shell,连接调度机,
因为设置最大连接数为2,此时应该连接的是server2,查看server2,增加量一个连接
5、此时,再第三个shell(连接server2的测试机),添加一个数据
因为建立的主从复制是,只可以sever1给server2传送数据,在server2添加数据,server1是不备份的。但是我们查看server1的数据库,发现添加数据存在,说明,实现量读写分离,写数据时,直接给sever1添加数据。
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)