【MySQL入门指北】主从复制及读写分离


文章目录

  • MySQL 主从复制及读写分离
    • 一、集群架构设计
    • 二、主从模式
    • 三、MySQL主从复制原理
    • 四、集群案列
      • 1.实验环境
      • 2.实验准备
      • 3.一主一从
      • 4.双主双从
    • 五、代理技术
      • 1.代理简介
      • 2.Mycat 实战练习


【MySQL入门指北】主从复制及读写分离_第1张图片


MySQL 主从复制及读写分离

一、集群架构设计

1、架构设计理念

在集群架构设计时,主要遵从下面三个维度:

1.可用性
2.扩展性
3.一致性

2、可用性设计

1.站点高可用,冗余站点
2.服务高可用,冗余服务
3.数据高可用,冗余数据

保证高可用的方法是冗余。但是数据冗余带来的问题是数据一致性问题。

实现高可用的方案有以下几种架构模式:

主从模式
简单灵活,能满足多种需求。比较主流的用法,但是写操作高可用需要自行处理。

双主模式
互为主从,有双主双写、双主单写两种方式,建议使用双主单写

3、扩展性设计

扩展性主要围绕着读操作扩展和写操作扩展展开。

如何扩展以提高读性能
加从库
简单易操作,方案成熟。
从库过多会引发主库性能损耗。建议不要作为长期的扩充方案,应该设法用良好的设计避免持续加从库来缓解读性能问题。
分库分表
可以分为垂直拆分和水平拆分,垂直拆分可以缓解部分压力,水平拆分理论上可以无限扩展。
如何扩展以提高写性能
分库分表
4、一致性设计

一致性主要考虑集群中各数据库数据同步以及同步延迟问题。可以采用的方案如下:

不使用从库
扩展读性能问题需要单独考虑,否则容易出现系统瓶颈。
增加访问路由层
可以先得到主从同步最长时间t,在数据发生修改后的t时间内,先访问主库。

二、主从模式

1、适用场景

MySQL主从模式是指数据可以从一个MySQL数据库服务器主节点复制到一个或多个从节点。MySQL 默认采用异步复制方式,这样从节点不用一直访问主服务器来更新自己的数据,从节点可以复制主数据库中的所有数据库,或者特定的数据库,或者特定的表。

【MySQL入门指北】主从复制及读写分离_第2张图片
数据库为什么需要主从架构呢?

1.高可用,实时灾备,用于故障切换。比如主库挂了,可以切从库。
2.读写分离,提供查询服务,减少主库压力,提升性能
3.备份数据,避免影响业务。

mysql主从复制用途:

1.实时灾备,用于故障切换(高可用)
2.读写分离,提供查询服务(读扩展)
3.数据备份,避免影响业务(高可用)
4.主从部署必要条件:

从库服务器能连通主库
主库开启binlog日志(设置log-bin参数)
主从server-id不同

三、MySQL主从复制原理

主从复制原理,简言之,分三步曲进行:

主数据库有个 bin log 二进制文件,记录了所有增删改 SQL 语句。(binlog线程)
从数据库把主数据库的 bin log 文件的 SQL 语句复制到自己的中继日志 relay log(io线程)
从数据库的 relay log 重做日志文件,再执行一次这些sql语句。(sql执行线程)

详细的主从复制过程如图:

【MySQL入门指北】主从复制及读写分离_第3张图片

重点

上图主从复制过程分了五个步骤进行:

1.主库的更新SQL(update、insert、delete)被写到binlog
2.从库发起连接,连接到主库。
3.此时主库创建一个 binlog dump thread,把 bin log 的内容发送到从库。
4.从库启动之后,创建一个 I/O 线程,读取主库传过来的 bin log 内容并写到 relay log
5.从库还会创建一个SQL线程,从 relay log 里面读取内容,(将其重放到备库数据库之上)从 ExecMasterLog_Pos 位置开始执行读取到的更新事件,将更新内容写入到 slave 的db

四、集群案列

1.实验环境

服务器名称 ip
mysql-master1 192.168.200.182
mysql-master2 192.168.200.183
mysql-slave1 192.168.200.184
mysql-slave2 192.168.200.185
mysql-other 192.168.200.186

2.实验准备

1.全新服务器-互相通信

2.配置域名解析(5台服务器分别进行配置)

