首页 > 数据库 >mysql主从切换—一主两从架构

mysql主从切换—一主两从架构

时间:2023-05-29 19:31:55浏览次数:43  
标签:rows 0.00 Source sec mysql 一主 NULL 主从

文档课题:mysql主从切换.
数据库:mysql 8.0.27
系统:rhel 7.3
安装包:mysql-8.0.27-linux-glibc2.12-x86_64.tar.xz
系统架构:一主两从
环境介绍:

mysql主从切换—一主两从架构_一主两从架构

1、应用场景
生产环境中,当主库A发生故障时,需要将从库B切换成主库,如下模拟主库Master宕机,slave01升级为Master,slave02保持不变,并实现slave01和slave02数据同步.
2、模拟异常
主库A 192.168.133.111将mysql进程关闭,模拟异常.
mysql> shutdown;
Query OK, 0 rows affected (0.00 sec)

mysql> system service mysql status;
 ERROR! MySQL is not running, but lock file (/var/lock/subsys/mysql) exists
3、确认中继日志
确保从库已执行完relay log中的全部更新,查看从库状态是否为has read all relay log.
3.1、查Slave01
mysql> stop slave io_thread;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show processlist \G
*************************** 1. row ***************************
     Id: 5
   User: event_scheduler
   Host: localhost
     db: NULL
Command: Daemon
   Time: 9918
  State: Waiting on empty queue
   Info: NULL
*************************** 2. row ***************************
     Id: 8
   User: root
   Host: localhost
     db: booksDB
Command: Query
   Time: 0
  State: init
   Info: show processlist
*************************** 3. row ***************************
     Id: 16
   User: system user
   Host: 
     db: NULL
Command: Query
   Time: 6812
  State: Replica has read all relay log; waiting for more updates
   Info: NULL
*************************** 4. row ***************************
     Id: 17
   User: system user
   Host: 
     db: NULL
Command: Connect
   Time: 6812
  State: Waiting for an event from Coordinator
   Info: NULL
*************************** 5. row ***************************
     Id: 18
   User: system user
   Host: 
     db: NULL
Command: Connect
   Time: 8302
  State: Waiting for an event from Coordinator
   Info: NULL
*************************** 6. row ***************************
     Id: 19
   User: system user
   Host: 
     db: NULL
Command: Connect
   Time: 8302
  State: Waiting for an event from Coordinator
   Info: NULL
*************************** 7. row ***************************
     Id: 20
   User: system user
   Host: 
     db: NULL
Command: Connect
   Time: 8302
  State: Waiting for an event from Coordinator
   Info: NULL
7 rows in set (0.00 sec)
3.2、查slave02
mysql> stop slave io_thread;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> show processlist;
+----+-----------------+-----------+---------+---------+-------+----------------------------------------------------------+-------
| Id | User            | Host      | db      | Command | Time  | State                                                    | Info             |
+----+-----------------+-----------+---------+---------+-------+----------------------------------------------------------+------------------+
|  5 | event_scheduler | localhost | NULL    | Daemon  | 11573 | Waiting on empty queue                                   | NULL             |
|  8 | root            | localhost | booksDB | Query   |     0 | init                                                     | show processlist |
| 10 | system user     |           | NULL    | Query   |  8516 | Replica has read all relay log; waiting for more updates | NULL             |
| 11 | system user     |           | NULL    | Connect |  8516 | Waiting for an event from Coordinator                    | NULL             |
| 12 | system user     |           | NULL    | Connect |  8629 | Waiting for an event from Coordinator                    | NULL             |
| 13 | system user     |           | NULL    | Connect |  8629 | Waiting for an event from Coordinator                    | NULL             |
| 14 | system user     |           | NULL    | Connect |  8629 | Waiting for an event from Coordinator                    | NULL             |
+----+-----------------+-----------+---------+---------+-------+----------------------------------------------------------+-------
7 rows in set (0.00 sec)
4、开始切换
停止从库B slave服务,然后执行reset master,将其重置成主库.
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> reset master;
Query OK, 0 rows affected (0.02 sec)

说明:从库B配置文件中需开启binlog,否则无法执行reset maste.
5、切换后操作
5.1、建复制用户
从库B切换为主库后,在新主库B上添加具有replication权限的用户repluser.
mysql> create user 'repluser'@'%' identified by 'repluser'; 
Query OK, 0 rows affected (0.01 sec)

mysql> grant replication slave,replication client on *.* to 'repluser'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> show master status \G
*************************** 1. row ***************************
             File: slave_log.000001
         Position: 732
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)
5.2、删新主库中继日志
删新主库B上的relay-log,否则下次重启会按照从库启动.
对配置文件my.cnf如下参数进行注释.
#relay-log=mysql-slave1-log
#read_only=ON
#relay_log_index=relay-log.index
5.3、从库C配置
在从库C上配置复制参数,具体操作如下.
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> change master to master_host='192.168.133.112';
Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> show warnings;
+---------+------+---------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                             |
+---------+------+---------------------------------------------------------------------------------------------------------------------+
| Warning | 1287 | 'CHANGE MASTER' is deprecated and will be removed in a future release. Please use CHANGE REPLICATION SOURCE instead |
| Warning | 1287 | 'MASTER_HOST' is deprecated and will be removed in a future release. Please use SOURCE_HOST instead                 |
+---------+------+---------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

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

mysql> show warnings;
+---------+------+-------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                               |
+---------+------+-------------------------------------------------------------------------------------------------------+
| Warning | 1287 | 'START SLAVE' is deprecated and will be removed in a future release. Please use START REPLICA instead |
+---------+------+-------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> show replica status \G
*************************** 1. row ***************************
             Replica_IO_State: Waiting for source to send event
                  Source_Host: 192.168.133.112
                  Source_User: repluser
                  Source_Port: 3306
                Connect_Retry: 60
              Source_Log_File: slave_log.000001
          Read_Source_Log_Pos: 732
               Relay_Log_File: mysql-slave1-log.000002
                Relay_Log_Pos: 947
        Relay_Source_Log_File: slave_log.000001
           Replica_IO_Running: Yes
          Replica_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_Source_Log_Pos: 732
              Relay_Log_Space: 1157
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Source_SSL_Allowed: No
           Source_SSL_CA_File: 
           Source_SSL_CA_Path: 
              Source_SSL_Cert: 
            Source_SSL_Cipher: 
               Source_SSL_Key: 
        Seconds_Behind_Source: 0
Source_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Source_Server_Id: 2
                  Source_UUID: ed0ed633-fd2c-11ed-af12-0050563cca0d
             Source_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
    Replica_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Source_Retry_Count: 86400
                  Source_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Source_SSL_Crl: 
           Source_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Source_TLS_Version: 
       Source_public_key_path: 
        Get_Source_public_key: 1
            Network_Namespace: 
1 row in set (0.00 sec)

说明:如上所示,slave_io_running和slave_sql_running均为yes,主从切换成功.
6、数据验证
6.1、新主库B删数据
主库B执行如下:
mysql> use booksDB
Database changed
mysql> show tables;
+-------------------+
| Tables_in_booksDB |
+-------------------+
| books             |
+-------------------+
1 row in set (0.00 sec)

mysql> select * from books;
+-------+---------------------------+-----------+
| bk_id | bk_title                  | copyright |
+-------+---------------------------+-----------+
| 11026 | Guide to MySQL 5.7        |      2008 |
| 11028 | Learning C++              |      2005 |
| 11033 | Study Html                |      2011 |
| 11035 | How to use php            |      2003 |
| 11041 | Inside VC++               |      2011 |
| 11069 | MYSQL professional        |      2009 |
| 11072 | Teach yourself javascript |      2005 |
| 11078 | Learning MYSQL            |      2010 |
+-------+---------------------------+-----------+
8 rows in set (0.00 sec)

mysql> delete from books where bk_id=11026;
Query OK, 1 row affected (0.00 sec)

mysql> select * from books;
+-------+---------------------------+-----------+
| bk_id | bk_title                  | copyright |
+-------+---------------------------+-----------+
| 11028 | Learning C++              |      2005 |
| 11033 | Study Html                |      2011 |
| 11035 | How to use php            |      2003 |
| 11041 | Inside VC++               |      2011 |
| 11069 | MYSQL professional        |      2009 |
| 11072 | Teach yourself javascript |      2005 |
| 11078 | Learning MYSQL            |      2010 |
+-------+---------------------------+-----------+
7 rows in set (0.00 sec)
6.2、从库C验证
mysql> use booksDB
Database changed
mysql> select * from books;
+-------+---------------------------+-----------+
| bk_id | bk_title                  | copyright |
+-------+---------------------------+-----------+
| 11028 | Learning C++              |      2005 |
| 11033 | Study Html                |      2011 |
| 11035 | How to use php            |      2003 |
| 11041 | Inside VC++               |      2011 |
| 11069 | MYSQL professional        |      2009 |
| 11072 | Teach yourself javascript |      2005 |
| 11078 | Learning MYSQL            |      2010 |
+-------+---------------------------+-----------+
7 rows in set (0.00 sec)

说明:切换后,新主库B和从库C数据实时同步.

参考网址:https://www.cnblogs.com/linjiqin/p/11208948.html

标签:rows,0.00,Source,sec,mysql,一主,NULL,主从
From: https://blog.51cto.com/u_12991611/6373696

相关文章

  • 安装mysql 压缩版数据库
    1、下载压缩版压缩包下载地址:https://dev.mysql.com/downloads/mysql/5.5.html#downloads主要步骤1、解压文件,切换到bin目录,复制地址D:\Software\mysql\mysql-8.0.30-winx64\bin2、在系统变量的path中添加地址3、创建添加my.ini文件添加一下内容[mysql]#设置mysq......
  • 11)MySQL编程基础
    1、用户会话变量:会话期间一直有效;但其他的客户机不能访问;一般情况下,用户会话变量的定义与赋值会同时进行; 方法一:使用set命令定义用户会话变量,并为其赋值;set@user_variable1=expre1[,@user_variable2=expre2,...];方法二:使用select语句定义用户会话变量,并为其赋值;sele......
  • MySQL之慢查询sql排查及优化
    前言sql语句优化的方式:1.尽量少join2.尽量少排序3.尽量避免select*4.尽量少or5.尽量用unionall代替union…(优化的方式有很多,这里就不一一举例了)当你避免这些问题的时候,为什么sql查询还是这么慢?排查慢查询sqlps:mysql版本为5.71.连接mysqlmysql-uroot-p2.查......
  • Mysql 字符集问题:utf8与utf8mb4
    UTF-8编码是一种变长的编码机制,可以用1~4个字节存储字符。MySQL中的utf8编码并不是真正的UTF-8,其最长只有3个字节。当遇到占4个字节的UTF-8编码,例如emoji字符或者复杂的汉字,会导致存储异常。从5.5.3开始,MySQL开始用utf8mb4编码来实现完整的UTF-8,其中mb4表示mo......
  • 如何将数据从MySQL/MongoDB中迁移至云开发数据库
    本篇文章从MySQL、MongoDB迁移到云开发数据库,其他数据库迁移也都大同小异~迁移大致分为以下几步?:从MySQL、MongoDB将数据库导出为JSON或CSV格式创建一个云开发环境到云开发数据库新建一个集合在集合内导入JSON或CSV格式文件Mysql迁移到云开发数据库为了方便,我们使用Na......
  • mysql 8 修改默认密码并修改密码策略
    1、使用默认密码登陆2、修改默认密码(必须修改默认密码)ALTERUSER'root'@'localhost'IDENTIFIEDBY'Mysql@23';3、修改默认密码策略SHOWVARIABLESLIKE'validate_password%';setglobalvalidate_password.policy=0;setglobalvalidate_password.lengt......
  • mysql之sql查询结果集小数保留
    客户要求,跟金额相关的数据,打印出来要保留两位小数,比如:13.2/13.200要显示为13.20;分析:首先查看数据库中的数据定义,均为decimal(12,2),直接通过数据库查询的结果也会保留两位小数,但是代码处理后,却会忽略掉最后一个“零”位。此处有两个解决方案:一、代码中处理,检测到为金额字......
  • 简单MySQL例子演示MVCC
    一沈秋园,满庭霜落,云烟北桥夜连城MVCC是多版本并发控制的缩写,是一种数据库和编程语言中常用的并发控制方法。它通过保存数据的历史版本,实现对数据库的高效访问。MySQL中MVCC主要是通过行记录中的隐藏字段(隐藏主键row_id,事务IDtrx_id,回滚指针roll_pointer),undo_log(版本链),Rea......
  • docker部署ZeLog项目(Tomcat+MySQL+ZrLog)
    一.docker的安装1.1、在安装dockers之前,建议关闭Centos7.9防火墙[root@192~]#systemctlstopfirewalld[root@192~]#setenforce0 setenforce是Linux的selinux防火墙配置命令执行setenforce0表示临时关闭selinux防火墙1.2、通过yum在线安装Docker服务[root@localhos......
  • mysql快速对比两个数据库表名的差异
    2023年5月29日14:51:02因为正式库老是和测试库有差1-2张表,表也有100张了,排除差异有点麻烦SELECTtable_nameFROMinformation_schema.TABLESWHEREtable_schema='数据库名'这样就得到两个数据库表名列新建两个txt,database.txt和database_test.txt选中两个文件,右键使用T......