溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MySQL主從復制與讀寫分離的原理

發(fā)布時間:2020-06-08 10:07:49 來源:億速云 閱讀:1348 作者:Leah 欄目:MySQL數(shù)據(jù)庫

這篇文章主要為大家分享MySQL主從復制與讀寫分離的原理。文中還介紹了如何配置和驗證主從復制與讀寫分離的實驗,希望大家通過這篇文章能有所收獲。

MySQL主從復制與讀寫分離

一、前言

前面我們已經(jīng)對MySQL數(shù)據(jù)庫的安裝、命令、備份、索引、事務以及存儲引擎等各個方面有了初步的理解,而今天我們將從宏觀的角度來理解什么是MySQL數(shù)據(jù)庫的主從復制與讀寫分離。

二、場景描述

在實際的生產(chǎn)環(huán)境中,如果對MySQL數(shù)據(jù)庫的讀與寫都在一臺服務器上進行操作,無論是在安全性,高可用性、還是高并發(fā)性等諸多方面都是無法滿足的;而這就需要對MySQL數(shù)據(jù)庫進行主從復制與讀寫分離。

我們使用一個架構圖來引出MySQL數(shù)據(jù)庫主從復制的原理以及其作用,請看下圖:

MySQL主從復制與讀寫分離的原理

服務器作用:

MySQL主服務器:負責數(shù)據(jù)的寫入;

MySQL從服務器:同步主服務器的數(shù)據(jù)并進行數(shù)據(jù)的輪循讀??;

那么這三臺具備相同服務的MySQL服務器就稱為“MySQL群集”。我們可以從上圖中看出,這樣的安排實現(xiàn)數(shù)據(jù)同步的基礎上,實現(xiàn)數(shù)據(jù)的讀寫分離,從而在保證數(shù)據(jù)的可靠性的同時也大大減輕了主服務器的壓力。

下面我們對MySQL的主從復制和讀寫分離進行逐一介紹并給出配置實例。

三、MySQL主從復制原理

MySQL主從復制與讀寫分離有著緊密的聯(lián)系,可以這么說,MySQL的讀寫分離的實現(xiàn)需要基于主從復制的基礎之上。

3.1MySQL支持的復制類型

  1. 基于語句的復制;——基于SQL語言命令的復制形式,使用SQL命令執(zhí)行復制,效率高
  2. 基于行的復制;——復制數(shù)據(jù)庫變化的內(nèi)容,不是執(zhí)行命令
  3. 混合類型的復制;——默認采用語句類型的復制,如果發(fā)現(xiàn)有不確定問題等其他原因造成無法復制的情況再進行基于行的復制

3.2MySQL復制的工作過程

我們通過下圖來理解MySQL主、從服務器是如何進行復制同步的過程的。

MySQL主從復制與讀寫分離的原理

1)首先,MySQL主服務器在更新數(shù)據(jù)庫或其他進行數(shù)據(jù)庫相關操作時,會在二進制日志文件中記錄這些改變(這我們在前面的增量備份與恢復的文章中進行了講述,log-bin的設置以及如何使用mysqladmin命令刷新該日志。)當寫入日志完成后,主服務器會告知存儲引擎提交事務;

2)MySQL從服務器會將主服務器的二進制日志文件(Binary log)復制到其中繼日志(Relay log)中。中繼日志通常存放在系統(tǒng)緩存中,因此中繼日志的開銷很??;

3)從服務器通過自身線程從中繼日志中讀取事件,更新自身的日志文件使其與主服務器中的數(shù)據(jù)一致。

ps:復制過程中有一個很重要的限制,即在從服務器上復制是串行化的,這就表明主服務器上的并行更新操作不能在從服務器上并行操作。

四、MySQL主從復制配置流程與實際操作

4.1主從復制所需環(huán)境

所需設備(我們在虛擬機上模擬進行配置)清單:

Centos7一臺:作為主服務器——master,ip地址為192.168.68.133

Centos7兩臺:作為從服務器——slave1、slave2IP地址分別為192.168.68.129、192.168.68.132

所需安裝服務:

安裝并且配置ntp服務、都安裝了MySQL5.7版本的數(shù)據(jù)庫

4.2主從復制具體流程步驟

4.2.1準備工作

首先,我們聯(lián)想一下增量備份的恢復操作,我們都是依賴于data目錄下的二進制日志文件,通過兩種方法實現(xiàn)的,其中一種就是根據(jù)時間節(jié)點進行備份恢復操作的。那么我們需要進行MySQL主從服務器復制,就需要先同步所有MySQL服務器的系統(tǒng)時間。

4.2.1.1主服務器準備工作的配置

1)服務器名稱設置以區(qū)別