[root@mysql-slave1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.182 mysql-master1
192.168.200.183 mysql-master2
192.168.200.184 mysql-slave1
192.168.200.185 mysql-slave2
192.168.200.186 mysql-other

3.全新安装mysql57-分别安装

在每台服务器找到mysql的默认密码,然后更改密码

grep password /var/log/mysqld.log

删除MySQL数据库

[root@mysql-slave1 ~]# rpm -qa | grep -i mysql
mysql-community-client-5.7.40-1.el7.x86_64
zabbix-web-mysql-scl-5.0.28-1.el7.noarch
mysql-community-embedded-5.7.40-1.el7.x86_64
mysql-community-embedded-devel-5.7.40-1.el7.x86_64
zabbix-server-mysql-5.0.28-1.el7.x86_64
mysql-community-server-5.7.40-1.el7.x86_64
zabbix-proxy-mysql-5.0.28-1.el7.x86_64
mysql-community-libs-5.7.40-1.el7.x86_64
mysql-community-libs-compat-5.7.40-1.el7.x86_64
rh-php72-php-mysqlnd-7.2.24-1.el7.x86_64
mysql-community-devel-5.7.40-1.el7.x86_64
mysql-community-test-5.7.40-1.el7.x86_64
mysql-community-embedded-compat-5.7.40-1.el7.x86_64
perl-DBD-MySQL-4.023-6.el7.x86_64
mysql80-community-release-el7-7.noarch
mysql-community-common-5.7.40-1.el7.x86_64
[root@mysql-slave1 ~]# yum remove mysql-* -y
已加载插件:fastestmirror
正在解决依赖关系
--> 正在检查事务
---> 软件包 mysql-community-client.x86_64.0.5.7.40-1.el7 将被 删除
---> 软件包 mysql-community-common.x86_64.0.5.7.40-1.el7 将被 删除
---> 软件包 mysql-community-devel.x86_64.0.5.7.40-1.el7 将被 删除
---> 软件包 mysql-community-embedded.x86_64.0.5.7.40-1.el7 将被 删除
---> 软件包 mysql-community-embedded-compat.x86_64.0.5.7.40-1.el7 将被 删除
---> 软件包 mysql-community-embedded-devel.x86_64.0.5.7.40-1.el7 将被 删除
---> 软件包 mysql-community-libs.x86_64.0.5.7.40-1.el7 将被 删除
---> 软件包 mysql-community-libs-compat.x86_64.0.5.7.40-1.el7 将被 删除
--> 正在处理依赖关系 libmysqlclient.so.18()(64bit),它被软件包 perl-DBD-MySQL-4.023-6.el7.x86_64 需要
--> 正在处理依赖关系 libmysqlclient.so.18()(64bit),它被软件包 zabbix-server-mysql-5.0.28-1.el7.x86_64 需要
--> 正在处理依赖关系 libmysqlclient.so.18()(64bit),它被软件包 zabbix-proxy-mysql-5.0.28-1.el7.x86_64 需要
--> 正在处理依赖关系 libmysqlclient.so.18(libmysqlclient_18)(64bit),它被软件包 perl-DBD-MySQL-4.023-6.el7.x86_64 需要
--> 正在处理依赖关系 libmysqlclient.so.18(libmysqlclient_18)(64bit),它被软件包 zabbix-server-mysql-5.0.28-1.el7.x86_64 需要
--> 正在处理依赖关系 libmysqlclient.so.18(libmysqlclient_18)(64bit),它被软件包 zabbix-proxy-mysql-5.0.28-1.el7.x86_64 需要
---> 软件包 mysql-community-server.x86_64.0.5.7.40-1.el7 将被 删除
---> 软件包 mysql-community-test.x86_64.0.5.7.40-1.el7 将被 删除
--> 正在检查事务
---> 软件包 perl-DBD-MySQL.x86_64.0.4.023-6.el7 将被 删除
---> 软件包 zabbix-proxy-mysql.x86_64.0.5.0.28-1.el7 将被 删除
---> 软件包 zabbix-server-mysql.x86_64.0.5.0.28-1.el7 将被 删除
--> 解决依赖关系完成

依赖关系解决

===================================================================================================================================================================================================================
 Package                                                          架构                                    版本                                           源                                                   大小
===================================================================================================================================================================================================================
正在删除:
 mysql-community-client                                           x86_64                                  5.7.40-1.el7                                   @mysql57                                            108 M
 mysql-community-common                                           x86_64                                  5.7.40-1.el7                                   @mysql57                                            2.8 M
 mysql-community-devel                                            x86_64                                  5.7.40-1.el7                                   @mysql57-community                                   24 M
 mysql-community-embedded                                         x86_64                                  5.7.40-1.el7                                   @mysql57-community                                  201 M
 mysql-community-embedded-compat                                  x86_64                                  5.7.40-1.el7                                   @mysql57-community                                   88 M
 mysql-community-embedded-devel                                   x86_64                                  5.7.40-1.el7                                   @mysql57-community                                  906 M
 mysql-community-libs                                             x86_64                                  5.7.40-1.el7                                   @mysql57                                             10 M
 mysql-community-libs-compat                                      x86_64                                  5.7.40-1.el7                                   @mysql57                                            5.9 M
 mysql-community-server                                           x86_64                                  5.7.40-1.el7                                   @mysql57                                            774 M
 mysql-community-test                                             x86_64                                  5.7.40-1.el7                                   @mysql57-community                                  671 M
为依赖而移除:
 perl-DBD-MySQL                                                   x86_64                                  4.023-6.el7                                    @base                                               323 k
 zabbix-proxy-mysql                                               x86_64                                  5.0.28-1.el7                                   @zabbix                                             4.6 M
 zabbix-server-mysql                                              x86_64                                  5.0.28-1.el7                                   @zabbix                                             7.1 M

事务概要
===================================================================================================================================================================================================================
移除  10 软件包 (+3 依赖软件包)

安装大小:2.7 G
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在删除    : mysql-community-embedded-devel-5.7.40-1.el7.x86_64                                                                                                                                            1/13 
  正在删除    : mysql-community-devel-5.7.40-1.el7.x86_64                                                                                                                                                     2/13 
  正在删除    : mysql-community-embedded-5.7.40-1.el7.x86_64                                                                                                                                                  3/13 
  正在删除    : mysql-community-test-5.7.40-1.el7.x86_64                                                                                                                                                      4/13 
  正在删除    : mysql-community-server-5.7.40-1.el7.x86_64                                                                                                                                                    5/13 
  正在删除    : mysql-community-client-5.7.40-1.el7.x86_64                                                                                                                                                    6/13 
  正在删除    : zabbix-server-mysql-5.0.28-1.el7.x86_64                                                                                                                                                       7/13 
警告:/etc/zabbix/zabbix_server.conf 已另存为 /etc/zabbix/zabbix_server.conf.rpmsave
  正在删除    : mysql-community-embedded-compat-5.7.40-1.el7.x86_64                                                                                                                                           8/13 
  正在删除    : perl-DBD-MySQL-4.023-6.el7.x86_64                                                                                                                                                             9/13 
  正在删除    : zabbix-proxy-mysql-5.0.28-1.el7.x86_64                                                                                                                                                       10/13 
警告:/etc/zabbix/zabbix_proxy.conf 已另存为 /etc/zabbix/zabbix_proxy.conf.rpmsave
  正在删除    : mysql-community-libs-compat-5.7.40-1.el7.x86_64                                                                                                                                              11/13 
  正在删除    : mysql-community-libs-5.7.40-1.el7.x86_64                                                                                                                                                     12/13 
  正在删除    : mysql-community-common-5.7.40-1.el7.x86_64                                                                                                                                                   13/13 
  验证中      : mysql-community-client-5.7.40-1.el7.x86_64                                                                                                                                                    1/13 
  验证中      : zabbix-proxy-mysql-5.0.28-1.el7.x86_64                                                                                                                                                        2/13 
  验证中      : mysql-community-devel-5.7.40-1.el7.x86_64                                                                                                                                                     3/13 
  验证中      : mysql-community-server-5.7.40-1.el7.x86_64                                                                                                                                                    4/13 
  验证中      : mysql-community-common-5.7.40-1.el7.x86_64                                                                                                                                                    5/13 
  验证中      : perl-DBD-MySQL-4.023-6.el7.x86_64                                                                                                                                                             6/13 
  验证中      : mysql-community-embedded-compat-5.7.40-1.el7.x86_64                                                                                                                                           7/13 
  验证中      : zabbix-server-mysql-5.0.28-1.el7.x86_64                                                                                                                                                       8/13 
  验证中      : mysql-community-embedded-5.7.40-1.el7.x86_64                                                                                                                                                  9/13 
  验证中      : mysql-community-libs-5.7.40-1.el7.x86_64                                                                                                                                                     10/13 
  验证中      : mysql-community-libs-compat-5.7.40-1.el7.x86_64                                                                                                                                              11/13 
  验证中      : mysql-community-test-5.7.40-1.el7.x86_64                                                                                                                                                     12/13 
  验证中      : mysql-community-embedded-devel-5.7.40-1.el7.x86_64                                                                                                                                           13/13 

删除:
  mysql-community-client.x86_64 0:5.7.40-1.el7            mysql-community-common.x86_64 0:5.7.40-1.el7           mysql-community-devel.x86_64 0:5.7.40-1.el7   mysql-community-embedded.x86_64 0:5.7.40-1.el7     
  mysql-community-embedded-compat.x86_64 0:5.7.40-1.el7   mysql-community-embedded-devel.x86_64 0:5.7.40-1.el7   mysql-community-libs.x86_64 0:5.7.40-1.el7    mysql-community-libs-compat.x86_64 0:5.7.40-1.el7  
  mysql-community-server.x86_64 0:5.7.40-1.el7            mysql-community-test.x86_64 0:5.7.40-1.el7            

作为依赖被删除:
  perl-DBD-MySQL.x86_64 0:4.023-6.el7                               zabbix-proxy-mysql.x86_64 0:5.0.28-1.el7                               zabbix-server-mysql.x86_64 0:5.0.28-1.el7                              

完毕!
[root@mysql-slave1 ~]# yum remove mysql80-community-release-el7-7.noarch
已加载插件:fastestmirror
正在解决依赖关系
--> 正在检查事务
---> 软件包 mysql80-community-release.noarch.0.el7-7 将被 删除
--> 解决依赖关系完成

依赖关系解决

===================================================================================================================================================================================================================
 Package                                                  架构                                  版本                                 源                                                                       大小
===================================================================================================================================================================================================================
正在删除:
 mysql80-community-release                                noarch                                el7-7                                @/mysql80-community-release-el7-7.noarch                                 10 k

事务概要
===================================================================================================================================================================================================================
移除  1 软件包

安装大小:10 k
是否继续?[y/N]:y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在删除    : mysql80-community-release-el7-7.noarch                                                                                                                                                         1/1 
警告:/etc/yum.repos.d/mysql-community.repo 已另存为 /etc/yum.repos.d/mysql-community.repo.rpmsave
  验证中      : mysql80-community-release-el7-7.noarch                                                                                                                                                         1/1 

删除:
  mysql80-community-release.noarch 0:el7-7                                                                                                                                                                         

完毕!
[root@mysql-slave1 ~]# service mysql status
Redirecting to /bin/systemctl status mysql.service
Unit mysql.service could not be found.
[root@mysql-slave1 ~]# find / -name mysql
/etc/selinux/targeted/active/modules/100/mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/share/mysql
[root@mysql-slave1 ~]# rm -rf /etc/selinux/targeted/active/modules/100/mysql
[root@mysql-slave1 ~]# rm -rf /var/lib/mysql
[root@mysql-slave1 ~]# rm -rf /var/lib/mysql/mysql
[root@mysql-slave1 ~]# rm -rf /usr/share/mysql
[root@mysql-slave1 ~]# rpm -qa | grep -i mysql
zabbix-web-mysql-scl-5.0.28-1.el7.noarch
rh-php72-php-mysqlnd-7.2.24-1.el7.x86_64

然后分别重新安装上 MySQL

[root@web001 ~]# yum install -y mysql80-community-release-el7-7.noarch.rpm 
已加载插件:fastestmirror
正在检查 mysql80-community-release-el7-7.noarch.rpm: mysql80-community-release-el7-7.noarch
mysql80-community-release-el7-7.noarch.rpm 将被安装
正在解决依赖关系
--> 正在检查事务
---> 软件包 mysql80-community-release.noarch.0.el7-7 将被 安装
--> 解决依赖关系完成

依赖关系解决

===========================================================================================================
 Package                        架构        版本        源                                            大小
===========================================================================================================
正在安装:
 mysql80-community-release      noarch      el7-7       /mysql80-community-release-el7-7.noarch       10 k

事务概要
===========================================================================================================
安装  1 软件包

总计:10 k
安装大小:10 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : mysql80-community-release-el7-7.noarch                                                 1/1 
  验证中      : mysql80-community-release-el7-7.noarch                                                 1/1 

已安装:
  mysql80-community-release.noarch 0:el7-7                                                                 

完毕!
[root@web001 ~]# ls /etc/yum.repos.d
CentOS-Base.repo       CentOS-fasttrack.repo  CentOS-Vault.repo               mysql-community.repo
CentOS-CR.repo         CentOS-Media.repo      CentOS-x86_64-kernel.repo       mysql-community-source.repo#源码包
CentOS-Debuginfo.repo  CentOS-Sources.repo    mysql-community-debuginfo.repo  zabbix.repo

安装mysql5.7版本(默认安装mysql8.0)

[root@mysql ~]# yum list |grep mysql
mysql80-community-release.noarch          el7-7                        @/mysql80-community-release-el7-7.noarch
rh-php72-php-mysqlnd.x86_64               7.2.24-1.el7                 @centos-sclo-rh
zabbix-proxy-mysql.x86_64                 5.0.28-1.el7                 @zabbix  
zabbix-server-mysql.x86_64                5.0.28-1.el7                 @zabbix  
zabbix-web-mysql-scl.noarch               5.0.28-1.el7                 @zabbix-frontend
akonadi-mysql.x86_64                      1.9.2-4.el7                  base     
apr-util-mysql.x86_64                     1.5.2-6.el7                  base     
dovecot-mysql.x86_64                      1:2.2.36-8.el7               base     
freeradius-mysql.x86_64                   3.0.13-15.el7                base     
libdbi-dbd-mysql.x86_64                   0.8.3-16.el7                 base     
mysql-community-client.i686               8.0.31-1.el7                 mysql80-community
mysql-community-client.x86_64             8.0.31-1.el7                 mysql80-community
mysql-community-client-plugins.i686       8.0.31-1.el7                 mysql80-community
mysql-community-client-plugins.x86_64     8.0.31-1.el7                 mysql80-community
mysql-community-common.i686               8.0.31-1.el7                 mysql80-community
mysql-community-common.x86_64             8.0.31-1.el7                 mysql80-community
mysql-community-devel.i686                8.0.31-1.el7                 mysql80-community
mysql-community-devel.x86_64              8.0.31-1.el7                 mysql80-community
mysql-community-embedded-compat.i686      8.0.31-1.el7                 mysql80-community
mysql-community-embedded-compat.x86_64    8.0.31-1.el7                 mysql80-community
mysql-community-icu-data-files.i686       8.0.31-1.el7                 mysql80-community
mysql-community-icu-data-files.x86_64     8.0.31-1.el7                 mysql80-community
mysql-community-libs.i686                 8.0.31-1.el7                 mysql80-community
mysql-community-libs.x86_64               8.0.31-1.el7                 mysql80-community
mysql-community-libs-compat.i686          8.0.31-1.el7                 mysql80-community
mysql-community-libs-compat.x86_64        8.0.31-1.el7                 mysql80-community
mysql-community-release.noarch            el7-5                        mysql-connectors-community
mysql-community-server.x86_64             8.0.31-1.el7                 mysql80-community
mysql-community-server-debug.x86_64       8.0.31-1.el7                 mysql80-community
mysql-community-test.x86_64               8.0.31-1.el7                 mysql80-community
mysql-connector-c++.x86_64                8.0.31-1.el7                 mysql-connectors-community
mysql-connector-c++-devel.x86_64          8.0.31-1.el7                 mysql-connectors-community
mysql-connector-c++-jdbc.x86_64           8.0.31-1.el7                 mysql-connectors-community
mysql-connector-j.noarch                  1:8.0.31-1.el7               mysql-connectors-community
mysql-connector-java.noarch               1:8.0.30-1.el7               mysql-connectors-community
mysql-connector-odbc.x86_64               8.0.31-1.el7                 mysql-connectors-community
mysql-connector-odbc-setup.x86_64         8.0.31-1.el7                 mysql-connectors-community
mysql-connector-python.noarch             2.0.4-1.el7                  mysql-connectors-community
mysql-connector-python.x86_64             8.0.23-1.el7                 mysql-connectors-community
mysql-connector-python-cext.x86_64        8.0.21-1.el7                 mysql-connectors-community
mysql-connector-python3.x86_64            8.0.31-1.el7                 mysql-connectors-community
mysql-connector-python3-cext.x86_64       8.0.21-1.el7                 mysql-connectors-community
mysql-ref-manual-8.0-en-html-chapter.noarch
                                          1-20220914                   mysql80-community
mysql-ref-manual-8.0-en-pdf.noarch        1-20220914                   mysql80-community
mysql-router.x86_64                       8.0.12-1.el7                 mysql-tools-community
mysql-router-community.x86_64             8.0.31-1.el7                 mysql-tools-community
mysql-shell.x86_64                        8.0.31-1.el7                 mysql-tools-community
mysql-utilities.noarch                    1.6.5-1.el7                  mysql-tools-community
mysql-utilities-extra.noarch              1.5.6-1.el7                  mysql-tools-community
mysql-workbench-community.x86_64          8.0.22-1.el7                 mysql-tools-community
pcp-pmda-mysql.x86_64                     4.3.2-13.el7_9               updates  
php-mysql.x86_64                          5.4.16-48.el7                base     
php-mysqlnd.x86_64                        5.4.16-48.el7                base     
qt-mysql.i686                             1:4.8.7-9.el7_9              updates  
qt-mysql.x86_64                           1:4.8.7-9.el7_9              updates  
qt5-qtbase-mysql.i686                     5.9.7-5.el7_9                updates  
qt5-qtbase-mysql.x86_64                   5.9.7-5.el7_9                updates  
redland-mysql.x86_64                      1.0.16-6.el7                 base     
rsyslog-mysql.x86_64                      8.24.0-57.el7_9.3            updates  
zabbix-web-mysql-scl-php73.noarch         5.0.28-1.el7                 zabbix2  

【MySQL入门指北】主从复制及读写分离_第4张图片
打开mysql的仓库修改自己需要的版本,这只enabled=1为开启&

[root@mysql ~]# vim /etc/yum.repos.d/mysql-community.repo

【MySQL入门指北】主从复制及读写分离_第5张图片
刷新缓存

[root@mysql ~]# yum makecache
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
base                                                                        | 3.6 kB  00:00:00     
extras                                                                      | 2.9 kB  00:00:00     
mysql-connectors-community                                                  | 2.6 kB  00:00:00     
mysql-tools-community                                                       | 2.6 kB  00:00:00     
mysql57-community                                                           | 2.6 kB  00:00:00     
updates                                                                     | 2.9 kB  00:00:00     
zabbix                                                                      | 2.9 kB  00:00:00     
zabbix2                                                                     | 2.9 kB  00:00:00     
(1/5): mysql-connectors-community/x86_64/other_db                           |  24 kB  00:00:00     
(2/5): mysql-tools-community/x86_64/other_db                                |  17 kB  00:00:00     
(3/5): mysql-connectors-community/x86_64/filelists_db                       |  38 kB  00:00:00     
(4/5): mysql-tools-community/x86_64/filelists_db                            | 393 kB  00:00:00     
(5/5): mysql57-community/x86_64/filelists_db                                | 1.9 MB  00:00:00     
元数据缓存已建立

[root@mysql ~]# yum list |grep mysql
mysql80-community-release.noarch          el7-7                        @/mysql80-community-release-el7-7.noarch
rh-php72-php-mysqlnd.x86_64               7.2.24-1.el7                 @centos-sclo-rh
zabbix-proxy-mysql.x86_64                 5.0.28-1.el7                 @zabbix  
zabbix-server-mysql.x86_64                5.0.28-1.el7                 @zabbix  
zabbix-web-mysql-scl.noarch               5.0.28-1.el7                 @zabbix-frontend
akonadi-mysql.x86_64                      1.9.2-4.el7                  base     
apr-util-mysql.x86_64                     1.5.2-6.el7                  base     
dovecot-mysql.x86_64                      1:2.2.36-8.el7               base     
freeradius-mysql.x86_64                   3.0.13-15.el7                base     
libdbi-dbd-mysql.x86_64                   0.8.3-16.el7                 base     
mysql-community-client.i686               5.7.40-1.el7                 mysql57-community
mysql-community-client.x86_64             5.7.40-1.el7                 mysql57-community
mysql-community-common.i686               5.7.40-1.el7                 mysql57-community
mysql-community-common.x86_64             5.7.40-1.el7                 mysql57-community
mysql-community-devel.i686                5.7.40-1.el7                 mysql57-community
mysql-community-devel.x86_64              5.7.40-1.el7                 mysql57-community
mysql-community-embedded.i686             5.7.40-1.el7                 mysql57-community
mysql-community-embedded.x86_64           5.7.40-1.el7                 mysql57-community
mysql-community-embedded-compat.i686      5.7.40-1.el7                 mysql57-community
mysql-community-embedded-compat.x86_64    5.7.40-1.el7                 mysql57-community
mysql-community-embedded-devel.i686       5.7.40-1.el7                 mysql57-community
mysql-community-embedded-devel.x86_64     5.7.40-1.el7                 mysql57-community
mysql-community-libs.i686                 5.7.40-1.el7                 mysql57-community
mysql-community-libs.x86_64               5.7.40-1.el7                 mysql57-community
mysql-community-libs-compat.i686          5.7.40-1.el7                 mysql57-community
mysql-community-libs-compat.x86_64        5.7.40-1.el7                 mysql57-community
mysql-community-release.noarch            el7-5                        mysql-connectors-community
mysql-community-server.x86_64             5.7.40-1.el7                 mysql57-community
mysql-community-test.x86_64               5.7.40-1.el7                 mysql57-community
mysql-connector-c++.x86_64                8.0.31-1.el7                 mysql-connectors-community
mysql-connector-c++-devel.x86_64          8.0.31-1.el7                 mysql-connectors-community
mysql-connector-c++-jdbc.x86_64           8.0.31-1.el7                 mysql-connectors-community
mysql-connector-j.noarch                  1:8.0.31-1.el7               mysql-connectors-community
mysql-connector-java.noarch               1:8.0.30-1.el7               mysql-connectors-community
mysql-connector-odbc.x86_64               8.0.31-1.el7                 mysql-connectors-community
mysql-connector-odbc-setup.x86_64         8.0.31-1.el7                 mysql-connectors-community
mysql-connector-python.noarch             2.0.4-1.el7                  mysql-connectors-community
mysql-connector-python.x86_64             8.0.23-1.el7                 mysql-connectors-community
mysql-connector-python-cext.x86_64        8.0.21-1.el7                 mysql-connectors-community
mysql-connector-python3.x86_64            8.0.31-1.el7                 mysql-connectors-community
mysql-connector-python3-cext.x86_64       8.0.21-1.el7                 mysql-connectors-community
mysql-ref-manual-5.5-en-html-chapter.noarch
                                          1-20170320                   mysql57-community
mysql-ref-manual-5.5-en-pdf.noarch        1-20170320                   mysql57-community
mysql-ref-manual-5.7-en-html-chapter.noarch
                                          1-20220831                   mysql57-community
mysql-ref-manual-5.7-en-pdf.noarch        1-20220831                   mysql57-community
mysql-router.x86_64                       8.0.12-1.el7                 mysql-tools-community
mysql-router-community.x86_64             8.0.31-1.el7                 mysql-tools-community
mysql-shell.x86_64                        8.0.31-1.el7                 mysql-tools-community
mysql-utilities.noarch                    1.6.5-1.el7                  mysql-tools-community
mysql-utilities-extra.noarch              1.5.6-1.el7                  mysql-tools-community
mysql-workbench-community.x86_64          8.0.22-1.el7                 mysql-tools-community
pcp-pmda-mysql.x86_64                     4.3.2-13.el7_9               updates  
php-mysql.x86_64                          5.4.16-48.el7                base     
php-mysqlnd.x86_64                        5.4.16-48.el7                base     
qt-mysql.i686                             1:4.8.7-9.el7_9              updates  
qt-mysql.x86_64                           1:4.8.7-9.el7_9              updates  
qt5-qtbase-mysql.i686                     5.9.7-5.el7_9                updates  
qt5-qtbase-mysql.x86_64                   5.9.7-5.el7_9                updates  
redland-mysql.x86_64                      1.0.16-6.el7                 base     
rsyslog-mysql.x86_64                      8.24.0-57.el7_9.3            updates  
zabbix-web-mysql-scl-php73.noarch         5.0.28-1.el7                 zabbix2  

上面操作完成即可看到mysql-server 由原来的mysql8.0变为msyql5.7版本
【MySQL入门指北】主从复制及读写分离_第6张图片
安装mysql

[root@mysql ~]# yum install -y mysql-community*  
//或者yum install  mysql-server 会自动安装mysql服务的相关依赖组件
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 mariadb.x86_64.1.5.5.68-1.el7 将被 取代
---> 软件包 mariadb-libs.x86_64.1.5.5.68-1.el7 将被 取代
---> 软件包 mariadb-server.x86_64.1.5.5.68-1.el7 将被 取代
---> 软件包 mysql-community-client.x86_64.0.5.7.40-1.el7 将被 舍弃
---> 软件包 mysql-community-common.x86_64.0.5.7.40-1.el7 将被 安装
---> 软件包 mysql-community-devel.x86_64.0.5.7.40-1.el7 将被 安装
---> 软件包 mysql-community-embedded.x86_64.0.5.7.40-1.el7 将被 安装
---> 软件包 mysql-community-embedded-compat.x86_64.0.5.7.40-1.el7 将被 安装
---> 软件包 mysql-community-embedded-devel.x86_64.0.5.7.40-1.el7 将被 安装
---> 软件包 mysql-community-libs.x86_64.0.5.7.40-1.el7 将被 舍弃
---> 软件包 mysql-community-libs-compat.x86_64.0.5.7.40-1.el7 将被 舍弃
---> 软件包 mysql-community-release.noarch.0.el7-5 将被 安装
---> 软件包 mysql-community-server.x86_64.0.5.7.40-1.el7 将被 舍弃
---> 软件包 mysql-community-test.x86_64.0.5.7.40-1.el7 将被 安装
--> 正在处理依赖关系 perl(JSON),它被软件包 mysql-community-test-5.7.40-1.el7.x86_64 需要
--> 正在检查事务
---> 软件包 perl-JSON.noarch.0.2.59-2.el7 将被 安装
--> 处理 mysql80-community-release-el7-7.noarch 与 mysql-community-release 的冲突
--> 解决依赖关系完成
错误:mysql80-community-release conflicts with mysql-community-release-el7-5.noarch
 您可以尝试添加 --skip-broken 选项来解决该问题
 您可以尝试执行:rpm -Va --nofiles --nodigest
[root@mysql ~]# yum install -y mysql-community* --skip-broken
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 mariadb.x86_64.1.5.5.68-1.el7 将被 取代
---> 软件包 mariadb-libs.x86_64.1.5.5.68-1.el7 将被 取代
---> 软件包 mariadb-server.x86_64.1.5.5.68-1.el7 将被 取代
---> 软件包 mysql-community-client.x86_64.0.5.7.40-1.el7 将被 舍弃
---> 软件包 mysql-community-common.x86_64.0.5.7.40-1.el7 将被 安装
---> 软件包 mysql-community-devel.x86_64.0.5.7.40-1.el7 将被 安装
---> 软件包 mysql-community-embedded.x86_64.0.5.7.40-1.el7 将被 安装
---> 软件包 mysql-community-embedded-compat.x86_64.0.5.7.40-1.el7 将被 安装
---> 软件包 mysql-community-embedded-devel.x86_64.0.5.7.40-1.el7 将被 安装
---> 软件包 mysql-community-libs.x86_64.0.5.7.40-1.el7 将被 舍弃
---> 软件包 mysql-community-libs-compat.x86_64.0.5.7.40-1.el7 将被 舍弃
---> 软件包 mysql-community-release.noarch.0.el7-5 将被 安装
---> 软件包 mysql-community-server.x86_64.0.5.7.40-1.el7 将被 舍弃
---> 软件包 mysql-community-test.x86_64.0.5.7.40-1.el7 将被 安装
--> 正在处理依赖关系 perl(JSON),它被软件包 mysql-community-test-5.7.40-1.el7.x86_64 需要
--> 正在检查事务
---> 软件包 perl-JSON.noarch.0.2.59-2.el7 将被 安装
--> 处理 mysql80-community-release-el7-7.noarch 与 mysql-community-release 的冲突
--> 正在检查事务
---> 软件包 mysql-community-release.noarch.0.el7-5 将被 安装
--> 正在检查事务
---> 软件包 mariadb.x86_64.1.5.5.68-1.el7 将被 取代
---> 软件包 mariadb-libs.x86_64.1.5.5.68-1.el7 将被 取代
---> 软件包 mariadb-server.x86_64.1.5.5.68-1.el7 将被 取代
---> 软件包 mysql-community-client.x86_64.0.5.7.40-1.el7 将被 舍弃
---> 软件包 mysql-community-common.x86_64.0.5.7.40-1.el7 将被 安装
---> 软件包 mysql-community-devel.x86_64.0.5.7.40-1.el7 将被 安装
---> 软件包 mysql-community-embedded.x86_64.0.5.7.40-1.el7 将被 安装
---> 软件包 mysql-community-embedded-compat.x86_64.0.5.7.40-1.el7 将被 安装
---> 软件包 mysql-community-embedded-devel.x86_64.0.5.7.40-1.el7 将被 安装
---> 软件包 mysql-community-libs.x86_64.0.5.7.40-1.el7 将被 舍弃
---> 软件包 mysql-community-libs-compat.x86_64.0.5.7.40-1.el7 将被 舍弃
---> 软件包 mysql-community-server.x86_64.0.5.7.40-1.el7 将被 舍弃
---> 软件包 mysql-community-test.x86_64.0.5.7.40-1.el7 将被 安装
---> 软件包 perl-JSON.noarch.0.2.59-2.el7 将被 安装
--> 解决依赖关系完成

因为依赖关系问题而跳过的软件包:
    mysql-community-release-el7-5.noarch 来自 mysql-connectors-community

依赖关系解决

===================================================================================================
 Package                            架构      版本             源                             大小
===================================================================================================
正在安装:
 mysql-community-client             x86_64    5.7.40-1.el7     mysql57-community              28 M
      替换  mariadb.x86_64 1:5.5.68-1.el7
 mysql-community-common             x86_64    5.7.40-1.el7     mysql57-community             311 k
 mysql-community-devel              x86_64    5.7.40-1.el7     mysql57-community             4.2 M
 mysql-community-embedded           x86_64    5.7.40-1.el7     mysql57-community              46 M
 mysql-community-embedded-compat    x86_64    5.7.40-1.el7     mysql57-community              22 M
 mysql-community-embedded-devel     x86_64    5.7.40-1.el7     mysql57-community             127 M
 mysql-community-libs               x86_64    5.7.40-1.el7     mysql57-community             2.6 M
      替换  mariadb-libs.x86_64 1:5.5.68-1.el7
 mysql-community-libs-compat        x86_64    5.7.40-1.el7     mysql57-community             1.2 M
      替换  mariadb-libs.x86_64 1:5.5.68-1.el7
 mysql-community-server             x86_64    5.7.40-1.el7     mysql57-community             178 M
      替换  mariadb-server.x86_64 1:5.5.68-1.el7
 mysql-community-test               x86_64    5.7.40-1.el7     mysql57-community             121 M
为依赖而安装:
 perl-JSON                          noarch    2.59-2.el7       base                           96 k
跳过(依赖问题):
 mysql-community-release            noarch    el7-5            mysql-connectors-community    6.0 k

事务概要
===================================================================================================
安装              10 软件包 (+1 依赖软件包)
跳过(依赖问题)   1 软件包

总下载量:532 M
Downloading packages:
警告:/var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.40-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
mysql-community-common-5.7.40-1.el7.x86_64.rpm 的公钥尚未安装
(1/11): mysql-community-common-5.7.40-1.el7.x86_64.rpm                      | 311 kB  00:00:00     
(2/11): mysql-community-devel-5.7.40-1.el7.x86_64.rpm                       | 4.2 MB  00:00:00     
(3/11): mysql-community-client-5.7.40-1.el7.x86_64.rpm                      |  28 MB  00:00:01     
(4/11): mysql-community-embedded-5.7.40-1.el7.x86_64.rpm                    |  46 MB  00:00:02     
(5/11): mysql-community-embedded-compat-5.7.40-1.el7.x86_64.rpm             |  22 MB  00:00:03     
(6/11): mysql-community-libs-5.7.40-1.el7.x86_64.rpm                        | 2.6 MB  00:00:00     
(7/11): mysql-community-libs-compat-5.7.40-1.el7.x86_64.rpm                 | 1.2 MB  00:00:00     
(8/11): mysql-community-embedded-devel-5.7.40-1.el7.x86_64.rpm              | 127 MB  00:00:06     
(9/11): perl-JSON-2.59-2.el7.noarch.rpm                                     |  96 kB  00:00:00     
(10/11): mysql-community-test-5.7.40-1.el7.x86_64.rpm                       | 121 MB  00:00:08     
(11/11): mysql-community-server-5.7.40-1.el7.x86_64.rpm                     | 178 MB  00:00:13     
---------------------------------------------------------------------------------------------------
总计                                                                27 MB/s | 532 MB  00:00:19     
从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022 检索密钥
导入 GPG key 0x3A79BD29:
 用户ID     : "MySQL Release Engineering "
 指纹       : 859b e8d7 c586 f538 430b 19c2 467b 942d 3a79 bd29
 软件包     : mysql80-community-release-el7-7.noarch (@/mysql80-community-release-el7-7.noarch)
 来自       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 检索密钥
导入 GPG key 0x5072E1F5:
 用户ID     : "MySQL Release Engineering "
 指纹       : a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 软件包     : mysql80-community-release-el7-7.noarch (@/mysql80-community-release-el7-7.noarch)
 来自       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : mysql-community-common-5.7.40-1.el7.x86_64                                    1/14 
  正在安装    : mysql-community-libs-5.7.40-1.el7.x86_64                                      2/14 
  正在安装    : mysql-community-client-5.7.40-1.el7.x86_64                                    3/14 
  正在安装    : mysql-community-server-5.7.40-1.el7.x86_64                                    4/14 
  正在安装    : mysql-community-devel-5.7.40-1.el7.x86_64                                     5/14 
  正在安装    : mysql-community-embedded-5.7.40-1.el7.x86_64                                  6/14 
  正在安装    : perl-JSON-2.59-2.el7.noarch                                                   7/14 
  正在安装    : mysql-community-test-5.7.40-1.el7.x86_64                                      8/14 
  正在安装    : mysql-community-embedded-devel-5.7.40-1.el7.x86_64                            9/14 
  正在安装    : mysql-community-libs-compat-5.7.40-1.el7.x86_64                              10/14 
  正在安装    : mysql-community-embedded-compat-5.7.40-1.el7.x86_64                          11/14 
  正在删除    : 1:mariadb-server-5.5.68-1.el7.x86_64                                         12/14 
warning: /var/log/mariadb/mariadb.log saved as /var/log/mariadb/mariadb.log.rpmsave
  正在删除    : 1:mariadb-5.5.68-1.el7.x86_64                                                13/14 
  正在删除    : 1:mariadb-libs-5.5.68-1.el7.x86_64                                           14/14 
  验证中      : mysql-community-client-5.7.40-1.el7.x86_64                                    1/14 
  验证中      : mysql-community-devel-5.7.40-1.el7.x86_64                                     2/14 
  验证中      : mysql-community-server-5.7.40-1.el7.x86_64                                    3/14 
  验证中      : mysql-community-common-5.7.40-1.el7.x86_64                                    4/14 
  验证中      : mysql-community-embedded-compat-5.7.40-1.el7.x86_64                           5/14 
  验证中      : mysql-community-libs-compat-5.7.40-1.el7.x86_64                               6/14 
  验证中      : mysql-community-libs-5.7.40-1.el7.x86_64                                      7/14 
  验证中      : perl-JSON-2.59-2.el7.noarch                                                   8/14 
  验证中      : mysql-community-embedded-5.7.40-1.el7.x86_64                                  9/14 
  验证中      : mysql-community-test-5.7.40-1.el7.x86_64                                     10/14 
  验证中      : mysql-community-embedded-devel-5.7.40-1.el7.x86_64                           11/14 
  验证中      : 1:mariadb-server-5.5.68-1.el7.x86_64                                         12/14 
  验证中      : 1:mariadb-5.5.68-1.el7.x86_64                                                13/14 
  验证中      : 1:mariadb-libs-5.5.68-1.el7.x86_64                                           14/14 

已安装:
  mysql-community-client.x86_64 0:5.7.40-1.el7                                                     
  mysql-community-common.x86_64 0:5.7.40-1.el7                                                     
  mysql-community-devel.x86_64 0:5.7.40-1.el7                                                      
  mysql-community-embedded.x86_64 0:5.7.40-1.el7                                                   
  mysql-community-embedded-compat.x86_64 0:5.7.40-1.el7                                            
  mysql-community-embedded-devel.x86_64 0:5.7.40-1.el7                                             
  mysql-community-libs.x86_64 0:5.7.40-1.el7                                                       
  mysql-community-libs-compat.x86_64 0:5.7.40-1.el7                                                
  mysql-community-server.x86_64 0:5.7.40-1.el7                                                     
  mysql-community-test.x86_64 0:5.7.40-1.el7                                                       

作为依赖被安装:
  perl-JSON.noarch 0:2.59-2.el7                                                                    

跳过(依赖问题):
  mysql-community-release.noarch 0:el7-5                                                           

替代:
  mariadb.x86_64 1:5.5.68-1.el7                     mariadb-libs.x86_64 1:5.5.68-1.el7             
  mariadb-server.x86_64 1:5.5.68-1.el7             

完毕!

查询MySQL服务器默认密码

root@guan~]#grep 'password' /var/log/mysqld.log
2022-04-17T03:39:40.521811Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 9-Q10DnGxFQr
#冒号和空格后面的,全是密码

