欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MySQL数据库的主从同步和读写分离

程序员文章站 2024-03-20 21:05:28
...

MySQL数据库的主从同步和读写分离

1.案例

企业拓扑图

MySQL数据库的主从同步和读写分离

1.在上述拓扑图中,后端MySQL数据库只有一台时,会有以下问题:

单点故障,导致服务不可用

无法处理大量的并发数据请求

数据丢失是大灾难

改造过后企业的拓扑图

MySQL数据库的主从同步和读写分离

2.改造的办法

增加MySQL数据库的服务器,对数据进行备份,形成主备

确保主备MySQL数据库服务器数据是一样的

主服务器宕机了,备份服务器继续工作,数据有保障

更高级的解决方案的拓扑图

MySQL数据库的主从同步和读写分离

重点:

1.我们需要主从同步账号

2.节点被调度的权限或者账号

3.代理账号

2.主从同步

1.主从同步的类型

1.基于语句的同步(默认)

在主服务器上执行的语句,从服务器执行同样的语句

2.基于行的同步

把改变的内容复制到从服务器

3.混合类型的复制

一旦发现基于语句无法精确复制时,就会采用基于行的复制

2.主从复制的工作过程

MySQL数据库的主从同步和读写分离

3.案例实施

1.配置MySQL master服务器

[aaa@qq.com ~]# hostnamectl set-hostname master
[aaa@qq.com ~]# su
//安装ntp,同步时间
[aaa@qq.com ~]# yum -y install ntp
//配置ntp的主配置文件
[aaa@qq.com ~]# vim /etc/ntp.conf
//本地时钟源
server 127.127.195.0  
//设置时间层级为8
fudge 127.127.195.0 stratum 8 
[aaa@qq.com ~]# systemctl start ntpd
//关闭防火墙
[aaa@qq.com ~]# systemctl stop firewalld
[aaa@qq.com ~]# setenforce 0
//安装MySQL5.7
//安装MySQL的源码编译包
[aaa@qq.com ~]# yum -y install ncurses ncurses-devel bison cmake gcc gcc-c++
//创建MySQL程序用户
[aaa@qq.com ~]# useradd -s /sbin/nologin mysql
//将压缩包解压到/opt目录下面
[aaa@qq.com ~]# tar -zxvf mysql-boost-5.7.20.tar.gz -C /opt
[aaa@qq.com ~]# cd /opt/mysql-5.7.20/
//cmake 编译安装MySQL5.7
[aaa@qq.com mysql-5.7.20]# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
[aaa@qq.com mysql-5.7.20]# make 
[aaa@qq.com mysql-5.7.20]# make install
//将/usr/local/mysql目录下面的所有文件的属主和属组都给mysql
[aaa@qq.com mysql-5.7.20]# chown -R mysql:mysql /usr/local/mysql/
//配置MySQL的my.cnf文件
[aaa@qq.com mysql-5.7.20]# vi /etc/my.cnf
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

//修改my.cnf的属主和属组
[aaa@qq.com mysql-5.7.20]# chown mysql:mysql /etc/my.cnf
//配置mysql的环境变量
[aaa@qq.com mysql-5.7.20]# echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
[aaa@qq.com mysql-5.7.20]# echo 'export PATH' >> /etc/profile
//加载环境变量
[aaa@qq.com mysql-5.7.20]# source /etc/profile
//初始化mysql数据库
[aaa@qq.com mysql-5.7.20]# cd /usr/local/mysql/
[aaa@qq.com mysql]# bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
[aaa@qq.com mysql]# cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
[aaa@qq.com mysql]# systemctl enable mysqld
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
[aaa@qq.com mysql]# netstat -ntap | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      22547/mysqld 
[aaa@qq.com mysql]# vim /etc/my.cnf
server-id  = 11
log-bin=master-bin
log-slave-updates=true
[aaa@qq.com mysql]# systemctl restart mysqld
[aaa@qq.com ~]# mysql -uroot -pabc123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20-log Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> grant replication slave on *.* to 'myslave'@'192.168.73.%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |      604 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> grant all on *.* to aaa@qq.com'192.168.73.%' identified by '123.com';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)


