本人水平有限,如有不足希望看到的人多多指教

ls

目录

1.1 实验环境介绍

1.2进入从库 Slave 1

1.3这里就假设Slave 1 为同步最快的,将其提升为主库

1.4简要说明(真对同步授权表主从结构)

1.5 从库一切完毕,修改hosts文件

1.6下面我们就再设置另一个slave 让他跟新的主库同步

1.7下面测试我们的成果

神奇的hosts解析

     可以写个shell脚本来实现下面的功能,这样能尽量减少网站停机时间

实验环境介绍

系统 Centos 5.5 32

数据库 Mysql 5.0.16

环境介绍

10.0.0.57   mysql主库

10.0.0.105  SLvae 1 模拟主库宕机后提升为Master

10.0.0.103  Slave 2 做从库

最近看群里人说

Mysql 主从同步 主库挂了怎么办?

1)网站测试环境

模拟用户访问网站

2)此刻我停掉主库模拟主机宕机

root@mysql zongjie 16:30:07 # ifconfig eth0

eth0      Link encap:Ethernet  HWaddr 00:0C:29:D6:F6:C7

inet addr:10.0.0.57  Bcast:10.0.0.255  Mask:255.255.255.0

inet6 addr: fe80::20c:29ff:fed6:f6c7/64 Scope:Link

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:37460 errors:0 dropped:0 overruns:0 frame:0

TX packets:38576 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:5513260 (5.2 MiB)  TX bytes:24159718 (23.0 MiB)

Interrupt:59 Base address:0x2000

root@mysql zongjie 16:30:14 # /etc/init.d/mysqld stop

Shutting down MySQL.....                                   [确定]

用户访问

1.2进入从库 Slave 1

①进入从库操作。建议不要立刻停掉从库,如果从主库复制来的sql语句并未完全执行完,此时stop slave 肯定会丢失数据

②提升主库的从库选择,选择与主库延迟最小的从库提为主库

可以查看

mysql> show processlist\G;

*************************** 1. row ***************************

Id: 1

User: system user

Host:

db: NULL

Command: Connect

Time: 30371

State: Reconnecting after a failed master event read

这个参数表示,正在等待重新连接主库

Info: NULL

*************************** 2. row ***************************

Id: 2

User: system user

Host:

db: NULL

Command: Connect

Time: -310998

State: Has read all relay log; waiting for the slave I/O thread to update it

这个 自然就是本地的sql 线程了,这里的意思是等待I/O进程更新。

Info: NULL

*************************** 3. row ***************************

Id: 4

User: rep

Host: 10.0.0.105:56720

db: NULL

Command: Binlog Dump

Time: 30320

State: Has sent all binlog to slave; waiting for binlog to be updated

因为这里我数据并不是很大,所以这里显示已经,主服务器已经把二进制文件的更新发到从服务器上了,

线程现在很空闲,意思就是本地已经保了主服务器上所有的Binlog了

这样,即使主库挂了,但此时从库上的数据是相对完整的。即使sql进程执行慢,也不用担心数据的大量丢失

Info: NULL

*************************** 4. row ***************************

Id: 6

User: root

Host: localhost

db: NULL

Command: Query

Time: 0

State: NULL

Info: show processlist

4 rows in set (0.00 sec)

ERROR:

No query specified

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Reconnecting after a failed master event read

Master_Host: 10.0.0.57

Master_User: rep

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000007

Read_Master_Log_Pos: 10552

Relay_Log_File: relay-bin.000019

Relay_Log_Pos: 10026

Relay_Master_Log_File: mysql-bin.000007

Slave_IO_Running: No

Slave_SQL_Running: Yes

Replicate_Do_DB:

Replicate_Ignore_DB: mysql

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: 10552

Relay_Log_Space: 10991

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: 2013

Last_IO_Error: error reconnecting to master 'rep@10.0.0.57:3306' - retry-time: 60  retries: 86400

Last_SQL_Errno: 0

Last_SQL_Error:

1 row in set (0.01 sec)

ERROR:

No query specified

#################################Slave 2 #####################

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Reconnecting after a failed master event read

Master_Host: 10.0.0.57

Master_User: rep

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000007

Read_Master_Log_Pos: 10552

Relay_Log_File: mysqld-relay-bin.000017

Relay_Log_Pos: 10018

Relay_Master_Log_File: mysql-bin.000007

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: 10552

Relay_Log_Space: 10018

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

1 row in set (0.00 sec)

可以发现 i/o 进程已经停止了

下面就开始进行操作

1.3这里就假设Slave 1 为同步最快的,将其提升为主库

①停掉slave

mysql> system ifconfig eth0

eth0      Link encap:Ethernet  HWaddr 00:0C:29:89:45:B9