修改MySQL服务器密码

[root@mysql~]#mysqladmin  -uroot  -p'默认密码'  password  '新密码'

登录MySQL系统

mysql  -u root -p'123456'
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 95
Server version: 5.7.40 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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 database;
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 'database' at line 1
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| zabbix             |
+--------------------+
4 rows in set (0.04 sec)

mysql> 

3.一主一从

一主一从(实验一)
配置mysql-master1和mysql-slave1

mysql-master1

1.部署一台新的mysql服务器。准备好域名解析。
2.第一次准备数据(验证主从同步使用)
[root@mysql-master1 ~]# mysql -uroot -p123456
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 4513
Server version: 5.7.40-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> create database master1db;
Query OK, 1 row affected (0.06 sec)

mysql> create table master1db.master1tab(name char(30));
Query OK, 0 rows affected (0.11 sec)

mysql> insert into master1db.master1tab values(1111);
Query OK, 1 row affected (0.01 sec)

mysql> insert into master1db.master1tab values(2222);
Query OK, 1 row affected (0.01 sec)

mysql> select * from  master1db.master1tab ;
+------+
| name |
+------+
| 1111 |
| 2222 |
+------+
2 rows in set (0.00 sec)

mysql> 

3.开启二进制日志
[root@mysql-master1 ~]# cat  /etc/my.cnf

