原文:http://6226001001.blog.51cto.com/9243584/1723273

1> 正常切换

1)从服务器检查SHOW PROCESSLIST语句的输出,直到你看到Has read all relaylogwaiting for the slave I/O thread to update it

2)确保从服务器已经处理了日志中的所有语句。 mysql> STOP SLAVE IO_THREAD

当从服务器都执行完这些,它们可以被重新配置为一个新的设置。

3)在被提升为主服务器的从服务器上,发出 STOP SLAVE和RESET MASTER和RESET SLAVE操作。

4)然后重启mysql服务。

5)在备用服务器(新的主服务器)创建用户grant replication slave on *.* torepdcs@'IP of A' identified by 'replpwd'

grant allprivileges on *.* to 'repdcs'@'IP' identified by replpwd;

6) 在主服务器上RESET MASTER。然后CHANGE MASTER TOMASTER_HOST='原从服务器IP',MASTER_USER='用户名',MASTER_PASSWORD='密码' ,master_log_file='master-bin.000015' ;

7)查看状态 show slave status \G;

Show master status \G;

如果还不行再次重启主库,并在备库上执行CHANGE MASTER TOMASTER_HOST='192.168.0.110',MASTER_USER='repdcs',MASTER_PASSWORD='111111',master_log_file='mysqlbin.000002';

8)修改应用的连接地址到新的主库

切换完成。

2> 主机直接宕机

1> 在备机上执行STOP SLAVE 和RESET MASTER

2> 查看show slave status \G;

3> 然后修改应用的连接地址。

mysql 从服务器切换为主服务器:

步骤如下:

1 确认从服务器已经完成所有同步操作:

stop slave io_thread

show processlist

直到看到状态都为:xxx has read all relay log 表示更新都执行完毕

2 停止从服务器slave服务

stop slave

3 将从服务器切换为主服务器:

reset master

完成切换。

首先保证主从数据库都开启二进制日志,方法是在my.cnf中的[mysqld]节中加入log-bin=log-bin-name

然后查看是否开启mysql> show variables;    log-bin  ON

为了在切换时不会漏掉数据库的更新,必须将主数据库停掉,设为只读:set global read_only=1;

(注意和FLUSH TABLES WITH READ LOCK;的区别, UNLOCK TABLES;解锁)

show variables; 确认 read_only  ON

然后在主库mysql> flush logs; 刷新一下log-bin,

在从库中查看mysql> show slave status \G 是否完全更新

查看主库的状态mysql> show master status \G 是否还有数据更新

如果没有就可以停掉从库mysql> stop slave;

查看新主库的状态mysql> show master status \G 记录file和pos

这时可以关掉新主库的只读属性mysql> set global read_only=0;

新从库改变连接的主库信息mysql> change master to

master_host='new_master_ip',

master_user='mysqlsync',

master_password='pass',

master_port=3306,

master_log_file='mysql-bin.xxxxxx',

master_log_pos=xxxx;

这时可以启动新从库了:mysql> start slave;

查看新从库状态:mysql> show slave status\G

介绍了 MySQL 主从配置过程,这篇 blog 介绍手工主从切换过程。
 
一 环境信息
主库  192.168.2.37/3306     主机名 db1
备库  192.168.2.38/3306     主机名 db2
版本  5.6.20
备注: 主从节点 mysql 安装略,Replication 安装略。

二 主从切换
--主,备节点都要创建 Replication 用户


create user 'rep1'@'192.168.2.%' identified by 'rep1abcd1243d';
GRANT REPLICATION SLAVE ON *.* TO 'rep1'@'192.168.2.%';

--查询从库状态

root@localhost:francs>show slave status\G

--查询主库状态


root@localhost:francs>show slave hosts;
+-----------+-------------------+------+-----------+--------------------------------------+
| Server_id | Host              | Port | Master_id | Slave_UUID                           |
+-----------+-------------------+------+-----------+--------------------------------------+
|         2 | 192.168.2.38(db2) | 3306 |         1 | ad397a06-7c56-11e4-b2fb-000c29dcb3b5 |
+-----------+-------------------+------+-----------+--------------------------------------+
1 row in set (0.00 sec)