2.配置MySQL从服务器slave1

//MySQL5.7的安装参照master的安装
[aaa@qq.com mysql]# hostnamectl set-hostname slave1
[aaa@qq.com mysql]# su
[aaa@qq.com mysql]# yum -y install ntp ntpdate
[aaa@qq.com mysql]# systemctl stop firewalld
[aaa@qq.com mysql]# setenforce 0
[aaa@qq.com mysql]# /usr/sbin/ntpdate 192.168.73.141
 9 Jan 11:16:04 ntpdate[5477]: the NTP socket is in use, exiting
[aaa@qq.com mysql]# vim /etc/my.cnf
server-id = 22
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
[aaa@qq.com mysql]# systemctl restart mysqld
[aaa@qq.com mysql]# mysql -u root -pabc123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> change master to master_host='192.168.73.141',master_user='myslave',master_password='123456'',master_log_file='master-bin.000001',master_log_pos=604;
Query OK, 0 rows affected, 2 warnings (0.04 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> grant all on *.* to aaa@qq.com'192.168.73.%' identified by '123.com';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.73.141
                  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.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Connecting
            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: 604
              Relay_Log_Space: 154
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 1130
                Last_IO_Error: error connecting to master 'aaa@qq.com:3306' - retry-time: 60  retries: 9
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
                  Master_UUID: 
             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: 200109 11:29:56
     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)

mysql> grant all on *.* to aaa@qq.com'192.168.73.%' identified by '123.com';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

3.配置MySQL从服务器slave2

[aaa@qq.com mysql]# hostnamectl set-hostname slave2
[aaa@qq.com mysql]# su
[aaa@qq.com mysql]# yum -y install ntp ntpdate
[aaa@qq.com mysql]# systemctl start ntpd
[aaa@qq.com mysql]# systemctl stop firewalld
[aaa@qq.com mysql]# setenforce 0
[aaa@qq.com mysql]# /usr/sbin/ntpdate 192.168.73.141
 9 Jan 11:27:05 ntpdate[23734]: the NTP socket is in use, exiting
[aaa@qq.com mysql]# vim /etc/my.cnf
server-id = 23
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
[aaa@qq.com mysql]# systemctl restart mysqld
[aaa@qq.com mysql]# mysql -uroot -pabc123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> change master to master_host='192.168.73.141',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=604;
Query OK, 0 rows affected, 2 warnings (0.12 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.73.141
                  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.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: master-bin.000001
             Slave_IO_Running: Connecting
            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: 604
              Relay_Log_Space: 154
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 1130
                Last_IO_Error: error connecting to master 'aaa@qq.com:3306' - retry-time: 60  retries: 1
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
                  Master_UUID: 
             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: 200109 11:33:22
     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)

mysql> grant all on *.* to aaa@qq.com'192.168.73.%' identified by '123.com';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

4.测试主从同步

在master主机上

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.11 sec)

mysql> create database school;
Query OK, 1 row affected (0.10 sec)

mysql> use school;
Database changed
mysql> create table info (id int,name char(20));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into info (id,name) values(1,'1hao');
Query OK, 1 row affected (0.01 sec)

mysql> insert into info (id,name) values(2,'2hao');
Query OK, 1 row affected (0.01 sec)

mysql> select * from info;
+------+------+
| id   | name |
+------+------+
|    1 | 1hao |
|    2 | 2hao |
+------+------+
2 rows in set (0.00 sec)

在slave1主机上

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use school;
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 info;
+------+------+
| id   | name |
+------+------+
|    1 | 1hao |
|    2 | 2hao |
+------+------+
2 rows in set (0.00 sec)