[mysqld]
log_bin=mysql_bin
server_id=1
[root@mysql-master1 ~]#systemctl restart mysqld   //重启mysql使配置文件生效
[root@mysql-master1 ~]# ls /var/lib/mysql  //查看日志开启是否生效,如果看如mysql_bin.000001 则说明开启成功了
auto.cnf         ib_logfile1       mysql_bin.000005        public_key.pem
ca-key.pem       ibtmp1            mysql_bin.index         server-cert.pem
ca.pem           master1db         mysql-master1-slow.log  server-key.pem
client-cert.pem  mysql             mysql-server-slow.log   sys
client-key.pem   mysql_bin.000001  mysql.sock              testdb
ib_buffer_pool   mysql_bin.000002  mysql.sock.lock         zabbix
ibdata1          mysql_bin.000003  performance_schema      zabbix-proxy-slow.log
ib_logfile0      mysql_bin.000004  private_key.pem

4.创建复制用户
mysql> grant replication slave,replication client on *.* to 'guan'@'192.168.200.%' identified by 'guan123456';
Query OK, 0 rows affected, 1 warning (0.01 sec)

5.备份mysql-master1数据库的数据
[root@mysql-master1 ~]# mysqldump -p'123456' --all-databases --single-transaction --master-data=2 --flush-logs > `date +%F`-mysql-all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@mysql-master1 ~]# ls
2022-11-11-mysql-all.sql         公共  图片  音乐
anaconda-ks.cfg                  模板  文档  桌面
initial-setup-ks.cfg             视频  下载



将备份文件发送给mysql-slave1

[root@mysql-master1 ~]# scp -r 2022-11-11-mysql-all.sql mysql-slave1:/tmp
The authenticity of host 'mysql-slave1 (192.168.200.184)' can't be established.
ECDSA key fingerprint is SHA256:ygT6h9ejxNmaemQtyIzVYHEbRko0BaG4PstS2LTavDM.
ECDSA key fingerprint is MD5:88:1f:c4:d2:fe:73:b9:1f:7f:26:cd:2c:ba:ad:5c:b5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'mysql-slave1,192.168.200.184' (ECDSA) to the list of known hosts.
root@mysql-slave1's password: 
2022-11-11-mysql-all.sql                                     100% 7524KB  26.7MB/s   00:00    
[root@mysql-master1 ~]# 

查看mysql-slave1是否接收到文件

[root@mysql-slave1 ~]# ls /tmp
2022-11-11-mysql-all.sql                                                           

观察二进制日志分割点

[root@mysql-slave1 ~]# vim  /tmp/2022-11-11-mysql-all.sql 
...
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql_bin.000007', MASTER_LOG_POS=154;
...
6.第二次准备数据(验证主从同步使用)
mysql> insert into master1db.master1tab values(333333);
Query OK, 1 row affected (0.14 sec)

mysql> insert into master1db.master1tab values(444444);
Query OK, 1 row affected (0.03 sec)

mysql> select * from master1db.master1tab;
+--------+
| name   |
+--------+
| 1111   |
| 2222   |
| 333333 |
| 444444 |
+--------+
4 rows in set (0.00 sec)

mysql> quit;
Bye

登录 mysql-slave1 验证guan用户是否可以使用

[root@mysql-slave1 ~]# mysql -uguan -p'guan123456' -h mysql-master1
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 1035
Server version: 5.7.40-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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

mysql> 

启动服务器序号

[root@mysql-slave1 ~]# vim /etc/my.cnf
[root@mysql-slave1 ~]# cat /etc/my.cnf

[mysqld]
server-id=2


[root@mysql-slave1 ~]# mysql -uroot -p'guan123456'
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 2
Server version: 5.7.40 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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                |
+--------------------+
4 rows in set (0.02 sec)

mysql> quit;
Bye

手动同步数据

mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)

mysql> source /tmp/2022-11-11-mysql-all.sql
Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)
...
mysql> 

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| master1db          |
| mysql              |
| performance_schema |
| sys                |
| testdb             |
| zabbix             |
+--------------------+
7 rows in set (0.00 sec)

mysql> select * from master1db.master1tab;
+------+
| name |
+------+
| 1111 |
| 2222 |
+------+
2 rows in set (0.01 sec)

设置主服务器
将主服务器mysql-master1的数据同步到服务器mysql-slave1

[root@mysql-master1 ~]# vim 2022-11-11-mysql-all.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql_bin.000007', MASTER_LOG_POS=154;
[root@mysql-slave1 ~]#mysql -uroot -p123456
mysql>change master to master_host='mysql-master1',master_user='guan',master_password='123456',master_log_file='mysql_bin.000007',master_log_pos=154;

启动主从设备同步

mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
mysql> select * from master1db.master1tab; //没有同步mysql-master1
+------+
| name |
+------+
| 1111 |
| 2222 |
+------+
2 rows in set (0.00 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: mysql-master1
                  Master_User: guan
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000007
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-slave1-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql_bin.000007
             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: 154
              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: 1045
                Last_IO_Error: error connecting to master 'rep@mysql-master1:3306' - retry-time: 60  retries: 17  //密码错误,这里不应该使用登录mysql-master1的密码123456,而是使用在mysql-master1中的guan这个用户的密码guan123456
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
                  Master_UUID: 
             Master_Info_File: /var/lib/mysql/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: 221112 02:04:07
     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> 
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> change master to master_host='mysql-master1',master_user='rep',master_password='guan123456',master_log_file='mysql_bin.000007',master_log_pos=154;
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: mysql-master1
                  Master_User: guan
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000008
          Read_Master_Log_Pos: 418
               Relay_Log_File: mysql-slave1-relay-bin.000003
                Relay_Log_Pos: 631
        Relay_Master_Log_File: mysql_bin.000008
             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: 418
              Relay_Log_Space: 1545
              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: 73bb1733-5f2c-11ed-b77a-000c290b1839
             Master_Info_File: /var/lib/mysql/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)

mysql> select * from master1db.master1tab;
+--------+
| name   |
+--------+
| 1111   |
| 2222   |
| 333333 |
| 444444 |
| 119    |
+--------+
5 rows in set (0.00 sec)

查看日志

[root@mysql-slave1 ~]# cat /var/log/messages
[root@mysql-slave1 ~]#cat /var/log/mysqld.log

一主一从(实验二)

环境:与实验一功能相同

实验前先重置 mysql-slave1 数据库

[root@mysql-slave1 ~]# systemctl stop mysqld
[root@mysql-slave1 ~]# rm -rf /var/lib/mysql/*
[root@mysql-slave1 ~]# systemctl start mysqld
[root@mysql-slave1 ~]# grep password /var/log/mysqld.log
....
2022-11-12T04:34:10.172347Z 1 [Note] A temporary password is generated for root@localhost: >yKwga-iY7-j

[root@mysql-slave1 ~]# mysqladmin -uroot -p'>yKwga-iY7-j' password 'guan123456'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
mysqladmin: unable to change password; error: 'Your password does not satisfy the current policy requirements'

[root@mysql-slave1 ~]# mys\ql -uroot -p'>yKwga-iY7-j'
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.40

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

mysql> exit;
Bye

[root@mysql-slave1 ~]# mysqladmin -uroot -p'>yKwga-iY7-j' password 'guan123456'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
[root@mysql-slave1 ~]# mysql -uroot -p'guan123456'
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 5
Server version: 5.7.40 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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                |
+--------------------+
4 rows in set (0.00 sec)



主服务器 msyql-master1

1.启动二进制日志文件,服务器ID ,GTID

[root@mysql-master1 ~]# vim /etc/my.cnf
[root@mysql-master1 ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
log_bin=mysql_bin
server_id=1
gtid_mode=ON
enforce_gtid_consistency=1

[root@mysql-master1 ~]# systemctl restart mysqld  //重启mysql服务使配置文件生效

2.授权复制用户guan
[root@mysql-master1 ~]# mysql -uroot -p123456
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 10
Server version: 5.7.40-log MySQL Community Server (GPL)

Copyright © 2000, 2022, Oracle and/or its affiliates.

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,replication client on . to ‘guan’@‘192.168.200.%’ identified by ‘guan123456’;
Query OK, 0 rows affected, 1 warning (0.01 sec)

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

mysql> exit;
Bye

3.备份数据库

[root@mysql-master1 ~]# mysqldump -p'123456' --all-databases --single-transaction --master-data=2 --flush-logs > `date +%F`-mysql-all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events. 

[root@mysql-master1 ~]# scp 2022-11-12-mysql-all.sql mysql-slave1:/tmp
root@mysql-slave1's password: 
Permission denied, please try again.
root@mysql-slave1's password: 
2022-11-12-mysql-all.sql               

登录 mysql-slave1 查看是否接收到日志文件

[root@mysql-slave1 ~]# ls /tmp
2022-11-11-mysql-all.sql
2022-11-12-mysql-all.sql

4.模拟数据库变化

[root@mysql-master1 ~]# mysql -uroot -p123456
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 180
Server version: 5.7.40-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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 |
| master1db          |
| mysql              |
| performance_schema |
| sys                |
| testdb             |
| zabbix             |
+--------------------+
7 rows in set (0.00 sec)

mysql> select * from master1db.master1tab;
+--------+
| name   |
+--------+
| 1111   |
| 2222   |
| 333333 |
| 444444 |
| 119    |
+--------+
5 rows in set (0.00 sec)

mysql> insert into master1db.master1tab values(120);
Query OK, 1 row affected (0.14 sec)

mysql> select * from master1db.master1tab;
+--------+
| name   |
+--------+
| 1111   |
| 2222   |
| 333333 |
| 444444 |
| 119    |
| 120    |
+--------+
6 rows in set (0.00 sec)

mysql> 

从服务器 mysql-slvae1
1.测试guan 用户是否可以使用

[root@mysql-slave1 ~]# mysql -h mysql-master1 -uguan -p'guan123456' 
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 227
Server version: 5.7.40-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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

mysql> 

2.启动二进制日志,服务器ID,GTID

[root@mysql-slave1 ~]# vim /etc/my.cnf
[root@mysql-slave1 ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
log_bin=mysql_bin
server_id=2
gtid_mode=ON
enforce_gtid_consistency=1

[root@mysql-slave1 ~]# systemctl restart mysqld  //重启mysql服务使配置文件生效

3.恢复手动同步数据

mysql> set sql_log_bin=0;  //临时关闭二进制日志文件
mysql> source /tmp/2022-11-12-mysql-all.sql  //到入mysql-master1 二进制日志备份文件

....
Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)


查看是否导入成功

这里因为我是在插入120这个数据之前备份的,所有没有120这个数据

mysql> select * from master1db.master1tab;
+--------+
| name   |
+--------+
| 1111   |
| 2222   |
| 333333 |
| 444444 |
| 119    |
+--------+
5 rows in set (0.01 sec)


4.设置主服务器

mysql> change master to master_host='mysql-master1',master_user='guan',master_password='guan123456',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

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: mysql-master1
                  Master_User: guan
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000010
          Read_Master_Log_Pos: 458
               Relay_Log_File: mysql-slave1-relay-bin.000002
                Relay_Log_Pos: 631
        Relay_Master_Log_File: mysql_bin.000010
             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: 458
              Relay_Log_Space: 845
              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: 73bb1733-5f2c-11ed-b77a-000c290b1839
             Master_Info_File: /var/lib/mysql/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: 73bb1733-5f2c-11ed-b77a-000c290b1839:3
            Executed_Gtid_Set: 73bb1733-5f2c-11ed-b77a-000c290b1839:1-3
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.05 sec)

5.返回主服务器(mysql-master1) 更新数据,在从服务器 (mysql-slave1) 观察是否同步

mysql> select * from master1db.master1tab;
+--------+
| name   |
+--------+
| 1111   |
| 2222   |
| 333333 |
| 444444 |
| 119    |
| 120    |
+--------+
6 rows in set (0.00 sec)

4.双主双从

前面的实验,主服务器单节点设置。假如服务器故障会影响全局的写入事件。

故需要设置双主双从

当前环境已经设置了mysql-master1为mysql-slave1的主服务器,只需要将mysql-slave1设置为master1的主服务器即可,故将mysql-slave1改名为mysql-master2

设置mysql-master2为mysql-master1的主服务器

1.在mysql-master2上进行授权

mysql> grant replication slave,replication client on *.* to 'guan'@'192.168.200.%' identified by 'guan123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.02 sec)

mysql> grant replication slave,replication client on *.* to 'guan'@'192.168.200.%' identified by 'guan123456';
Query OK, 0 rows affected, 1 warning (0.04 sec)

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


2.在mysql-master1进行配置

配置mysql-master1的主服务器为mysql-master2

mysql> change master to master_host='mysql-master2',master_user='guan',master_password='guan123456',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.06 sec)

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

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: mysql-master2
                  Master_User: guan
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000001
          Read_Master_Log_Pos: 621
               Relay_Log_File: mysql-master1-relay-bin.000002
                Relay_Log_Pos: 834
        Relay_Master_Log_File: mysql_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: 621
              Relay_Log_Space: 1049
              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: 2
                  Master_UUID: 3ff81eac-6243-11ed-be20-000c295a3e87
             Master_Info_File: /var/lib/mysql/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: 3ff81eac-6243-11ed-be20-000c295a3e87:1-2
            Executed_Gtid_Set: 3ff81eac-6243-11ed-be20-000c295a3e87:1-2,
73bb1733-5f2c-11ed-b77a-000c290b1839:1-3
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.05 sec)

ERROR: 
No query specified

测试
1.先在mysql-maste1上插入数据,在mysql-master2上观察

[root@mysql-master1 ~]# mysql -uroot -p123456

mysql> select * from master1db.master1tab;
+--------+
| name   |
+--------+
| 1111   |
| 2222   |
| 333333 |
| 444444 |
| 119    |
| 120    |
+--------+
6 rows in set (0.00 sec)

mysql> insert into master1db.master1tab values(121);
Query OK, 1 row affected (0.01 sec)

mysql> select * from master1db.master1tab;
+--------+
| name   |
+--------+
| 1111   |
| 2222   |
| 333333 |
| 444444 |
| 119    |
| 120    |
| 121    |
+--------+
7 rows in set (0.04 sec)

mysql> exit;


[root@mysql-master2 ~]# mysql -uroot -p123456 //在msyql-master2看到在mysql-master1插入的数据则说明同步成功
mysql> select * from master1db.master1tab;
+--------+
| name   |
+--------+
| 1111   |
| 2222   |
| 333333 |
| 444444 |
| 119    |
| 120    |
| 121    |
+--------+
7 rows in set (0.01 sec)

mysql> 

2.先在mysql-maste2上插入数据,在mysql-master1上观察

[root@mysql-master2 ~]# mysql -uroot -p123456

mysql> insert into  master1db.master1tab values(122);
Query OK, 1 row affected (0.06 sec)

mysql> select * from master1db.master1tab;
+--------+
| name   |
+--------+
| 1111   |
| 2222   |
| 333333 |
| 444444 |
| 119    |
| 120    |
| 121    |
| 122    |
+--------+
8 rows in set (0.01 sec)

mysql> 

[root@mysql-master1 ~]# mysql -uroot -p123456  //在msyql-master1看到在mysql-master2插入的数据则说明同步成功
mysql> select * from master1db.master1tab;
+--------+
| name   |
+--------+
| 1111   |
| 2222   |
| 333333 |
| 444444 |
| 119    |
| 120    |
| 121    |
| 122    |
+--------+
8 rows in set (0.00 sec)

双方同步成功,则说明双主双内设置完成

进行两台从服务器的配置
1.同步现有数据库

[root@mysql-master1 ~]# mysqldump -p'123456' --all-databases --single-transaction --master-data=2 --flush-logs > mmss-mysql-all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events. 
[root@mysql-master1 ~]# ls
2022-11-11-mysql-all.sql  log.txt             mysql_bin.000002  模板  下载
2022-11-12-mysql-all.sql  mmss-mysql-all.sql  mysql_bin.index   视频  音乐
anaconda-ks.cfg           -mysql-all.sql      zabbix            图片  桌面
initial-setup-ks.cfg      mysql_bin.000001    公共              文档
[root@mysql-master1 ~]# scp -r  mmss-mysql-all.sql mysql-slave1:/tmp 
Warning: Permanently added the ECDSA host key for IP address '192.168.200.183' to the list of known hosts.
root@mysql-slave1's password: 
mmss-mysql-all.sql                                               100% 7525KB  18.8MB/s   00:00    
[root@mysql-master1 ~]# scp -r  mmss-mysql-all.sql mysql-slave2:/tmp 
The authenticity of host 'mysql-slave2 (192.168.200.185)' can't be established.
ECDSA key fingerprint is SHA256:ygT6h9ejxNmaemQtyIzVYHEbRko0BaG4PstS2LTavDM.
ECDSA key fingerprint is MD5:88:1f:c4:d2:fe:73:b9:1f:7f:26:cd:2c:ba:ad:5c:b5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'mysql-slave2,192.168.200.185' (ECDSA) to the list of known hosts.
root@mysql-slave2's password: 
mmss-mysql-all.sql                                               100% 7525KB  29.3MB/s   00:00    

登录mysql-slave1 查看是否接收到mysql-master1发送的日志

[root@mysql-slave1 ~]# ls /tmp
mmss-mysql-all.sql  

[root@mysql-slave2 ~]# ls /tmp
mmss-mysql-all.sql 

[root@mysql-slave1 ~]# mysql -p'guan123456' < /tmp/mmss-mysql-all.sql  
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@mysql-slave1 ~]# mysql -uroot -p'guan123456'
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 8
Server version: 5.7.40 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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 |
| master1db          |
| mysql              |
| performance_schema |
| sys                |
| testdb             |
| zabbix             |
+--------------------+
7 rows in set (0.00 sec)

mysql> 
      
[root@mysql-slave2 ~]# mysql -p'guan123456' < /tmp/mmss-mysql-all.sql  
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@mysql-slave2 ~]# mysql -uroot -p'guan123456'
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 8
Server version: 5.7.40 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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 |
| master1db          |
| mysql              |
| performance_schema |
| sys                |
| testdb             |
| zabbix             |
+--------------------+
7 rows in set (0.00 sec)

mysql> 
 
       

2.启动从服务器ID,GTID

[root@mysql-slave1 ~]# vim /etc/my.cnf
[root@mysql-slave1 ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]

server_id=3
gtid_mode=ON
enforce_gtid_consistency=1
master-info-repository=TABLE
realy-log-info-repository=TABLE
[root@mysql-slave1 ~]# systemctl restart mysqld


[root@mysql-slave2 ~]# vim /etc/my.cnf
[root@mysql-slave2 ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]

server_id=4
gtid_mode=ON
enforce_gtid_consistency=1
master-info-repository=TABLE
realy-log-info-repository=TABLE

[root@mysql-slave2 ~]# systemctl restart mysqld

3.设置主服务器
mysql-slave1

[root@mysql-slave1 ~]# mysql -uroot -p'guan123456'
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@mysql-slave1 ~]# mysql -uroot -p'123456'
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 5
Server version: 5.7.40 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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='mysql-master1',master_user='guan',master_password='guan123456',master_auto_position=1 for channel'mysql-master1';
Query OK, 0 rows affected, 2 warnings (0.13 sec)

mysql> change master to master_host='mysql-master2',master_user='guan',master_password='guan123456',master_auto_position=1 for channel'mysql-master2';
Query OK, 0 rows affected, 2 warnings (0.03 sec)

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

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: mysql-master1
                  Master_User: guan
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000012
          Read_Master_Log_Pos: 194
               Relay_Log_File: [email protected]
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql_bin.000012
             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: 194
              Relay_Log_Space: 599
              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: 73bb1733-5f2c-11ed-b77a-000c290b1839
             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: 3ff81eac-6243-11ed-be20-000c295a3e87:1-3,
73bb1733-5f2c-11ed-b77a-000c290b1839:1-4
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: mysql-master1
           Master_TLS_Version: 
*************************** 2. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: mysql-master2
                  Master_User: guan
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000001
          Read_Master_Log_Pos: 885
               Relay_Log_File: [email protected]
                Relay_Log_Pos: 414
        Relay_Master_Log_File: mysql_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: 885
              Relay_Log_Space: 646
              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: 2
                  Master_UUID: 3ff81eac-6243-11ed-be20-000c295a3e87
             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: 3ff81eac-6243-11ed-be20-000c295a3e87:1-3,
73bb1733-5f2c-11ed-b77a-000c290b1839:1-4
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: mysql-master2
           Master_TLS_Version: 
2 rows in set (0.00 sec)

ERROR: 
No query specified

mysql> 



mysql-slave2

mysql> change master to master_host='mysql-master1',master_user='guan',master_password='guan123456',master_auto_position=1 for channel'mysql-master1';
Query OK, 0 rows affected, 2 warnings (0.06 sec)

mysql> change master to master_host='mysql-master2',master_user='guan',master_password='guan123456',master_auto_position=1 for channel'mysql-master2';
Query OK, 0 rows affected, 2 warnings (0.02 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: mysql-master1
                  Master_User: guan
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000012
          Read_Master_Log_Pos: 194
               Relay_Log_File: [email protected]
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql_bin.000012
             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: 194
              Relay_Log_Space: 599
              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: 73bb1733-5f2c-11ed-b77a-000c290b1839
             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: 3ff81eac-6243-11ed-be20-000c295a3e87:1-3,
73bb1733-5f2c-11ed-b77a-000c290b1839:1-4
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: mysql-master1
           Master_TLS_Version: 
*************************** 2. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: mysql-master2
                  Master_User: guan
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000001
          Read_Master_Log_Pos: 885
               Relay_Log_File: [email protected]
                Relay_Log_Pos: 414
        Relay_Master_Log_File: mysql_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: 885
              Relay_Log_Space: 646
              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: 2
                  Master_UUID: 3ff81eac-6243-11ed-be20-000c295a3e87
             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: 3ff81eac-6243-11ed-be20-000c295a3e87:1-3,
73bb1733-5f2c-11ed-b77a-000c290b1839:1-4
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: mysql-master2
           Master_TLS_Version: 
2 rows in set (0.01 sec)

ERROR: 
No query specified

验证是否成功
1.先往mysql-master1里插入数据

[root@mysql-master1 ~]# mysql -uroot -p'guan123456'
mysql> select * from master1db.master1tab;
+--------+
| name   |
+--------+
| 1111   |
| 2222   |
| 333333 |
| 444444 |
| 119    |
| 120    |
| 121    |
| 122    |
+--------+
8 rows in set (0.20 sec)

mysql> insert into  master1db.master1tab values(123);
Query OK, 1 row affected (0.14 sec)

mysql> select * from master1db.master1tab;
+--------+
| name   |
+--------+
| 1111   |
| 2222   |
| 333333 |
| 444444 |
| 119    |
| 120    |
| 121    |
| 122    |
| 123    |
+--------+
9 rows in set (0.00 sec)

mysql> 

mysql-master2

[root@mysql-master1 ~]# mysql -uroot -p'guan123456'
mysql> select * from master1db.master1tab;
+--------+
| name   |
+--------+
| 1111   |
| 2222   |
| 333333 |
| 444444 |
| 119    |
| 120    |
| 121    |
| 122    |
| 123    |
+--------+
9 rows in set (0.00 sec)

mysql> 

mysql-slave1

[root@mysql-master1 ~]# mysql -uroot -p'guan123456'
mysql> select * from master1db.master1tab;
+--------+
| name   |
+--------+
| 1111   |
| 2222   |
| 333333 |
| 444444 |
| 119    |
| 120    |
| 121    |
| 122    |
| 123    |
+--------+
9 rows in set (0.00 sec)

mysql> 

mysql-slave2

[root@mysql-master1 ~]# mysql -uroot -p'guan123456'
mysql> select * from master1db.master1tab;
+--------+
| name   |
+--------+
| 1111   |
| 2222   |
| 333333 |
| 444444 |
| 119    |
| 120    |
| 121    |
| 122    |
| 123    |
+--------+
9 rows in set (0.00 sec)

mysql> 

五、代理技术

1.代理简介

2.Mycat 实战练习

2.1配置JAVA 环境

在oracle官网下载:java的包并上传到服务器上
oracle官网地址:https://www.oracle.com/java/technologies/downloads/
【MySQL入门指北】主从复制及读写分离_第7张图片

[root@mycat ~]# ls
anaconda-ks.cfg                             yum-metadata-parser-1.1.4-10.el7.x86_64.rpm        模板  下载
initial-setup-ks.cfg                        yum-plugin-fastestmirror-1.1.31-34.el7.noarch.rpm  视频  音乐
mysql80-community-release-el7-7.noarch.rpm  zabbix                                             图片  桌面
yum-3.4.3-132.el7.centos.0.1.noarch.rpm     公共                                               文档
[root@mycat ~]# cd 桌面
[root@mycat 桌面]# ls
jdk-8u351-linux-x64.tar.gz                  yum-3.4.3-168.el7.centos.noarch.rpm
mysql80-community-release-el7-7.noarch.rpm  yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
nginx-1.22.0                                yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm
nginx-1.22.0.tar.gz                         zabbix-6.0.9.tar.gz
VMwareTools-10.3.10-13959562.tar.gz         zabbix-release-4.0-2.el7.noarch.rpm
vmware-tools-distrib                        zabbix.shell
[root@mycat 桌面]# cp jdk-8u351-linux-x64.tar.gz /root
[root@mycat 桌面]# cd
[root@mycat ~]# ls
anaconda-ks.cfg                             yum-3.4.3-132.el7.centos.0.1.noarch.rpm            公共  文档
initial-setup-ks.cfg                        yum-metadata-parser-1.1.4-10.el7.x86_64.rpm        模板  下载
jdk-8u351-linux-x64.tar.gz                  yum-plugin-fastestmirror-1.1.31-34.el7.noarch.rpm  视频  音乐
mysql80-community-release-el7-7.noarch.rpm  zabbix                                             图片  桌面
[root@mycat ~]# tar xf jdk-8u351-linux-x64.tar.gz -C /usr/local/
[root@mycat ~]# ls /usr/local
bin  etc  games  include  jdk1.8.0_351  lib  lib64  libexec  nginx  sbin  share  src
[root@mycat ~]# ln -s /usr/local/jdk1.8.0_351/ /usr/local/java
[root@mycat ~]# ls -l /usr/local
总用量 0
drwxr-xr-x.  2 root root   6 411 2018 bin
drwxr-xr-x.  2 root root   6 411 2018 etc
drwxr-xr-x.  2 root root   6 411 2018 games
drwxr-xr-x.  2 root root   6 411 2018 include
lrwxrwxrwx   1 root root  24 1112 23:24 java -> /usr/local/jdk1.8.0_351/
drwxr-xr-x   8 root root 273 1112 23:23 jdk1.8.0_351
drwxr-xr-x.  2 root root   6 411 2018 lib
drwxr-xr-x.  2 root root   6 411 2018 lib64
drwxr-xr-x.  2 root root   6 411 2018 libexec
drwxr-xr-x. 11 root root 151 821 23:14 nginx
drwxr-xr-x.  2 root root   6 411 2018 sbin
drwxr-xr-x.  5 root root  49 417 2022 share
drwxr-xr-x.  2 root root   6 411 2018 src


[root@mycat ~]# vim  /etc/profile  //添加三行内容,设置JAVA变量,便于JAVA调用
....

JAVA_HOME=/usr/local/java
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH

[root@mycat ~]# tail -5 /etc/profile
unset i
unset -f pathmunge
JAVA_HOME=/usr/local/java
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH

[root@mycat ~]# source /etc/profile
[root@mycat ~]# echo $JAVA_HOME
/usr/local/java

[root@mycat ~]# java -version
java version "1.8.0_351"
Java(TM) SE Runtime Environment (build 1.8.0_351-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.351-b10, mixed mode)

2.2配置Mycat

下载mycat
Mycat官网地址:http://www.mycat.org.cn/
【MySQL入门指北】主从复制及读写分离_第8张图片

Mycat下载地址:http://dl.mycat.org.cn/1.6-RELEASE/
【MySQL入门指北】主从复制及读写分离_第9张图片
上传下载好的Mycat包到服务器上

[root@mycat 桌面]# ls
jdk-8u351-linux-x64.tar.gz                            yum-3.4.3-168.el7.centos.noarch.rpm
Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz  yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
mysql80-community-release-el7-7.noarch.rpm            yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm
nginx-1.22.0                                          zabbix-6.0.9.tar.gz
nginx-1.22.0.tar.gz                                   zabbix-release-4.0-2.el7.noarch.rpm
VMwareTools-10.3.10-13959562.tar.gz                   zabbix.shell
vmware-tools-distrib
[root@mycat 桌面]# cp Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz /root
[root@mycat 桌面]# cd
[root@mycat ~]# ls
anaconda-ks.cfg                                       yum-3.4.3-132.el7.centos.0.1.noarch.rpm            模板  音乐
initial-setup-ks.cfg                                  yum-metadata-parser-1.1.4-10.el7.x86_64.rpm        视频  桌面
jdk-8u351-linux-x64.tar.gz                            yum-plugin-fastestmirror-1.1.31-34.el7.noarch.rpm  图片
Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz  zabbix                                             文档
mysql80-community-release-el7-7.noarch.rpm            公共                                               下载



[root@mycat ~]# tar xf Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz -C /usr/local/
[root@mycat ~]# ls /usr/local/mycat
bin  catlet  conf  lib  logs  version.txt
[root@mycat ~]# ls /usr/local/mycat/logs
[root@mycat ~]# 

2.3配置 mycat 前端
注释掉多余的用户
【MySQL入门指北】主从复制及读写分离_第10张图片

启动 mycat 管理员
【MySQL入门指北】主从复制及读写分离_第11张图片

2.4配置 mycat 后端
备份schema.xml文件防止改错

[root@mycat ~]# cp /usr/local/mycat/conf/schema.xml .
[root@mycat ~]# ls
anaconda-ks.cfg                                       zabbix
initial-setup-ks.cfg                                  公共
jdk-8u351-linux-x64.tar.gz                            模板
Mycat-server-1.6-RELEASE-20161028204710-linux.tar.gz  视频
mysql80-community-release-el7-7.noarch.rpm            图片
schema.xml                                            文档

[root@mycat ~]#vim /usr/local/mycat/conf/schema.xml

删除以下内容
【MySQL入门指北】主从复制及读写分离_第12张图片
错误配置文件

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">

        <schema name="guanDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">

        <dataNode name="dn1" dataHost="localhost1" database="guanDB" />

        <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
         writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
                <heartbeat>select user()</heartbeat>
                <writeHost host="mysql-master1" url="mysql-master1:3306" user="mycatproxy" password="123456">
                        <readHost host="mysql-slave1" url="mysql-slave1:3306" user="mycatproxy" password="123456" />
                        <readHost host="mysql-slave2" url="mysql-slave2:3306" user="mycatproxy" password="123456" />
                </writeHost>


                <writeHost host="mysql-master2" url="mysql-master1:3306" user="mycatproxy" password="123456">
                        <readHost host="mysql-slave1" url="mysql-slave1:3306" user="mycatproxy" password="123456" />
                        <readHost host="mysql-slave2" url="mysql-slave2:3306" user="mycatproxy" password="123456" />
                </writeHost>
</mycat:schema>

可以通过日志找到错误

[root@mycat ~]# /usr/local/mycat/bin/mycat start
Starting Mycat-server...
[root@mycat ~]# netstat -anpt | grep java
[root@mycat ~]# ps aux |grep java
root      62830  0.0  0.0 112824   980 pts/0    R+   01:54   0:00 grep --color=auto java
[root@mycat ~]# ps aux |grep mycat
root      62848  0.0  0.0 112824   980 pts/0    R+   01:54   0:00 grep --color=auto mycat

[root@mycat ~]# cd /usr/local/mycat/logs
[root@mycat logs]# ls
mycat.log  wrapper.log

[root@mycat logs]# cat /usr/local/mycat/logs/mycat.log 
[root@mycat logs]# cat /usr/local/mycat/logs/wrapper.log 
STATUS | wrapper  | 2022/11/13 01:52:37 | --> Wrapper Started as Daemon
STATUS | wrapper  | 2022/11/13 01:52:37 | Launching a JVM...
INFO   | jvm 1    | 2022/11/13 01:52:37 | Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=64M; support was removed in 8.0
INFO   | jvm 1    | 2022/11/13 01:52:39 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
java.lang.Thread.run(Thread.java:750)
INFO   | jvm 1    | 2022/11/13 01:52:41 | Caused by: io.mycat.config.util.ConfigException: org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 3; 元素类型 "dataHost" 必须由匹配的结束标记 "" 终止。
INFO   | jvm 1    | 2022/11/13 01:52:41 | 	at 
INFO   | jvm 1    | 2022/11/13 01:52:41 | Caused by: org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 3; 元素类型 "dataHost" 必须由匹配的结束标记 "" 终止。
INFO   | jvm 1    | 2022/11/13 01:52:41 | 	at 
INFO   | jvm 1    | 2022/11/13 01:52:41 | 	... 13 more
STATUS | wrapper  | 2022/11/13 01:52:43 | <-- Wrapper Stopped

最终配置文件

<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
//schema name: mycat 维护的集群名称
        <schema name="guanDB" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1"></schema>

        <dataNode name="dn1" dataHost="localhost1" database="guanDB" />
//datanode: 后方节点群的名称   datahost: 后方节点群的主机名称
        <dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
         writeType="0" dbType="mysql" dbDriver="native" switchType="1"  slaveThreshold="100">
         //在本案列中switchType值设置为1,表示自动切换,某些对主从数据一致要求较高的场景,建议使用2判断主从状态后在切换,切换的触发条件为主节点mysql服务器崩溃或停止
slaveThreshold主从的延迟在多少秒以内,则把读请求分发到这个从节点,否则不往这个节点分发,假设生产环境能容忍的主从延时为60秒,则设置此值为60,
此案例中设置为60
                <heartbeat>select user()</heartbeat>
                <writeHost host="mysql-master1" url="mysql-master1:3306" user="mycatproxy" password="123456">
                //writehost: 写主机
                        <readHost host="mysql-slave1" url="mysql-slave1:3306" user="mycatproxy" password="123456" />
                //readHost: 读主机
                        <readHost host="mysql-slave2" url="mysql-slave2:3306" user="mycatproxy" password="123456" />
                </writeHost>


                <writeHost host="mysql-master2" url="mysql-master1:3306" user="mycatproxy" password="123456">
                        <readHost host="mysql-slave1" url="mysql-slave1:3306" user="mycatproxy" password="123456" />
                        <readHost host="mysql-slave2" url="mysql-slave2:3306" user="mycatproxy" password="123456" />
                </writeHost>
</dataHost>
</mycat:schema>

balance 类型

1.balance=“0”,关闭读写分离功能。所有读操作都发送到当前可用的writeHost 上。
2.balance=“1”,开启读写分离,所有读操作都随机发送到readHost

writeType 属性

备份型
1.writeType=“0”,所有写操作发送到配置的第一个 writeHost,第一个挂了切换到还生存的第二个writeHost,重新启动后,以切换后的为准,切换记录在配置文件中:dnindex.properties.

负载型
2.writeType=“1”,所有写操作都随机发送到配置的 writeHost

switchType 模式

switchType 指的是切换模式,目前的取值也有4种
1.switchType=“-1” 负1表示不自动切换
2.switchTyep=“1” 默认值,表示根据延时自动切换
3.switchType="2"根据MySQL主从同步的状态决定是否切换,心跳语句为 show slave status
【MySQL入门指北】主从复制及读写分离_第13张图片

登录mysql-master1进行给 mycatproxy 用户进行授权

[root@mysql-master1 ~]# mysql -uroot -p123456

mysql> grant all on *.* to 'mycatproxy'@'192.168.200.186' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.42 sec)