hostnamectl set-hostname master
su

2)安裝配置ntp服務

yum install ntp -y 
#修改ntp服務配置文件,添加下面的兩句
vim /etc/ntp.conf

server 127.127.68.0         #服務器本地網(wǎng)段,127.127表示的就是192.168
fudge 127.127.68.0 stratum 8 #時區(qū)設置(東八區(qū))
#保存退出
systemctl start ntpd
#可以使用netstat -nutp | grep ntpd 命令查看服務開啟狀態(tài)

3)關閉防火墻和SELinux功能

systemctl stop firewalld
setenforce 0
4.2.1.2兩臺從服務器的準備工作的配置(二者步驟一致)

1)服務器名稱設置

hostnamectl set-hostname slave1(slave2)
su

2)兩臺從服務器上安裝ntp和ntpdate服務并開啟服務

yum install ntp ntpdate -y
systemctl start ntpd

3)兩臺從服務器上關閉防火墻和SELinux功能

systemctl stop firewalld
setenforce 0

4)使用ntpdate命令進行時間同步

[root@slave1 ~]# /usr/sbin/ntpdate 192.168.68.133
 9 Jan 15:35:13 ntpdate[67450]: the NTP socket is in use, exiting
4.2.2MySQL服務的配置
4.2.2.1主服務器上MySQL的修改與配置

1)修改mysql的主配置文件并重啟mysql服務

vim /etc/my.cnf
#配置如下:
log-bin = master-bin  #二進制日志文件 master-bin可以自己設置
server-id = 1         #服務器的id號,用于區(qū)別
log-slave-updates=true #開啟從服務器更新日志功能(結合復制流程連接)
systemctl restart mysqld.service

2)進入數(shù)據(jù)庫進行權限設置(授權)與刷新

[root@lokott ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17-log Source distribution

Copyright (c) 2000, 2016, 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.68.%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;#刷新系統(tǒng)權限表
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 |      603 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

注意核心命令:授權命令的解釋,還有以上位置Position 記錄數(shù)據(jù)603

命令格式:

grant replication slave on *.* to 'myslave'@'192.168.68.%' identified by '123456';
#grant ——授權命令   replication slave  從服務器復制操作     *.*表示的是所有數(shù)據(jù)庫所有表

其具體含義是:賦予192.168.68.0網(wǎng)段的主機(服務器)復制(同步)主服務器的所有數(shù)據(jù)庫數(shù)據(jù);

4.2.2.2從服務器上MySQL的修改與配置

1)修改主配置文件并重啟服務

vim /etc/my.cnf
#配置如下
log-bin = mysql-bin
server-id = 2
relay-log = relay-log-bin     #中繼日志
relay-log-index = slave-relay-log-bin.index #索引文件

#保存退出重啟
systemctl restart mysqld.service

2)進入數(shù)據(jù)庫配置同步

[root@slave1 ~]# mysql -uroot -p
...#省略部分內(nèi)容
mysql> change master to master_host='192.168.68.133',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=603;
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.68.133
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 603
               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
 ...#省略部分內(nèi)容 上面的兩行表示從服務器給出的IO線程和數(shù)據(jù)庫SQL語句都在運行狀態(tài)
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)
4.2.3進行主從復制的測試與驗證

在主服務器上創(chuàng)建一個新的數(shù)據(jù)庫,新建表和數(shù)據(jù);在服務器上進行查看;

主服務器:

create database test;
Query OK, 1 row affected (0.00 sec)

從服務上slave1查詢:

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

mysql> exit
Bye
[root@slave1 ~]# 

從服務器slave2上查詢:

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

mysql> exit
Bye
[root@slave2 ~]# 

4.3主從復制小結

該配置實現(xiàn)的是從服務器通過主服務器給予的權限進行復制操作,我們需要通過實驗配置理解上述的主從復制的原理以及其復制的過程。只有在理解掌握了如何進行MySQL數(shù)據(jù)庫的主從復制,才能理解和進行MySQL讀寫分離的配置操作。

五、MySQL讀寫分離原理

讀寫分離的原理,簡單來說就是實現(xiàn)上圖中,在主服務器上寫數(shù)據(jù),使用從服務器輪循讀取數(shù)據(jù)的功能。

我們結合下圖來理解讀寫分離的過程

MySQL主從復制與讀寫分離的原理

基于代理層實現(xiàn):代理一般位于客戶端與服務器之間,代理服務器接到客戶端請求通過判斷后轉(zhuǎn)發(fā)到后端數(shù)據(jù)庫,有兩個代表性程序。

1)MySQL_Proxy:通過自帶的lua腳本進行SQL判斷;