在slave2主机上

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use school;
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 info;
+------+------+
| id   | name |
+------+------+
|    1 | 1hao |
|    2 | 2hao |
+------+------+
2 rows in set (0.00 sec)

3.读写分离

1.读写分离的原理

1.读写分离就是只在主服务器上写,只在从服务器上读

2.主数据库处理事务性查询,而从数据库处理select查询

3.数据库复制被用来把事务性查询导致的变更同步到集群中的从数据库

2.案例实施

1.搭建amoeba代理服务器

[aaa@qq.com ~]# hostnamectl set-hostname amoeba
[aaa@qq.com ~]# su
[aaa@qq.com ~]# ls
amoeba-mysql-binary-2.2.0.tar.gz  jdk-6u14-linux-x64.bin     模板  文档  桌面
anaconda-ks.cfg                   mysql-boost-5.7.20.tar.gz  视频  下载
initial-setup-ks.cfg              公共                       图片  音乐
[aaa@qq.com ~]# systemctl stop firewalld
[aaa@qq.com ~]# setenforce 0
[aaa@qq.com ~]# cp jdk-6u14-linux-x64.bin /usr/local
[aaa@qq.com ~]# cd /usr/local
[aaa@qq.com local]# ls
bin  etc  games  include  jdk-6u14-linux-x64.bin  lib  lib64  libexec  mysql  sbin  share  src
[aaa@qq.com local]# chmod +x jdk-6u14-linux-x64.bin 
[aaa@qq.com local]# ./jdk-6u14-linux-x64.bin 
Please enter "yes" or "no".
Do you agree to the above license terms? [yes or no]
yes
[aaa@qq.com local]# ls
bin  games    jdk1.6.0_14             lib    libexec  sbin   src
etc  include  jdk-6u14-linux-x64.bin  lib64  mysql    share
[aaa@qq.com local]# mv jdk1.6.0_14/ jdk1.6
[aaa@qq.com local]# ls
bin  games    jdk1.6                  lib    libexec  sbin   src
etc  include  jdk-6u14-linux-x64.bin  lib64  mysql    share
[aaa@qq.com local]# echo '
> 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' >> /etc/profile
[aaa@qq.com local]# source /etc/profile
[aaa@qq.com 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)
[aaa@qq.com local]# mkdir /usr/local/amoeba
[aaa@qq.com local]# cd
[aaa@qq.com ~]# tar -zxvf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba
[aaa@qq.com ~]# chmod -R 755 /usr/local/amoeba
[aaa@qq.com ~]# /usr/local/amoeba/bin/amoeba
amoeba start|stop
[aaa@qq.com ~]# cd /usr/local/amoeba
[raaa@qq.com amoeba]# vim conf/amoeba.xml
 30                                         <property name="user">amoeba</property>
 31 
 32                                         <property name="password">123456</property>
117                 <property name="defaultPool">master</property>
118                 <property name="writePool">master</property>
119                 <property name="readPool">slaves</property>
[aaa@qq.com amoeba]# vim conf/amoeba.xml
 26                         <property name="user">test</property>
 27                         
 28                         
 29                         <property name="password">123.com</property>
 45         <dbServer name="user"  parent="abstractServer">
 46                 <factoryConfig>
 47                         <!-- mysql ip -->
 48                         <property name="ipAddress">192.168.73.141</property>
 49                 </factoryConfig>
 50         </dbServer>
 45         <dbServer name="master"  parent="abstractServer">
 46                 <factoryConfig>
 47                         <!-- mysql ip -->
 48                         <property name="ipAddress">192.168.73.141</property>
 49                 </factoryConfig>
 50         </dbServer>
 51 
 52         <dbServer name="slave1"  parent="abstractServer">
 53                 <factoryConfig>
 54                         <!-- mysql ip -->
 55                         <property name="ipAddress">192.168.73.138</property>
 56                 </factoryConfig>
 57         </dbServer>
 58         
 59         <dbServer name="slave2"  parent="abstractServer">
 60                 <factoryConfig>
 61                         <!-- mysql ip -->
 62                         <property name="ipAddress">192.168.73.142</property>
 63                 </factoryConfig>
 64         </dbServer>
 65
 66         <dbServer name="slaves" virtual="true">
 67                 <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">
 68                         <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->
 69                         <property name="loadbalance">1</property>
 70 
 71                         <!-- Separated by commas,such as: server1,server2,server1 -->
 72                         <property name="poolNames">slave1,slave2</property>
 73                 </poolConfig>
 74         </dbServer>
[aaa@qq.com ~]# netstat -ntap | grep java
tcp6       0      0 :::8066                 :::*                    LISTEN      6097/java           
tcp6       0      0 127.0.0.1:24038         :::*                    LISTEN      6097/java           
tcp6      37      0 192.168.73.140:45008    192.168.73.141:3306     CLOSE_WAIT  5838/java           
tcp6       0      0 192.168.73.140:42792    192.168.73.141:3306     ESTABLISHED 5975/java           
tcp6       0      0 192.168.73.140:54100    192.168.73.142:3306     ESTABLISHED 6097/java           
tcp6       0      0 192.168.73.140:35558    192.168.73.138:3306     ESTABLISHED 5975/java           
tcp6       0      0 192.168.73.140:44924    192.168.73.141:3306     ESTABLISHED 6097/java           
tcp6       0      0 192.168.73.140:51968    192.168.73.142:3306     ESTABLISHED 5975/java           
tcp6       0      0 192.168.73.140:37690    192.168.73.138:3306     ESTABLISHED 6097/java

2.在客户机上面

[aaa@qq.com ~]# hostnamectl set-hostname client
[aaa@qq.com ~]# su
[aaa@qq.com ~]# yum -y install mysql
[aaa@qq.com ~]# systemctl stop firewalld
[aaa@qq.com ~]# setenforce 0
[aaa@qq.com ~]# mysql -u amoeba -p 123456 -h 192.168.73.140 -P8066
Enter password: \\密码123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1218932998
Server version: 5.1.45-mysql-amoeba-proxy-2.2.0

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    1218932998
Current database: *** NONE ***

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

MySQL [(none)]> use school;
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 [school]> select * from info;
+------+------+
| id   | name |
+------+------+
|    1 | 1hao |
|    2 | 2hao |
+------+------+
2 rows in set (0.01 sec)

3.关闭slave的slave

//slave2
[aaa@qq.com ~]# mysql -uroot -pabc123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1534
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)

mysql> use school;
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 info (id,name) values(4,'4hao');
Query OK, 1 row affected (0.01 sec)
//slave1
[aaa@qq.com ~]# mysql -uroot -pabc123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1534
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use school;
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 info (id,name) values (3,'3hao');
Query OK, 1 row affected (0.00 sec)

4.在client里面测试

MySQL [school]> select * from info;
+------+------+
| id   | name |
+------+------+
|    1 | 1hao |
|    2 | 2hao |
|    4 | 4hao |
+------+------+
3 rows in set (0.02 sec)

MySQL [school]> select * from info;
+------+------+
| id   | name |
+------+------+
|    1 | 1hao |
|    2 | 2hao |
|    3 | 3hao |
+------+------+
3 rows in set (0.01 sec)

MySQL [school]> select * from info;
+------+------+
| id   | name |
+------+------+
|    1 | 1hao |
|    2 | 2hao |
|    4 | 4hao |
+------+------+
3 rows in set (0.00 sec)

MySQL [school]> select * from info;
+------+------+
| id   | name |
+------+------+
|    1 | 1hao |
|    2 | 2hao |
|    3 | 3hao |
+------+------+
3 rows in set (0.00 sec)

从这个测试中,我们可以看出: MySQL对读请求做了负载均衡,对多次请求采用了轮询算法,所有的用于处理读请求的MySQL会轮流(或者按权重)来响应 。