2.5启动 mycat
在 mycat 服务器上启动

[root@mycat logs]# /usr/local/mycat/bin/mycat start
Starting Mycat-server...
[root@mycat logs]# netstat -anpt | grep java
tcp        0      0 127.0.0.1:32000         0.0.0.0:*               LISTEN      63558/java          
tcp6       0      0 :::39561                :::*                    LISTEN      63558/java          
tcp6       0      0 :::9066                 :::*                    LISTEN      63558/java          
tcp6       0      0 :::36852                :::*                    LISTEN      63558/java          
tcp6       0      0 :::1984                 :::*                    LISTEN      63558/java          
tcp6       0      0 :::8066                 :::*                    LISTEN      63558/java          
tcp6       0      0 127.0.0.1:31000         127.0.0.1:32000         ESTABLISHED 63558/java          

在 mycat 服务器上安装 一个客户端

[root@mycat logs]# yum install  -y mariadb
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 mariadb.x86_64.1.5.5.68-1.el7 将被 安装
--> 正在处理依赖关系 mariadb-libs(x86-64) = 1:5.5.68-1.el7,它被软件包 1:mariadb-5.5.68-1.el7.x86_64 需要
--> 正在检查事务
---> 软件包 mariadb-libs.x86_64.1.5.5.68-1.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

===================================================================================================
 Package                   架构                版本                        源                 大小
===================================================================================================
正在安装:
 mariadb                   x86_64              1:5.5.68-1.el7              base              8.8 M
为依赖而安装:
 mariadb-libs              x86_64              1:5.5.68-1.el7              base              760 k

事务概要
===================================================================================================
安装  1 软件包 (+1 依赖软件包)

总下载量:9.5 M
安装大小:53 M
Downloading packages:
(1/2): mariadb-libs-5.5.68-1.el7.x86_64.rpm                                 | 760 kB  00:00:00     
(2/2): mariadb-5.5.68-1.el7.x86_64.rpm                                      | 8.8 MB  00:00:01     
---------------------------------------------------------------------------------------------------
总计                                                               8.3 MB/s | 9.5 MB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : 1:mariadb-libs-5.5.68-1.el7.x86_64                                             1/2 
  正在安装    : 1:mariadb-5.5.68-1.el7.x86_64                                                  2/2 
  验证中      : 1:mariadb-libs-5.5.68-1.el7.x86_64                                             1/2 
  验证中      : 1:mariadb-5.5.68-1.el7.x86_64                                                  2/2 

已安装:
  mariadb.x86_64 1:5.5.68-1.el7                                                                    

作为依赖被安装:
  mariadb-libs.x86_64 1:5.5.68-1.el7                                                               

完毕!

[root@mycat logs]# netstat -anpt |grep 3306


[root@mycat logs]# mysql -hmycat -uroot -p'guan123456' -P8066
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.29-mycat-1.6-RELEASE-20161028204710 MyCat Server (OpenCloundDB)

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

MySQL [(none)]> 

登录mysql-master1创建数据库并且创建表,然后往表里面插入数据
因为mycat 只有查询和读的权限,没有写的权限

[root@mysql-master1 ~]# mysql -uroot -p123456

mysql> create database guanDB;
Query OK, 1 row affected (0.00 sec)

mysql> create table guanDB.test1 (id int);
Query OK, 0 rows affected (0.11 sec)

mysql> insert into guanDB.test1 values(123456);
Query OK, 1 row affected (0.04 sec)


在mycat 服务器上进行查询

[root@mycat logs]# mysql -hmycat -uroot -p'guan123456' -P8066

MySQL [(none)]> show databases;
+----------+
| DATABASE |
+----------+
| guanDB   |
+----------+
1 row in set (0.00 sec)

MySQL [(none)]> select * from guanDB.test1;
+--------+
| id     |
+--------+
| 123456 |
+--------+
1 row in set (0.22 sec)

MySQL [(none)]> 


你可能感兴趣的:(MySQL,mysql,数据库,服务器)