2)Amoeba:Java語言開發(fā),阿里巴巴將其用于生產(chǎn)環(huán)境,不支持事務與存儲過程;

六、MySQL讀寫分離配置流程與實際操作

環(huán)境:基于上面主從復制的流程進行后續(xù)陪配置

添加設備:Centos7兩臺:其中一臺作為Amoeba代理服務器,另一臺作為客戶端測試服務器。

所需軟件包:在Amoeba代理服務器上需要安裝jdk和amoeba相關環(huán)境及應用(amoeba使用Java開發(fā))

1)安裝jdk環(huán)境和ameoba環(huán)境

[root@amoeba ~]# cd tar/
[root@amoeba tar]# ls
amoeba-mysql-binary-2.2.0.tar.gz  jdk-6u14-linux-x64.bin 
 cd tar/
 ls
 cp jdk-6u14-linux-x64.bin /usr/local/
 cd /usr/local/
 chmod +x jdk-6u14-linux-x64.bin 
 ./jdk-6u14-linux-x64.bin 
 mv jdk1.6.0_14/ /usr/local/jdk1.6
 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

 source /etc/profile
 mkdir /usr/local/amoeba
 #回到壓縮包目錄下解壓amoeba
 tar zxf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba
 cd /usr/local/

2)配置amoeba相關參數(shù)

配置路徑在/usr/local/amoeba/conf中,配置文件為ameoba.xml,dbServers.xml。前者為amoeba主配置文件(還記得tomcat嗎?),后者是數(shù)據(jù)庫服務器的相關配置文件。

1、更改ameoba.xml

30                                         <property name="user">amoeba</property>
31 
32                                         <property name="password">123456</property>
#user是我們在client端登錄amoeba代理服務器的身份名稱,password是登錄密碼,待會我們將會在client端上登錄
15                 <property name="defaultPool">master</property>
116 
117                 <property name="writePool">master</property>
118                 <property name="readPool">slaves</property>
119 
#defaultPool表示默認的服務器 writePool表示指定寫服務器(組),readPool表示指定讀服務器(組),slaves將在dbServers.xml文件中配置

2、更改dbServers.xml

 26                         <property name="user">test</property>
 27 
 28                         <!--  mysql password -->
 29                         <property name="password">123123</property>

 45        <dbServer name="master"  parent="abstractServer">
 46                 <factoryConfig>
 47                         <!-- mysql ip -->
 48                         <property name="ipAddress">192.168.68.133</property>
 49                 </factoryConfig>
 50         </dbServer>
 51 
 52         <dbServer name="slave1"  parent="abstractServer">
 53                 <factoryConfig>
 54                         <!-- mysql ip -->
 55                         <property name="ipAddress">192.168.68.129</property>
 56                 </factoryConfig>
 57         </dbServer>
 58 
 59         <dbServer name="slave2"  parent="abstractServer">
 60                 <factoryConfig>
 61                         <!-- mysql ip -->
 62                         <property name="ipAddress">192.168.68.132</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>

3、開啟amoeba(可以在另一個terminal查看端口netstat -natp | grep 8066)

[root@amoeba bin]# amoeba start &
[1] 121608
[root@amoeba bin]# log4j:WARN log4j config load completed from file:/usr/local/amoeba/conf/log4j.xml
2020-01-10 08:20:03,413 INFO  context.MysqlRuntimeContext - Amoeba for Mysql current versoin=5.1.45-mysql-amoeba-proxy-2.2.0
log4j:WARN ip access config load completed from file:/usr/local/amoeba/conf/access_list.conf
2020-01-10 08:20:03,566 INFO  net.ServerableConnectionManager - Amoeba for Mysql listening on 0.0.0.0/0.0.0.0:8066.
2020-01-10 08:20:03,567 INFO  net.ServerableConnectionManager - Amoeba Monitor Server listening on /127.0.0.1:39466.

4、客戶機client端上安裝mysql數(shù)據(jù)庫,登錄查看

[root@localhost ~]# mysql -u amoeba -p123456 -h 192.168.68.144 -P8066
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 2023306452
Server version: 5.1.45-mysql-amoeba-proxy-2.2.0 Source distribution

Copyright (c) 2000, 2016, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> use test;
Database changed
mysql> show tables;
Empty set (0.01 sec)

#因為此時我們還沒有創(chuàng)建如何數(shù)據(jù)表和數(shù)據(jù),所以在客戶端上是查看不到任何數(shù)據(jù)的。

5、我們在主服務器上創(chuàng)建一個表并且在從服務器上查看是否存在這個表,存在則主從復制正常

主服務器:

mysql> use test;
Database changed
mysql> create table info (id int(5) not null primary key auto_increment,name varchar(10) not null);
Query OK, 0 rows affected (0.02 sec)

