Mysql高阶第一章 主从架构的复现

这里写目录标题

  • 前言
  • 第一章 找到主数据库和从数据库对应的虚拟机
    • 1.1 查对应虚拟机IP
    • 1.2 rpm 查找数据库是否安装
    • 1.3 查找数据库详细配置信息
  • 第二章 启动主从复制
    • 2.1 SHOW SLAVE STATUS\G;
    • 2.2 Slave_IO_Running: No
    • 2.3 解决方案
      • 2.3.1 进入slave中输入,停止从库同步:
      • 2.3.2 进入master中输入,清空日志
      • 2.3.3 进入master中输入,记录状态信息
      • 2.3.4 之后进入从库配置,不要再动主库
      • 2.3.5 测试
      • 2.3.6 从库里面一开始没有数据
      • 2.7 测试后验证确实多了一条数据
  • 第三章 验证主库

前言

这其实是一个很久以前搭的项目,五月在面移动的时候,面试官就问了我知晓哪些数据库的高级特性,有没有做过集群部署,我说我没有做过,只是搭建过分布式,然后就是分布式里面的主从架构。但是这个时候又要涉及到主从架构里面的一些原理问题。
咱们就先来考虑一下一主一从架构
本文并不是从0到1的搭建过程,而是从1到1.01的练习版本,如果有想搭建的,可以在小破站上看康师傅讲解。
Mysql高阶第一章 主从架构的复现_第1张图片

第一章 找到主数据库和从数据库对应的虚拟机

1.1 查对应虚拟机IP

当时配置的时候,虚拟机配的太多了,搞得直接分辨不了这些虚拟机哪个是哪个了
Mysql高阶第一章 主从架构的复现_第2张图片如上图所示,链接建的太多了

来确定有没有安装数据库:

1.2 rpm 查找数据库是否安装

rpm -qa | grep mysql

查出来这个结果

[root@centos121 ~]# rpm -qa | grep mysql
mysql-community-client-plugins-8.0.25-1.el7.x86_64
mysql-community-client-8.0.25-1.el7.x86_64
mysql-community-common-8.0.25-1.el7.x86_64
mysql-community-libs-8.0.25-1.el7.x86_64
mysql-community-server-8.0.25-1.el7.x86_64

1.3 查找数据库详细配置信息

这一步需要确定哪个
最后的解决方案,就是vim my.cnf,一个个看配置信息,因为我当时是照着尚硅谷那个康师傅Mysql配的嘛
同时看那个初始的参照配置文档

主节点的特点是如下图所示
Mysql高阶第一章 主从架构的复现_第3张图片
从节点中,my.cnf 是这样的
Mysql高阶第一章 主从架构的复现_第4张图片
记下这两个设备的IP地址

主:192.168.80.121
从:192.168.80.120

第二章 启动主从复制

这个启动并不是很顺利,好不容易找到主库和从库,结果呢,找不到

2.1 SHOW SLAVE STATUS\G;

标题这个语句其实是有问题的,问题的关键就是这个
不可以加这个符号

ERROR: 
No query specified

2.2 Slave_IO_Running: No

这个错误具体来是这样的,类似于binary log的读取出现了问题,在slave这个数据库里面,它会报1236的错误。

Last_IO_Error: Got fatal error 1236 from master解决方法
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'
               Last_SQL_Errno: 0

由于清数据导致主从库不同步了

2.3 解决方案

2.3.1 进入slave中输入,停止从库同步:

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

2.3.2 进入master中输入,清空日志

mysql> flush logs;
Query OK, 0 rows affected (0.02 sec)

2.3.3 进入master中输入,记录状态信息

mysql> show master status;
+----------------+----------+--------------+------------------+-------------------+
| File           | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+----------+--------------+------------------+-------------------+
| xyt-bin.000005 |      156 |              |                  |                   |
+----------------+----------+--------------+------------------+-------------------+

2.3.4 之后进入从库配置,不要再动主库

mysql> CHANGE MASTER TO MASTER_LOG_FILE='xyt-bin.000005',MASTER_LOG_POS=156;
Query OK, 0 rows affected, 3 warnings (0.01 sec)

mysql>  start slave;
Query OK, 0 rows affected, 1 warning (0.03 sec)

mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.80.121
                  Master_User: slave1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: xyt-bin.000005
          Read_Master_Log_Pos: 156
               Relay_Log_File: centos121-slave-relay-bin.000002
                Relay_Log_Pos: 322
        Relay_Master_Log_File: xyt-bin.000005
             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: 156
              Relay_Log_Space: 541
              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: d2f34f1e-df67-11ed-bf42-000c29229240
             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: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set, 1 warning (0.00 sec)

2.3.5 测试

Mysql高阶第一章 主从架构的复现_第5张图片

2.3.6 从库里面一开始没有数据

Mysql高阶第一章 主从架构的复现_第6张图片

2.7 测试后验证确实多了一条数据

Mysql高阶第一章 主从架构的复现_第7张图片

第三章 验证主库

Mysql高阶第一章 主从架构的复现_第8张图片

121确实是主节点
Mysql高阶第一章 主从架构的复现_第9张图片
二次验证配置完成

你可能感兴趣的:(三,mysql,架构,数据库)