inet addr:10.0.0.105  Bcast:255.255.255.255  Mask:255.255.255.0

inet6 addr: fe80::20c:29ff:fe89:45b9/64 Scope:Link

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:49614 errors:0 dropped:0 overruns:0 frame:0

TX packets:32316 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:17092794 (16.3 MiB)  TX bytes:5457395 (5.2 MiB)

Interrupt:59 Base address:0x2000

mysql> stop slave;

Query OK, 0 rows affected (0.00 sec)

②开启从库log-bin选项(从库不建议开启bin log文件)

[root@MYSQL 3307]# grep log-bin /data/3306/my.cnf

log-bin = /data/3306/mysql-bin

这里可能需要重启下从库

[root@MYSQL 3307]# /data/3306/mysql restart

Restarting MySQL...

Stoping MySQL...

Enter password:

Starting MySQL..

授权一个同步的用户

mysql> grant replication slave on *.* to 'slave'@'10.0.0.%' identified by '123';

Query OK, 0 rows affected (0.01 sec)

1.4简要说明(真对同步授权表主从结构)

###########简要说明###################

如果这里有同步主库的授权库,可以不用重新授权一个用户,

此时只需要在Slave 2 上进行如下设置即可

mysql> change master to MASTER_HOST='10.0.0.105',MASTER_PORT=3306;

ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first

mysql> stop slave ;

Query OK, 0 rows affected (0.09 sec)

mysql> change master to MASTER_HOST='10.0.0.105',MASTER_PORT=3306;

Query OK, 0 rows affected (0.01 sec)

mysql> start slave;

Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 10.0.0.105

Master_User: rep

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000008

Read_Master_Log_Pos: 3785

Relay_Log_File: mysqld-relay-bin.000009

Relay_Log_Pos: 3922

Relay_Master_Log_File: mysql-bin.000008

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

############End#######################

这里有些人说要执行 reset master,感觉没必要。毕竟一般的mysql主库机器性能相对slave比较好,这样等主库维护后,还是要用主库的

1.5从库一切完毕,下面就该在web server 上做了

这里我将hosts 先备份下 方便比较

[root@APACHE etc]# cp hosts hosts.xp

[root@APACHE etc]# diff hosts hosts.xp

7c7

< 10.0.0.105    master.mysql

---

> 10.0.0.57     master.mysql

这里只是修改下master.mysql 地址

本着实用的原则,应该与开发人员协商好,分发hosts 文件对运维来说很简单。对开发也方便,不用记IP地址。一举两得

此刻再次查看web

到这里已经能正常对外提供服务

1.6下面我们就再设置另一个slave 让他跟新的主库同步

mysql> change master to MASTER_HOST='10.0.0.105',MASTER_PORT=3306,MASTER_USER='slave',MASTER_PASSWORD='123';

ERROR 1198 (HY000): This operation cannot be performed with a running slave; run STOP SLAVE first

mysql> stop slave;

Query OK, 0 rows affected (0.00 sec)

mysql> change master to MASTER_HOST='10.0.0.105',MASTER_PORT=3306,MASTER_USER='slave',MASTER_PASSWORD='123';

Query OK, 0 rows affected (0.05 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: 10.0.0.105

Master_User: slave

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000008

Read_Master_Log_Pos: 3932

Relay_Log_File: mysqld-relay-bin.000009

Relay_Log_Pos: 4069

Relay_Master_Log_File: mysql-bin.000008

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

在设置Slave 2 的时候 ,可以不需要指定bin log 文件的位置和位置点,毕竟slave 1 slave 2 都跟MASTER 同步过,所以可以不用再次指定

1.7下面测试我们的成果

主库创建库操作

mysql> system ifconfig eth0

eth0      Link encap:Ethernet  HWaddr 00:0C:29:89:45:B9

inet addr:10.0.0.105  Bcast:255.255.255.255  Mask:255.255.255.0

inet6 addr: fe80::20c:29ff:fe89:45b9/64 Scope:Link

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:51542 errors:0 dropped:0 overruns:0 frame:0

TX packets:36032 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:17240206 (16.4 MiB)  TX bytes:10300211 (9.8 MiB)

Interrupt:59 Base address:0x2000

mysql> create database testzhucong;

Query OK, 1 row affected (0.00 sec)

从库验证结果

mysql> system ifconfig eth0

eth0      Link encap:Ethernet  HWaddr 00:0C:29:B9:80:C2

inet addr:10.0.0.103  Bcast:255.255.255.255  Mask:255.255.255.0

inet6 addr: fe80::20c:29ff:feb9:80c2/64 Scope:Link

UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

RX packets:42409 errors:0 dropped:0 overruns:0 frame:0

TX packets:28298 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000

RX bytes:44970006 (42.8 MiB)  TX bytes:2551998 (2.4 MiB)

Interrupt:59 Base address:0x2000

mysql> show databases ;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| ceshi              |

| hostceshi          |

| mysql              |

| test               |

| testzhucong        |

+--------------------+

6 rows in set (0.08 sec)

转载于:https://blog.51cto.com/8time/1047388

不一样的思路,hosts文件在Mysql主从中的应用相关推荐

  1. 将.frm、.myd和.myi文件导入mysql数据库中

    1.先建立一个想要的数据库 2.找到存放数据库文件夹 安装目录下搜索找到 my.ini 文件,然后在文件中搜索 datadir , datadir="D:\Documents and Set ...

  2. imac下修改本地hosts文件解决react项目中的跨域问题

    1.跨域问题:No 'Access-Control-Allow-Origin' header is present on the requested resource. 解决: 1)在本机命令行输入 ...

  3. scrapy实战----将数据存储到csv文件和MySQL数据库中

    本章将通过爬取51jobs求职网站中的python职位信息来实现不同方式的数据存储的需求. github地址--->源代码 我们先来看一下:51jobs网站 我们需要的数据有,职位名 公司名 工 ...

  4. mysql主从中异步和半同步的区别

    MySQL主从复制,默认是异步复制.异步复制,即master执行完事物并提交后,二进制日志记录完这些更新操作后,就又开始下一批事物.并不关心这些更新是否被复制到从上. 而半同步复制则相反,它需要等待至 ...

  5. Linux操作系统下/etc/hosts文件配置方法

    1.关于/etc/host,主机名和IP配置文件 Hosts - The static table lookup for host name(主机名查询静态表) hosts文件是Linux系统中一个负 ...

  6. 【转】Linux操作系统下/etc/hosts文件配置方法

    原文链接 http://my.oschina.net/liting/blog/387544 1.关于/etc/host,主机名和IP配置文件 Hosts - The static table look ...

  7. Linux下hosts文件详解

    1.主机名: 无论在局域网还是INTERNET上,每台主机都有一个IP地址,是为了区分此台主机和彼台主机,也就是说IP地址就是主机的门牌号. 公网:IP地址不方便记忆,所以又有了域名.域名只是在公网( ...

  8. excel数据库_将excel文件导入mysql数据库教程(PHP实现)

    点击蓝字关注我们!每天获取最新的编程小知识! 源 / php中文网      源 / www.php.cn 在这篇文章中,我将给大家介绍如何使用PHP将excel文件导入mysql数据库.有时候我们需 ...

  9. MySQL数据库中常见的日志文件汇总!

    一个优秀的Java后端开发工程师一定要掌握MySQL数据库,而日志文件记录了影响MySQL数据库的各种类型活动,因此也成为其必须要掌握的知识.今天千锋ava培训小编就给大家介绍MySQL数据库中常见的 ...

最新文章

  1. stelnet 简介 华为ssh的另一种称呼
  2. SQL Server2005的XML数据类型之基础篇 (2)
  3. Android通过cat /sys/kernel/debug/usb/devices获取USB信息
  4. 设备管理系统html,蓝色的远程监控设备系统后台管理界面html模板
  5. PMCAFF微课堂|诸葛理财联合创始人:互联网金融产品的那些坑
  6. 循环结构作业c语言,c语言循环结构练习题带答案
  7. JUnit简单使用教程
  8. 音频光端机的几个重要指标
  9. Min(BZOJ 1441)
  10. 控制输入框的:选择,复制,粘贴功能。
  11. 2021-06-26图层的概念
  12. 如何制作一个商城小程序?
  13. Ollydbg 1.10's plugin LsMem2CAry.dll
  14. start-stop-daemon 用法
  15. 请假时间计算方式java_java计算两段时间的重复天数
  16. DDS作业(作业3)
  17. 突破单IP频繁反爬虫限制的小技巧
  18. linux编译cu文件,Linux编译安装RTL8192CU芯片驱动
  19. asr标注工具_BLASR:PacBio数据比对工具
  20. Android studio 安装步骤

热门文章

  1. MySQL绿色版 官网下载+安装(win7)
  2. 限制网速 制造测试条件
  3. Linux应用:FTP
  4. springcloud(二):注册中心Eureka
  5. Using jQuery to add a dynamic “Back To Top” floating button with smooth scroll
  6. Windows 7里的计算器,中文版,给Vista和2008用吧
  7. N-甲基-N-亚硝基脲(MNU)与眼睛健康(思考中)
  8. 患者是否应该主动关注医疗知识
  9. wordpress主题部署到github pages(调研+具体操作-还没有完成)
  10. 2020Alibaba数学预选赛第2轮试题