mysql> desc info;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(5)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(10) | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)

mysql> select * from info;
Empty set (0.00 sec)

從服務器:

[root@slave1 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.17-log Source distribution

Copyright (c) 2000, 2016, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.01 sec)

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 |
+----------------+
| info           |
+----------------+
1 row in set (0.00 sec)

6、在client端上查看這個表

mysql> show tables;
Empty set (0.01 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| info           |
+----------------+
1 row in set (0.00 sec)

7、那么我們?nèi)绾悟炞C讀寫分離呢?還記得我們在從服務器上的start slave命令嗎?我們可以停止主從復制,使用stop slave即可

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

8、我們在client端插入新的數(shù)據(jù),分別在主從服務器上進行查看

client端:

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

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

主服務器:

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

從服務器:

mysql> select * from info;
Empty set (0.00 sec)
主從服務器寫分離小結

從以上的實驗中,我們可以得出結論,經(jīng)過主從復制與寫分離的配置,我們可以將寫數(shù)據(jù)的任務在主服務器上進行,主服務器寫入完成后,由從服務器經(jīng)過主從復制的過程進行(start slave)復制,因為在生產(chǎn)環(huán)境中我們不能stop slave(停止主從復制)的?。?!。但是為了驗證讀寫分離原理,我們只能先stop slave了。

下面驗證讀分離。

我們在從服務器上分別寫入一些數(shù)據(jù)(兩臺從服務器上寫的不一樣)

slave1:

mysql> insert into info (name) values('lisi');
Query OK, 1 row affected (0.00 sec)

mysql> select * from info;
+----+------+
| id | name |
+----+------+
|  1 | lisi |
+----+------+
1 row in set (0.00 sec)

slave2:

mysql> insert into info (name) values('wangwu');
Query OK, 1 row affected (0.00 sec)

mysql> select * from info;
+----+--------+
| id | name   |
+----+--------+
|  1 | wangwu |
+----+--------+
1 row in set (0.00 sec)

client 查詢:(可能會有問題,因為是采用了auto_increment自增列)

因此我們在從服務器上開啟復制功能,在主從服務器上修改字段,然后在從服務器上關閉復制功能,并且重新寫入不同的數(shù)據(jù),在client端查看。

兩臺從服務器:

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

主服務器:

mysql> alter table info modify id int(5) not null;
Query OK, 1 row affected (0.02 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> desc info;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(5)      | NO   | PRI | NULL    |       |
| name  | varchar(10) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> delete from info;
Query OK, 1 row affected (0.00 sec)

從服務器:

mysql> alter table info modify id int(5) not null;
Query OK, 1 row affected (0.02 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> desc info;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(5)      | NO   | PRI | NULL    |       |
| name  | varchar(10) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> delete from info;
Query OK, 1 row affected (0.00 sec)

client :重新寫入數(shù)據(jù)

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

主服務器:

mysql> select * from info;
+----+----------+
| id | name     |
+----+----------+
|  1 | zhangsan |
+----+----------+
1 row in set (0.00 sec)
#此時需要重新查看狀態(tài)記錄position,此時如下:
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |     2955 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

從服務器:

mysql> change master to master_host='192.168.68.133',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=2955;
ERROR 3021 (HY000): This operation cannot be performed with a running slave io thread; run STOP SLAVE IO_THREAD FOR CHANNEL '' first.
mysql> STOP SLAVE IO_THREAD FOR CHANNEL '';
Query OK, 0 rows affected (0.00 sec)

mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host='192.168.68.133',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=2955;
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.68.133
                  Master_User: myslave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 2955
               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
              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: 2955
              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: 5bb93767-328a-11ea-820a-000c290bd936
             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)

算了,我們將三個服務器上的數(shù)據(jù)表的數(shù)據(jù)都刪了重新進行主從復制吧;按照主從復制執(zhí)行之后繼續(xù)進行讀分離的操作;

關閉從服務器主從復制:

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

從服務器上寫入不同數(shù)據(jù):

slave1:

mysql> insert into info values(2,'lisi');
Query OK, 1 row affected (0.00 sec)

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

slave2:

mysql> insert into info values(3,'wangwu');
Query OK, 1 row affected (0.00 sec)

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

client:

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

mysql> select * from info;
+----+----------+
| id | name     |
+----+----------+
|  1 | zhangsan |
|  3 | wangwu   |
+----+----------+
2 rows in set (0.00 sec)
讀分離小結

MySQL讀分離則是在MySQL的從服務器上進行數(shù)據(jù)讀取操作。其中兩臺做負載均衡,輪循讀取,減輕壓力,提高并發(fā)訪問。

關于MySQL主從復制與讀寫分離就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI