table of Contents
One, master-slave replication
1.1 MySQL replication types
- Statement-based replication
- Row-based replication
- Mixed types of replication
Two, MySQL read and write separation
- Read-write separation is based on master-slave replication, first read-write separation, then master-slave replication
- Only write on the master server and read only on the slave server
- The main database handles transactional queries, and the slave database handles SELECT queries
- Database replication is used to synchronize transactional query changes to the slave database in the cluster
- Read-write separation scheme
Based on program code internal implementation
Based on intermediate proxy layer to implement MySQL-Proxy Amoeba
3. Master-slave replication and read-write separation experiment
3.1 Experimental environment
-
Server version: CentOS7.6
-
Database version: mysql 5.7.17
-
Master server master: 192.168.233.100
-
Slave server slave1: 192.168.233.200
-
Slave server slave2: 192.168.233.180
-
Amoeba server: 192.168.233.50
-
To facilitate the experiment, close all server firewalls and core protections to avoid interference
. Enter iptables -F setenforce 0 for each server
3.2 Master-slave replication
3.2.1 master
[root@master ~]# vim /etc/my.cnf
[mysqld]
server-id = 11
log-bin=master-bin ## 开启二进制日志
log_slave-updates=ture ## 允许同步
[root@master ~]# systemctl restart mysqld
[root@master ~]# mysql -uroot -p
mysql> grant replication slave on *.* to 'myslave'@'192.168.233.%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec) ## 授权允许复制的账号
mysql> flush privileges; # 刷新
Query OK, 0 rows affected (0.01 sec)
mysql> show master status; ## 展示主服务器此时的信息 不要在对主服务器有任何操作 不然Position会动
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 604 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
3.2.2 Slave server
- Slave1
[root@slave1 ~]# vim /etc/my.cnf
[mysqld]
server-id = 21 ## 不能一样 是唯一的
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
[root@slave1 ~]# systemctl restart mysqld
[root@slave1 ~]# mysql -uroot -p
mysql> change master to master_host='192.168.233.100',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=604; # #指定主服务地址 用户名 密码 日志名字 开始复制位置
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave; ## 开始从服务器功能
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.233.100
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 604
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 321
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes ## 出现这两个Yes 表示成功
Slave_SQL_Running: Yes
……………………省略一部分
- slave2
[root@slave2 ~]# vim /etc/my.cnf
[mysqld]
server-id = 31
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
[root@slave2 ~]# systemctl restart mysqld
[root@slave2 ~]# mysql -uroot -p
mysql> change master to master_host='192.168.233.100',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=604;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.233.100
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 604
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 321
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
3.3 Read and write separation
- Prepare the jdk-6u14-linux-x64.bin compressed package and prepare the JDK environment on the amoeba server
- amoeba-mysql-binary-2.2.0.tar ## amoeba compressed package
3.3.1 Configure Amoeba server
[root@amoeba ~]# cp jdk-6u14-linux-x64.bin /usr/local/
[root@amoeba ~]# chmod -R 755 /usr/local/jdk-6u14-linux-x64.bin
[root@amoeba ~]# cd /usr/local/
[root@amoeba local]# ./jdk-6u14-linux-x64.bin
yes
enter
[root@amoeba opt]# cd /usr/local/
[root@amoeba local]# mv jdk1.6.0_14/ jdk1.6
[root@amoeba local]# vim /etc/profile ##将 JAVA 环境加入到全局变量
export JAVA_HOME=/usr/local/jdk1.6
export CLSSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/bin/:$PATH:$HOME/bin
export AMOEBA_HOME=/usr/local/amoeba
export PATH=$PATH:$AMOEBA_HOME/bin
[root@amoeba local]# source /etc/profile ## 重新读取 使刚刚的配置生效
[root@amoeba opt]# mkdir /usr/local/amoeba
[root@amoeba opt]# tar zxvf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/ ## 解压到刚刚创建的文件夹
[root@master opt]# chmod -R 755 /usr/local/amoeba/ ## 基于 文件权限
[root@amoeba opt]# /usr/local/amoeba/bin/amoeba ## 检测amoeba是否安装成功 出现下列显示信息表示安装成功
amoeba start|stop
3.3.2 Master-slave service opening authority account
- Enter on both the master and slave servers
mysql> grant all on *.* to test@'192.168.233.%' identified by '123.com';
3.3.3 Modify Amoeba server configuration file
[root@amoeba opt]# /usr/local/amoeba/bin/amoeba
amoeba start|stop
[root@amoeba opt]# cd /usr/local/amoeba/
[root@amoeba amoeba]# vim conf/amoeba.xml
<property name="user">amoeba</property> ## 30行 ## 设置使用amoeba的账号密码
<property name="password">123456</property> ## 32行
115 <property name="defaultPool">master</property>
118 <property name="writePool">master</property> ## 这两行去掉注释
119 <property name="readPool">slaves</property>
[root@amoeba amoeba]# vim conf/dbServers.xml
22 <!-- mysql schema -->
23 <property name="schema">mysql</property> ##这里原本是test 一定要主要修改成数据库已
有的一个库,mysql 5.5版本自带一个test 这里用的5.7 没有这个库,不改成已有的库,连接的时候会报错
24
25 <!-- mysql user -->
26 <property name="user">test</property>
27
28 <!-- mysql password -->
29 <property name="password">123.com</property>
45 <dbServer name="master" parent="abstractServer">
46 <factoryConfig>
47 <!-- mysql ip -->
48 <property name="ipAddress">192.168.233.100</property>
49 </factoryConfig>
50 </dbServer>
51
52 <dbServer name="slave1" parent="abstractServer">
53 <factoryConfig>
54 <!-- mysql ip -->
55 <property name="ipAddress">192.168.233.200</property>
56 </factoryConfig>
57 </dbServer>
58 <dbServer name="slave2" parent="abstractServer">
59 <factoryConfig>
60 <!-- mysql ip -->
61 <property name="ipAddress">192.168.233.180</property>
62 </factoryConfig>
63
64 <dbServer name="slaves" virtual="true">
65 <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
66 <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
67 <property name="loadbalance">1</property>
68
69 <!-- Separated by commas,such as: server1,server2,server1 -->
70 <property name="poolNames">slave1,slave2</property>
71 </poolConfig>
72 </dbServer>
[root@amoeba amoeba]# yum -y install mysql ## 在amoeba服务器安装一个mysql
[root@amoeba amoeba]# /usr/local/amoeba/bin/amoeba & ## 开启amoba服务
3.4 Test
- Main server
mysql> use school
Database changed
mysql> create table zang (id int(10),name varchar(10),address varchar(20));
Query OK, 0 rows affected (0.01 sec)
从服务器
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
主服务器
mysql> insert into zang values(1,'zhangsan','this is master');
从服务器
从服务器1
mysql> insert into zang values(2,'lisi','this is slave1');
Query OK, 1 row affected (0.00 sec)
从服务器2
mysql> insert into zang values(3,'wangwu','this is slave2');
Query OK, 1 row affected (0.01 sec)
- Client view zang table## Two read from the server in turn
Database changed
MySQL [school]> select *from zang;
+------+------+----------------+
| id | name | address |
+------+------+----------------+
| 2 | lisi | this is slave1 |
+------+------+----------------+
1 row in set (0.01 sec)
MySQL [school]> select *from zang;
+------+--------+----------------+
| id | name | address |
+------+--------+----------------+
| 3 | wangwu | this is slave2 |
+------+--------+----------------+
1 row in set (0.02 sec)
- Client write data
MySQL [school]> insert into zang values(4,'zhaoliu','this is client');
Query OK, 1 row affected (0.03 sec)
只能在主服务器看到
mysql> select * from zang;
+------+----------+----------------+
| id | name | address |
+------+----------+----------------+
| 1 | zhangsan | this is master |
| 4 | zhaoliu | this is client |
+------+----------+----------------+
2 rows in set (0.00 sec)
从服务器只能看到自己写入的数据
mysql> select *from zang;
+------+--------+----------------+
| id | name | address |
+------+--------+----------------+
| 3 | wangwu | this is slave2 |
+------+--------+----------------+
1 row in set (0.00 sec)