--从库: 停止 IO_THREAD 线程


root@localhost:francs>stop slave IO_THREAD;

root@localhost:francs>show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.2.37
                  Master_User: rep1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: bin-log.000001
          Read_Master_Log_Pos: 362
               Relay_Log_File: db2-relay-bin.000002
                Relay_Log_Pos: 523
        Relay_Master_Log_File: bin-log.000001
             Slave_IO_Running: No
            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: 362
              Relay_Log_Space: 694
              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: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 0c130d47-22bb-11e4-aaaa-000c2986ac80
             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 the slave I/O thread to update it
           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
1 row in set (0.00 sec)

--激活从库(从库上操作)


root@localhost:francs>stop slave;
root@localhost:francs>reset master;
root@localhost:francs>reset slave all;

root@localhost:francs>show binary logs;
+----------------+-----------+
| Log_name       | File_size |
+----------------+-----------+
| bin-log.000001 |       120 |
+----------------+-----------+
1 row in set (0.00 sec)

备注:reset slave all 命令会删除从库的 replication 参数,之后 show slave status\G 的信息返回为空。

--将原来主库变为从库


CHANGE MASTER TO
   MASTER_HOST='192.168.2.38',
   MASTER_PORT=3306,
   MASTER_USER='rep1',
   MASTER_PASSWORD='rep1abcd1243d',
   MASTER_LOG_FILE='bin-log.000001',
   MASTER_LOG_POS=120;
   
root@localhost:francs>start slave;
root@localhost:francs>  show slave status\G

备注:这步执行之后,发现 db1 日志文件报了以下错误,提示 db1 连接不上 db2。

--db1 日志报错

2015-03-02 14:24:14 26198 [ERROR] Slave I/O: error connecting to master 'rep1@192.168.2.38:3306' - retry-time: 60  retries: 8, Error_code: 1045

--解决过程


--连接测试
[mysql@db1 ~]$ mysql -h 192.168.2.38 -P 3306 -urep1 
备注:居然不需要密码能直接能连上。

--测试匿名用户
[mysql@db1 ~]$ mysql -h 192.168.2.38 -P 3306 -urep2 
备注:依然不需要密码。

--原因分析
root@localhost:francs>select Host,User,Password from mysql.user where User='';
+-----------+------+----------+
| Host      | User | Password |
+-----------+------+----------+
| localhost |      |          |
| db1       |      |          |
+-----------+------+----------+
备注: 原来 db2 节点上存在 User 为空的的两行,表示匿名用户可以连接数据库, 删除这两行,之后  flush privileges;

--再连接测试
[mysql@db1 ~]$ mysql -h 192.168.2.38 -P 3306 -urep1 
备注:这次连接需要密码了。之后再次观看 db1 同步日志,不再报错。

三 数据验证
             主从切换后db2 为主节点, db1 为备节点,在 db2 节点上插入一条数据测试同步是否正常。
    
--db2 上执行


root@localhost:francs>insert into test_sr(id) values(30);
Query OK, 1 row affected (0.03 sec)

root@localhost:francs>select * from test_sr order by id desc limit 1;
+------+---------------------+
| id   | create_time         |
+------+---------------------+
|   30 | 2015-03-02 15:19:53 |
+------+---------------------+
1 row in set (0.00 sec)

--db1 上验证


root@localhost:francs>select * from test_sr order by id desc limit 1;
+------+---------------------+
| id   | create_time         |
+------+---------------------+
|   30 | 2015-03-02 15:19:53 |
+------+---------------------+
1 row in set (0.00 sec)

备注:数据同步正常,以上是对 MySQL 主备切换的初次了解,后续再补充。

