一、案例概述
在企业应用中,成熟的业务通常数据量都比较大
单台mysql在安全性、高可用性和高并发方面都无法满足实际的需求
配置多台主从数据库服务器以实现读写分离
二、案例前置知识点
1. 读写分离的定义
基本的原理是让主数据库处理事务性增、改、删操作 (INSERT、UPDATE、DELETE),而从数据库处理SELECT查询操作。 数据库复制被用来把事务性操作导致的变更同步到集群中的从数据库。
2. 读写分离的原因
因为数据库的"写"(写10000条数据可能要3分钟)操作是比较耗时的。
但是数据库的"读"(读10000条数据可能只要5秒钟)。
所以读写分离,解决的是,数据库的写入,影响了查询的效率。
3. 读写分离的应用
数据库不一定要读写分离, 如果程序使用数据库较多时,而更新少,查询多的情况下会考虑使用。利用数据库主从同步, 再通过读写分离可以分担数据库压力,提高性能。
4. 主从复制与读写分离
在实际的生产环境中, 对数据库的读和写都在同一个数据库服务器中,是不能满足实际需求的。无论是在安全性、高可用性还是高并发等各个方面都是完全不能满足实际需求的。因此,通过主从复制的方式来同步数据,再通过读写分离来提升数据库的并发负载能力。有点类似于rsync,但是不同的是rsync是对磁盘文件做备份,而mysql主从复制是对数据库中的数据、语句做备份。
5. mysql支持的复制类型
(1)STATEMENT
基于语句的复制。在服务器上执行sql语句,在从服务器上执行同样的语句,mysql默认采用基于语句的复制,执行效率高。
(2)ROW
基于行的复制。把改变的内容复制过去, 而不是把命令在从服务器上执行一遍。
(3)MIXED
混合类型的复制。默认采用基于语句的复制,一旦发现基于语句无法精确复制时, 就会采用基于行的复制。
6. 主从复制的工作过程
(1)Master节点将数据的改变记录成二进制日志(bin log),当Master上的数据发生改变时,则将其改变写入二进制日志中。
(2)Slave节点会在一定时间间隔内对Master的二进制日志进行探测其是否发生改变,如果发生改变,则开始一个I/O线程请求 Master的二进制事件。
(3)同时Master节点为每个I/O线程启动一个dump线程,用于向其发送二进制事件,并保存至Slave节点本地的中继日志(Relay log)中,Slave节点将启动SQL线程从中继日志中读取二进制日志,在本地重放,即解析成sql语句逐一执行,使得其数据和Master节点的保持一致,最后I/O线程和SQL线程将进入睡眠状态,等待下一次被唤醒。
注∶
●中继日志通常会位于OS缓存中,所以中继日志的开销很小。
●复制过程有一个很重要的限制,即复制在slave上是串行化的,也就是说Master上的并行更新操作不能在Slave上并行操作。
7. MySQL主从复制高延迟的原因
(1)master服务器高并发,形成大量事务
(2)网络延迟
(3)主从硬件设备导致 cpu主频、内存io、硬盘io
(4)本来就不是同步复制、而是异步复制
8. MySQL主从复制高延迟的解决办法
(1)从库优化Mysql参数。比如增大innodb_buffer_pool_size,让更多操作在Mysql内存中完成,减少磁盘操作。
(2)从库使用高性能主机。包括cpu强悍、内存加大。避免使用虚拟云主机,使用物理主机,这样提升了i/o方面性能。
(3)从库使用SSD磁盘,避免使用低速的机械硬盘。
(4)网络优化,避免跨机房实现同步。
9. 常见的 MySQL 读写分离
目前较为常见的 MySQL 读写分离分为以下两种:
(1)基于程序代码内部实现
在代码中根据select、insert进行路由分类,这类方法也是目前生产环境应用最广泛的。
优点是性能较好,因为在程序代码中实现,不需要增加额外的设备为硬件开支;缺点是需要开发人员来实现, 运维人 员无从下手。
但是并不是所有的应用都适合在程序代码中实现读写分离,像一些大型复杂的Java应用,如果在程序代码中实现读写分离对代码改动就较大。
(2)基于中间代理层实现
代理一般位于客户端和服务器之间,代理服务器接到客户端请求后通过判断后转发到后端数据库,有以下代表性程序。
①MySQL-Proxy
MySQL-Proxy为MySQL开源项目,通过其自带的lua脚本进行SQL判断。
②Atlas
是由奇虎360的Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目。它是在mysql-proxy0.8.2版本的基础上,对其进行了优化,增加了一些新的功能特性。360内部使用Atlas运行的mysql业务,每天承载的读写请求数达几十亿条,支持事物以及存储过程。
③Amoeba
由陈思儒开发,作者曾就职于阿里巴巴。该程序由Java语言进行开发,阿里巴巴将其用于生产环境。但是它不支持事务和存储过程。
由于使用MySQL Proxy需要写大量的Lua脚本,这些Lua并不是现成的,而是需要自己去写。这对于并不熟悉MySQL Proxy内置变量和MySQL Protocol的人来说是非常困难的。
Amoeba是一个非常容易便用、可移植性非常强的软件。因此它在生产环境中被广泛应用于数据库的代理层。
三、案例环境
1. 服务器IP、软件配置
服务器 | 主机名 | IP | 软件版本 |
---|---|---|---|
Master服务器 | master | 192.168.122.10 | mysql 5.7 |
Slave1服务器 | slave1 | 192.168.122.100 | mysql 5.7 |
Slave2服务器 | slave2 | 192.168.122.101 | mysql 5.7 |
Amoeba服务器 | amoeba | 192.168.122.11 | jdk1.5/Amoeba |
客户端服务器 | client | 192.168.122.12 | mysql(yum安装mariadb) |
2. 各服务器环境配置
systemctl stop firewalld | |
systemctl disable firewalld | |
setenforce 0 |
四、案例实施
1. 搭建MySQL主从复制
(1)MySQL主从服务器时间同步
Master服务器(192.168.122.10)
[root@master ~]# yum install -y ntp | |
#NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议。 | |
[root@master ~]# vim /etc/ntp.conf | |
##末行添加 | |
server 127.127.122.0 ##设置本地是时钟源,122为主机所在网段 | |
fudge 127.127.122.0 stratum 8 ##设置时间层级为8(一般在15以内皆可) | |
[root@master ~]# service ntpd start | |
Redirecting to /bin/systemctl start ntpd.service |
Slave1服务器(192.168.122.100)
[root@slave1 ~]# yum install -y ntp ntpdate | |
[root@slave1 ~]# service ntpd start | |
Redirecting to /bin/systemctl start ntpd.service | |
[root@slave1 ~]# /usr/sbin/ntpdate 192.168.122.10 | |
#进行时间同步 | |
6 Sep 02:14:26 ntpdate[1622]: the NTP socket is in use, exiting | |
[root@slave1 ~]# crontab -e | |
#为防止时间的延迟和偏差,设置计划任务进行定时同步 | |
*/30 * * * * /usr/sbin/ntpdate 192.168.122.10 |
Slave2服务器(192.168.122.101)
[root@slave2 ~]# yum install -y ntp ntpdate | |
[root@slave2 ~]# service ntpd start | |
Redirecting to /bin/systemctl start ntpd.service | |
[root@slave2 ~]# /usr/sbin/ntpdate 192.168.122.10 | |
#进行时间同步 | |
6 Sep 02:20:39 ntpdate[1738]: the NTP socket is in use, exiting | |
[root@slave2 ~]# crontab -e | |
#为防止时间的延迟和偏差,设置计划任务进行定时同步 | |
*/30 * * * * /usr/sbin/ntpdate 192.168.122.10 |
(2)主服务器的MySQL配置
[root@master ~]# vim /etc/my.cnf | |
server-id = 1 ##定义server-id,每台主机不可相同 | |
log-bin=master-bin ##添加,主服务器开启二进制日志 | |
binlog_format = MIXED ##本次使用MIXED模式 | |
log-slave-updates=true ##添加,允许从服务器更新二进制日志 | |
[root@master ~]# systemctl restart mysqld | |
[root@master ~]# mysql -u root -p | |
Enter password: | |
mysql> grant replication slave on *.* to 'myslave'@'192.168.122.%' identified by '123456'; | |
##设置从服务器账号并授权 | |
Query OK, 0 rows affected, 1 warning (0.00 sec) | |
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 | |
mysql> select user,host,authentication_string from user; | |
+---------+---------------+-------------------------------------------+ | |
| user | host | authentication_string | | |
+---------+---------------+-------------------------------------------+ | |
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | |
| myslave | 192.168.122.% | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | |
+---------+---------------+-------------------------------------------+ | |
2 rows in set (0.00 sec) | |
mysql> flush privileges; | |
Query OK, 0 rows affected (0.00 sec) | |
mysql> show master status; | |
##查看主服务器状态,file列显示日志名,Position列显示偏移量 | |
mysql> show master status; | |
+-------------------+----------+--------------+------------------+-------------------+ | |
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | | |
+-------------------+----------+--------------+------------------+-------------------+ | |
| master-bin.000001 | 154 | | | | | |
+-------------------+----------+--------------+------------------+-------------------+ | |
1 row in set (0.00 sec) |
(3)从服务器的MySQL配置
Slave1服务器(192.168.122.100)
[root@slave1 ~]# vim /etc/my.cnf | |
server-id = 2 ##修改,注意id与其他主机都不能相同 | |
relay-log=relay-log-bin ##添加,开启中继日志,从主服务器上同步日志文件记录到本地 | |
relay-log-index=slave-relay-bin.index ##添加,定义中继日志文件的位置和名称 | |
[root@slave1 ~]# systemctl restart mysqld | |
[root@slave1 ~]# mysql -u root -p | |
Enter password: | |
mysql> change master to | |
-> master_host='192.168.122.10', | |
-> master_user='myslave', | |
-> master_password='123456', | |
-> master_log_file='master-bin.000001', | |
-> master_log_pos=154; | |
##配置同步,注意master_log_file和master_log_pos的值要与Master查询的一致 | |
Query OK, 0 rows affected, 2 warnings (0.01 sec) | |
mysql> start slave; | |
##启动同步,如有报错执行reset slave; | |
Query OK, 0 rows affected (0.00 sec) | |
mysql> show slave status\G | |
##查看Slave状态,确保IO和SQL线程都是Yes,代表同步正常。 | |
*************************** 1. row *************************** | |
Slave_IO_State: Waiting for master to send event | |
Master_Host: 192.168.122.10 | |
Master_User: myslave | |
Master_Port: 3306 | |
Connect_Retry: 60 | |
Master_Log_File: master-bin.000001 | |
Read_Master_Log_Pos: 154 | |
Relay_Log_File: relay-log-bin.000002 | |
Relay_Log_Pos: 321 | |
Relay_Master_Log_File: master-bin.000001 | |
Slave_IO_Running: Yes | |
##负责与主机的IO通信 | |
Slave_SQL_Running: Yes | |
##负责自己的slave mysql进程 | |
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: 154 | |
Relay_Log_Space: 526 | |
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: 026cf8f0-09e1-11ec-8ae9-000c2959bebe | |
Master_Info_File: /usr/local/mysql/data/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: | |
Auto_Position: 0 | |
Replicate_Rewrite_DB: | |
Channel_Name: | |
Master_TLS_Version: | |
1 row in set (0.00 sec) | |
Slave2服务器(192.168.122.101)
[root@slave2 ~]# vim /etc/my.cnf | |
server-id = 3 ##修改,注意id与其他主机都不能相同 | |
relay-log=relay-log-bin ##添加,开启中继日志,从主服务器上同步日志文件记录到本地 | |
relay-log-index=slave-relay-bin.index ##添加,定义中继日志文件的位置和名称 | |
[root@slave2 ~]# systemctl restart mysqld | |
[root@slave2 ~]# mysql -u root -p | |
Enter password: | |
mysql> change master to | |
-> master_host='192.168.122.10', | |
-> master_user='myslave', | |
-> master_password='123456', | |
-> master_log_file='master-bin.000001', | |
-> master_log_pos=154; | |
##配置同步,注意master_log_file和master_log_pos的值要与Master查询的一致 | |
Query OK, 0 rows affected, 2 warnings (0.01 sec) | |
mysql> start slave; | |
##启动同步,如有报错执行reset slave; | |
Query OK, 0 rows affected (0.00 sec) | |
mysql> show slave status\G | |
##查看Slave状态,确保IO和SQL线程都是Yes,代表同步正常。 | |
*************************** 1. row *************************** | |
Slave_IO_State: Waiting for master to send event | |
Master_Host: 192.168.122.10 | |
Master_User: myslave | |
Master_Port: 3306 | |
Connect_Retry: 60 | |
Master_Log_File: master-bin.000001 | |
Read_Master_Log_Pos: 154 | |
Relay_Log_File: relay-log-bin.000002 | |
Relay_Log_Pos: 321 | |
Relay_Master_Log_File: master-bin.000001 | |
Slave_IO_Running: Yes | |
##负责与主机的IO通信 | |
Slave_SQL_Running: Yes | |
##负责自己的slave mysql进程 | |
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: 154 | |
Relay_Log_Space: 526 | |
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: 026cf8f0-09e1-11ec-8ae9-000c2959bebe | |
Master_Info_File: /usr/local/mysql/data/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: | |
Auto_Position: 0 | |
Replicate_Rewrite_DB: | |
Channel_Name: | |
Master_TLS_Version: | |
1 row in set (0.00 sec) |
注意:
一般Slave_IO_Running:No 的可能性有:
- 网络不通
- my.cnf配置有问题
- 密码、file文件名、pos偏移量不对
- 防火墙没有关闭
(4)验证主从复制效果
Master服务器(192.168.122.10)
mysql> show databases; | |
+--------------------+ | |
| Database | | |
+--------------------+ | |
| information_schema | | |
| mysql | | |
| performance_schema | | |
| sys | | |
+--------------------+ | |
4 rows in set (0.00 sec) | |
mysql> create database test; | |
Query OK, 1 row affected (0.01 sec) | |
mysql> show databases; | |
+--------------------+ | |
| Database | | |
+--------------------+ | |
| information_schema | | |
| mysql | | |
| performance_schema | | |
| sys | | |
| test | | |
+--------------------+ | |
5 rows in set (0.00 sec) |
Slave1服务器(192.168.122.100)
mysql> show databases; | |
+--------------------+ | |
| Database | | |
+--------------------+ | |
| information_schema | | |
| mysql | | |
| performance_schema | | |
| sys | | |
| test | | |
+--------------------+ | |
5 rows in set (0.00 sec) |
Slave2服务器(192.168.122.101)
mysql> show databases; | |
+--------------------+ | |
| Database | | |
+--------------------+ | |
| information_schema | | |
| mysql | | |
| performance_schema | | |
| sys | | |
| test | | |
+--------------------+ | |
5 rows in set (0.01 sec) |
2. 搭建MySQL读写分离
(1)Amoeba服务器配置
①安装Java环境
因为Amoeba是基于jdk1.5开发的,所以官方推荐使用jdk1.5或1.6版本,高版本不建议使用。
[root@amoeba ~]# cd /opt | |
##将jdk1.6以及amoeba安装包上传至/opt目录中 | |
[root@amoeba opt]# cp jdk-6u14-linux-x64.bin /usr/local | |
[root@amoeba opt]# cd /usr/local/ | |
[root@amoeba local]# chmod +x jdk-6u14-linux-x64.bin | |
[root@amoeba local]# ./jdk-6u14-linux-x64.bin | |
##回车至用户选项,yes后再回车 | |
[root@amoeba local]# mv jdk1.6.0_14/ /usr/local/jdk1.6 | |
[root@amoeba local]# vim /etc/profile | |
##末行写入 | |
export JAVA_HOME=/usr/local/jdk1.6 | |
export CLASSPATH=$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 local]# java -version | |
java version "1.6.0_14" | |
Java(TM) SE Runtime Environment (build 1.6.0_14-b08) | |
Java HotSpot(TM) 64-Bit Server VM (build 14.0-b16, mixed mode) |
②安装Amoeba软件
[root@amoeba local]# mkdir /usr/local/amoeba | |
[root@amoeba local]# cd /opt | |
[root@amoeba opt]# tar zxvf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba/ | |
[root@amoeba opt]# chmod -R 755 /usr/local/amoeba/ | |
[root@amoeba opt]# /usr/local/amoeba/bin/amoeba | |
amoeba start|stop | |
##如显示amoeba start|stop说明安装成功 |
(2)配置Amoeba读写分离,两个Slave读负载均衡
- 先在Master、Slave1、Slave2、的MySQL上开放权限给Amoeba访问
Master服务器(192.168.122.10)
mysql> grant all on *.* to test@'192.168.122.%' identified by '123456'; | |
Query OK, 0 rows affected, 1 warning (0.00 sec) |
Slave1服务器(192.168.122.100)
mysql> grant all on *.* to test@'192.168.122.%' identified by '123456'; | |
Query OK, 0 rows affected, 1 warning (0.00 sec) |
Slave2服务器(192.168.122.101)
mysql> grant all on *.* to test@'192.168.122.%' identified by '123456'; | |
Query OK, 0 rows affected, 1 warning (0.00 sec) |
- 再回到amoeba服务器配置amoeba服务
[root@amoeba opt]# cd /usr/local/amoeba/conf | |
[root@amoeba conf]# cp amoeba.xml amoeba.xml.bak | |
##修改amoeba配置文件前,先做备份 | |
[root@amoeba conf]# vim amoeba.xml | |
##30行,修改 | |
<property name="user">amoeba</property> | |
##32行,修改 | |
<property name="password">123456</property> | |
##115行,修改 | |
<property name="defaultPool">master</property> | |
##117行-120行,取消注释并修改 | |
<property name="writePool">master</property> | |
<property name="readPool">slaves</property> | |
[root@amoeba conf]# cp dbServers.xml dbServers.xml.bak | |
##修改数据库配置文件前,先做备份 | |
##23行,注释掉,作用:默认进入test库,以防mysql中没有test库,会报错。 | |
<!-- property name="schema">test</property --> | |
##26行,修改,使用之前创建的授权用户 | |
<property name="user">test</property> | |
##28行-30行,取消注释并修改,密码为之前创建的授权用户密码 | |
<!-- mysql password --> | |
<property name="password">123456</property> | |
##45行,修改,设置主服务器的名为master | |
<dbServer name="master" parent="abstractServer"> | |
##48行,修改,设置主服务器的地址 | |
<property name="ipAddress">192.168.122.10</property> | |
##52行,修改,设置从服务器的名为slave1 | |
<dbServer name="slave1" parent="abstractServer"> | |
##55行,修改,设置从服务器1的复制 | |
<property name="ipAddress">192.168.122.100</property> | |
##58行,复制上面6行粘贴,修改为从服务器2的配置信息。建议到52行使用“6yy”后到58行“p” | |
59 <dbServer name="slave2" parent="abstractServer"> | |
60 <factoryConfig> | |
61 <!-- mysql ip --> | |
62 <property name="ipAddress">192.168.122.101</property> | |
63 </factoryConfig> | |
64 </dbServer> | |
##65行,修改 | |
<dbServer name="slaves" virtual="true"> | |
##71行,修改 | |
<property name="poolNames">slave1,slave2</property> | |
[root@amoeba conf]# /usr/local/amoeba/bin/amoeba start& | |
##后台启动amoeba软件,按ctrl+c返回 | |
[2] 5554 | |
[root@amoeba conf]# remote application= .Amoeba:25177 response OK | |
amoeba server is running with port=25177 | |
^C | |
[2]+ 退出 255 /usr/local/amoeba/bin/amoeba start | |
[root@amoeba conf]# netstat -natp |grep java | |
##查看8066端口是否开启,默认端口为TCP8066 | |
tcp6 0 0 127.0.0.1:25177 :::* LISTEN 5505/java | |
tcp6 0 0 :::8066 :::* LISTEN 5505/java | |
tcp6 0 0 192.168.122.11:41708 192.168.122.10:3306 ESTABLISHED 5505/java | |
tcp6 0 0 192.168.122.11:45476 192.168.122.101:3306 ESTABLISHED 5505/java | |
tcp6 0 0 192.168.122.11:52888 192.168.122.100:3306 ESTABLISHED 5505/java |
(3)客户端服务器测试读写分离
①测试一:客户端建表
客户端服务器(192.168.122.12)
[root@client ~]# yum install -y mariadb-server mariadb | |
[root@client ~]# systemctl start mariadb.service | |
[root@client ~]# mysql -u amoeba -p123456 -h 192.168.122.11 -P8066 | |
##通过amoeba服务器代理访问mysql,在通过客户端连接mysql后写入的数据只有主服务会记录,然后同步给从服务器。 | |
##这里通过192.168.122.11主机的8066端口使用用户amoeba登录mysql。 | |
Welcome to the MariaDB monitor. Commands end with ; or \g. | |
Your MySQL connection id is 738807903 | |
Server version: 5.1.45-mysql-amoeba-proxy-2.2.0 Source distribution | |
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. | |
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. | |
MySQL [(none)]> show databases; | |
##登陆成功 | |
+--------------------+ | |
| Database | | |
+--------------------+ | |
| information_schema | | |
| mysql | | |
| performance_schema | | |
| sys | | |
| test | | |
+--------------------+ | |
5 rows in set (0.01 sec) | |
MySQL [(none)]> use test; | |
Database changed | |
MySQL [test]> show tables; | |
Empty set (0.01 sec) | |
MySQL [test]> create table test(id int,name char(20)); | |
Query OK, 0 rows affected (0.01 sec) | |
MySQL [test]> show tables; | |
+----------------+ | |
| Tables_in_test | | |
+----------------+ | |
| test | | |
+----------------+ | |
1 row in set (0.01 sec) |
Master服务器(192.168.122.10)
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 | |
mysql> show tables; | |
+----------------+ | |
| Tables_in_test | | |
+----------------+ | |
| test | | |
+----------------+ | |
1 row in set (0.01 sec) |
Slave1服务器(192.168.122.100)
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> show tables; | |
+----------------+ | |
| Tables_in_test | | |
+----------------+ | |
| test | | |
+----------------+ | |
1 row in set (0.00 sec) |
Slave2服务器(192.168.122.101)
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> show tables; | |
+----------------+ | |
| Tables_in_test | | |
+----------------+ | |
| test | | |
+----------------+ | |
1 row in set (0.00 sec) |
测试结果:客户端中做出的修改,在所有服务器上都能看到。
②测试二:关闭slave功能后,在主从服务器插入数据
Slave1服务器(192.168.122.100)
mysql> stop slave; | |
Query OK, 0 rows affected (0.00 sec) | |
mysql> insert into test values(1,'slave1'); | |
Query OK, 1 row affected (0.00 sec) | |
mysql> select * from test; | |
+------+--------+ | |
| id | name | | |
+------+--------+ | |
| 1 | slave1 | | |
+------+--------+ | |
1 row in set (0.00 sec) |
Slave2服务器(192.168.122.101)
mysql> stop slave; | |
Query OK, 0 rows affected (0.00 sec) | |
mysql> insert into test values(2,'slave2'); | |
Query OK, 1 row affected (0.01 sec) | |
mysql> select * from test; | |
+------+--------+ | |
| id | name | | |
+------+--------+ | |
| 2 | slave2 | | |
+------+--------+ | |
1 row in set (0.00 sec) |
Master服务器(192.168.122.10)
mysql> insert into test values(3,'master'); | |
Query OK, 1 row affected (0.00 sec) | |
mysql> select * from test; | |
+------+--------+ | |
| id | name | | |
+------+--------+ | |
| 3 | master | | |
+------+--------+ | |
1 row in set (0.00 sec) |
客户端服务器(192.168.122.12)
MySQL [test]> select * from test; | |
+------+--------+ | |
| id | name | | |
+------+--------+ | |
| 1 | slave1 | | |
+------+--------+ | |
1 row in set (0.01 sec) | |
MySQL [test]> select * from test; | |
+------+--------+ | |
| id | name | | |
+------+--------+ | |
| 2 | slave2 | | |
+------+--------+ | |
1 row in set (0.00 sec) | |
MySQL [test]> select * from test; | |
+------+--------+ | |
| id | name | | |
+------+--------+ | |
| 1 | slave1 | | |
+------+--------+ | |
1 row in set (0.00 sec) | |
MySQL [test]> select * from test; | |
+------+--------+ | |
| id | name | | |
+------+--------+ | |
| 2 | slave2 | | |
+------+--------+ | |
1 row in set (0.00 sec) |
测试结果:客户端在slave1、slave2中轮询查看数据。由于从服务器都关闭了slave功能,因此无法查看Master服务器中的修改。
③关闭slave功能后,客户端服务器写入数据
客户端服务器(192.168.122.12)
MySQL [test]> insert into test values(4,'client1'); | |
Query OK, 1 row affected (0.00 sec) | |
MySQL [test]> select * from test; | |
+------+--------+ | |
| id | name | | |
+------+--------+ | |
| 1 | slave1 | | |
+------+--------+ | |
1 row in set (0.00 sec) | |
MySQL [test]> select * from test; | |
+------+--------+ | |
| id | name | | |
+------+--------+ | |
| 2 | slave2 | | |
+------+--------+ | |
1 row in set (0.01 sec) |
Master服务器(192.168.122.10)
mysql> select * from test; | |
+------+---------+ | |
| id | name | | |
+------+---------+ | |
| 3 | master | | |
| 4 | client1 | | |
+------+---------+ | |
2 rows in set (0.00 sec) |
Slave1服务器(192.168.122.100)
mysql> select * from test; | |
+------+--------+ | |
| id | name | | |
+------+--------+ | |
| 1 | slave1 | | |
+------+--------+ | |
1 row in set (0.00 sec) |
Slave2服务器(192.168.122.101)
mysql> select * from test; | |
+------+--------+ | |
| id | name | | |
+------+--------+ | |
| 2 | slave2 | | |
+------+--------+ | |
1 row in set (0.00 sec) |
测试结果:客户端服务器的修改操作是对于Master服务器进行的,因此Master服务器上可以看到客户端服务器所做的insert操作。由于客户端服务器的select操作是对从服务器进行的,而从服务器关闭了slave功能,无法获取更新,因此客户端服务器本身以及从服务器都无法查看更新操作。
④从服务器开启slave服务
Slave1服务器(192.168.122.100)
mysql> start slave; | |
Query OK, 0 rows affected (0.00 sec) | |
mysql> select * from test; | |
+------+---------+ | |
| id | name | | |
+------+---------+ | |
| 1 | slave1 | | |
| 3 | master | | |
| 4 | client1 | | |
+------+---------+ | |
3 rows in set (0.00 sec) |
Slave2服务器(192.168.122.101)
mysql> start slave; | |
Query OK, 0 rows affected (0.00 sec) | |
mysql> select * from test; | |
+------+---------+ | |
| id | name | | |
+------+---------+ | |
| 2 | slave2 | | |
| 3 | master | | |
| 4 | client1 | | |
+------+---------+ | |
3 rows in set (0.00 sec) |
Master服务器(192.168.122.10)
mysql> select * from test; | |
+------+---------+ | |
| id | name | | |
+------+---------+ | |
| 3 | master | | |
| 4 | client1 | | |
+------+---------+ | |
2 rows in set (0.00 sec) |
客户端服务器(192.168.122.12)
MySQL [test]> select * from test; | |
+------+---------+ | |
| id | name | | |
+------+---------+ | |
| 1 | slave1 | | |
| 3 | master | | |
| 4 | client1 | | |
+------+---------+ | |
3 rows in set (0.01 sec) | |
MySQL [test]> select * from test; | |
+------+---------+ | |
| id | name | | |
+------+---------+ | |
| 2 | slave2 | | |
| 3 | master | | |
| 4 | client1 | | |
+------+---------+ | |
3 rows in set (0.00 sec) |
测试结果:从服务器开启slave服务后可以从Master服务器上获取更新,但是从服务器上的更新不会被Master服务器看到。
⑤客户端服务器再次插入数据
客户端服务器(192.168.122.12)
MySQL [test]> select * from test; | |
+------+---------+ | |
| id | name | | |
+------+---------+ | |
| 1 | slave1 | | |
| 3 | master | | |
| 4 | client1 | | |
| 5 | client2 | | |
+------+---------+ | |
4 rows in set (0.00 sec) | |
MySQL [test]> select * from test; | |
+------+---------+ | |
| id | name | | |
+------+---------+ | |
| 2 | slave2 | | |
| 3 | master | | |
| 4 | client1 | | |
| 5 | client2 | | |
+------+---------+ | |
4 rows in set (0.00 sec) |
Master服务器(192.168.122.10)
mysql> select * from test; | |
+------+---------+ | |
| id | name | | |
+------+---------+ | |
| 3 | master | | |
| 4 | client1 | | |
| 5 | client2 | | |
+------+---------+ | |
3 rows in set (0.00 sec) |
Slave1服务器(192.168.122.100)
mysql> select * from test; | |
+------+---------+ | |
| id | name | | |
+------+---------+ | |
| 1 | slave1 | | |
| 3 | master | | |
| 4 | client1 | | |
| 5 | client2 | | |
+------+---------+ | |
4 rows in set (0.00 sec) |
Slave2服务器(192.168.122.101)
mysql> select * from test; | |
+------+---------+ | |
| id | name | | |
+------+---------+ | |
| 2 | slave2 | | |
| 3 | master | | |
| 4 | client1 | | |
| 5 | client2 | | |
+------+---------+ | |
4 rows in set (0.00 sec) |
测试结果:客户端服务器上的数据修改,会同步到所有服务器中。
标签:主从复制,读写,MySQL,192.168,sec,Master,mysql,test,服务器 From: https://www.cnblogs.com/ponyton/p/16968313.html