(转)mysql主从切换步骤的更多相关文章

  1. mysql主从切换步骤

    1>   正常切换 1)从server检查SHOW PROCESSLIST语句的输出,直到你看到Has read all relaylogwaiting for the slave I/O th ...

  2. mysql主从切换

    mysql 主从切换 主停,从做主步骤如下: 1 确认从服务器已经完成所有同步操作:stop slave io_thread show processlist 直到看到状态都为:xxx has rea ...

  3. mysql 主从搭建步骤

    mysql 主从搭建步骤 1:主库开启master端bin-log 2:主库创建备份用户 3:主库全备 4:从库导入全备数据 5:从库修改change master to信息 6:从库slave st ...

  4. master_pos_wait函数与MySQL主从切换

    背景 主从切换是高可用MySQL架构的必要步骤(即使用不发生,也要有备无患).一般设置为双M(M1.M2),假设当前状态为写M1,而M2只读,切换的大致流程如下: 1.  停止应用写M1,将M1设置为 ...

  5. mysql 主从切换

    4)提升slave为master Stop slave: Reset master; Reset slave all; 在5.6.3版本之后 Reset slave; 在5.6.3版本之前 查看sla ...

  6. mysql主从配置步骤

    主服务器配置: 1)登陆MySQL数据库 mysql>mysql -uroot -p123 2)给从服务器设置授权用户 mysql>grant all slave on *.* to us ...

  7. Mysql主从配置步骤与各种错误

    测试环境: 2台腾讯云服务器.CentOS 7.2 64位,1G,lnmp. PHP:5.6:Mysql:5.5 两台干净的服务器 下面开始配置主服务器(master) 1.修改配置:  log-bi ...

  8. mysql主从切换摘要

    1.需要提升为主的从库,停止io线程等待slave数据全部更新完毕 stop slave IO_THREAD #show processlist的输出,直到看到状态是Slave has read al ...

  9. 一次mysql主从加keepalived配置搭建及切换演示

    [需求] 根据需求需要搭建mysql主从架构数据库及加keepalived进行自动切换VIP [环境介绍]  系统环境:CentOS release 6.4 (Final) + Server vers ...

随机推荐

  1. tab切换代码优化

    上次的tab切换的代码里面有很多重复的代码,需要做做优化,把重复的代码用函数封装起来调用. 优化前: <script> //获取id封装成一个函数$()方便调用 function $(id ...

  2. redis windows下安装

    1.下载redis windows文件包 下载地址 2.解压文件包 复制压缩包地址 3.进入cmd 命令行 cd进入redis文件包目录 4.执行 redis-server.exe  使用netsta ...

  3. Python + Robotframework + Appium 之APP自动化测试小试牛刀(Android)

    Robotframework如何好?这里先不说了~ Python更不用说了~ Appium前面的文章有介绍~ 今天直接来Python+Robotframework+Appium 三者结合起来,对And ...

  4. 7.使用ZookeeperNet进行CDU操作参数详解

    一.创建,删除,更新 1. zkCli.sh 中使用命令执行. 2. 学会查看api文档 zookeeper C# driver 官方文档,文档是java代码的. http://zookeeper.a ...

  5. J-Link eclipse Plug-ins install

    Quicklinks If you know what this is all about and you just need the update site details: name: GNU A ...

  6. Nutch 问题杂记

    1. 如何绕过目标站点的robots.txt限制 多数站点都是只允许百度.google等搜索引擎抓取的,所以会在robots.txt里限制其他爬虫. nutch自然是会遵循robots协议的,但是我们 ...

  7. centos 下wps 与goland 不能输入中文的解决办法

    输入法:CentOS7自带ibus,如果你用的是fcitx请在对应的地方进行修改 系统:CentOS7,这个方案应该适用于大多数Linux发行版本 intelliJ goland中文输入法问题解决 首 ...

  8. nginx 托管.net core的service文件

    在 /etc/systemd/system/ 中新建一个服务文件site1.service vim /etc/systemd/system/site1.service [Unit] Descripti ...

  9. python全栈开发_day31_OSI七层协议和c/s架构

    一:OSI七层协议 应用层 =>表示层 =>会话层 =>传输层 =>网络层 =>数据链路层 =>物理连接层 二:c/s架构 b/s的本质也是c/s 手机端:好像cs ...

  10. CTF中密码学一些基础【三】

    本文作者:i春秋签约作家——MAX. 看看今天教程: 看着几个字符在键盘的位置,直接就是三个圈圈,圆心的三个字符就是答案 非常简单! 答案就是KEY 看题解密就好了!! 根据提示Asp encode解 ...