我的编程空间,编程开发者的网络收藏夹
学习永远不晚

5.6 MySql主从自动切换脚本

短信预约 -IT技能 免费直播动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

5.6 MySql主从自动切换脚本

2015.07.17<br /> <br /> <br /> <div id="codeText" class="codeText"> <ol style="margin:0 1px 0 0;padding:5px 0pt;" start="1" class="dp-css none_number"> <li> <span style="color:#000000;">#<span style="color:#0000CC;">!</span><span style="color:#0000CC;">/</span>bin/bash<br /> </span> </li> <li> cat <span style="color:#0000CC;">&lt;</span><span style="color:#0000CC;">&lt;</span> README<br /> </li> <li> #####################################################################################################<br /> </li> <li> #Auther <span style="color:#0000CC;">:</span>zhanglin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #<br /> </li> <li> #Date <span style="color:#0000CC;">:</span> 2015<span style="color:#0000CC;">.</span>07<span style="color:#0000CC;">.</span>17 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#<br /> </li> <li> #Step 1<span style="color:#0000CC;">:</span> point the slave IP &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;                                      #<br /> </li> <li> #Step 2<span style="color:#0000CC;">:</span> check the master <span style="color:#0000FF;">and</span> slave information whether good <span style="color:#0000FF;">for</span> change   &nbsp; &nbsp;             #<br /> </li> <li> #Step 3<span style="color:#0000CC;">:</span> stop old slave<span style="color:#0000CC;">,</span>then get new master binlog <span style="color:#FF0000;">name</span> <span style="color:#0000FF;">and</span> postation<span style="color:#0000CC;">,</span>then execute change master to #<br /> </li> <li> #Step 4<span style="color:#0000CC;">:</span> start slave<span style="color:#0000CC;">,</span><span style="color:#0000FF;">and</span> show whether change successed<span style="color:#0000CC;">. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span>&nbsp;#<br /> </li> <li> #####################################################################################################<br /> </li> <li> README<br /> </li> <li> User<span style="color:#0000CC;">=</span>root<br /> </li> <li> PW<span style="color:#0000CC;">=</span>123456<br /> </li> <li> read <span style="color:#0000CC;">-</span>p <span style="color:#FF00FF;">"-- Please input the slave IP:"</span> Slave<br /> </li> <li> Master<span style="color:#0000CC;">=</span><span style="color:#0000CC;">$</span><span style="color:#0000CC;">(</span>mysql <span style="color:#0000CC;">-</span>u${User} <span style="color:#0000CC;">-</span>h${Slave} <span style="color:#0000CC;">-</span>p${PW} <span style="color:#0000CC;">-</span>e <span style="color:#FF00FF;">"show slave status \G;"</span><span style="color:#0000CC;">|</span>awk <span style="color:#FF00FF;">'/Master_Host/{print $2}'</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">[</span> <span style="color:#0000CC;">-</span>n <span style="color:#FF00FF;">"${Master}"</span> <span style="color:#0000CC;">]</span><br /> </li> <li> then<br /> </li> <li> <span style="color:#0000FF;">echo</span> <span style="color:#0000CC;">-</span>e <span style="color:#FF00FF;">"--Master IP:${Master},Slave IP:${Slave}"</span><br /> </li> <li> M<span style="color:#0000CC;">=</span><span style="color:#0000CC;">$</span><span style="color:#0000CC;">(</span>mysql <span style="color:#0000CC;">-</span>u${User} <span style="color:#0000CC;">-</span>h${Master} <span style="color:#0000CC;">-</span>p${PW} <span style="color:#0000CC;">-</span>e <span style="color:#FF00FF;">"show master status;"</span><span style="color:#0000CC;">|</span>awk <span style="color:#FF00FF;">'NR==2{print $2}'</span><span style="color:#0000CC;">)</span><br /> </li> <li> S<span style="color:#0000CC;">=</span><span style="color:#0000CC;">$</span><span style="color:#0000CC;">(</span>mysql <span style="color:#0000CC;">-</span>u${User} <span style="color:#0000CC;">-</span>h${Slave} <span style="color:#0000CC;">-</span>p${PW} <span style="color:#0000CC;">-</span>e <span style="color:#FF00FF;">"show slave status \G;"</span><span style="color:#0000CC;">|</span>awk <span style="color:#FF00FF;">'/Read_Master_Log_Pos/{print $2}'</span><span style="color:#0000CC;">)</span><br /> </li> <li> <span style="color:#0000FF;">echo</span> <span style="color:#0000CC;">-</span>e <span style="color:#FF00FF;">"-- master pos:${M};slave pos is:${S}"</span><br /> </li> <li> &nbsp;<span style="color:#0000FF;">else</span><br /> </li> <li> <span style="color:#0000FF;">echo</span> <span style="color:#FF00FF;">"-- Slave IP input wrong,please input again ! "</span><br /> </li> <li> exit 1<br /> </li> <li> &nbsp;fi<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">[</span> <span style="color:#FF00FF;">"${M}"</span> <span style="color:#0000CC;">-</span><span style="color:#0000FF;">eq</span> <span style="color:#FF00FF;">"${S}"</span> <span style="color:#0000CC;">]</span><br /> </li> <li> then<br /> </li> <li> &nbsp;read <span style="color:#0000CC;">-</span>p <span style="color:#FF00FF;">"-- Master-Slave is accordance,input Yes to start changing:"</span> var<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case <span style="color:#FF00FF;">"$var"</span> <span style="color:#0000FF;">in</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000CC;">[</span>Yy<span style="color:#0000CC;">]</span><span style="color:#FF0000;">es</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mysql <span style="color:#0000CC;">-</span>u${User} <span style="color:#0000CC;">-</span>h${Slave} <span style="color:#0000CC;">-</span>p${PW} <span style="color:#0000CC;">-</span>e <span style="color:#FF00FF;">"stop slave;reset slave;change master to master_host='';"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pos<span style="color:#0000CC;">=</span><span style="color:#0000CC;">$</span><span style="color:#0000CC;">(</span>mysql <span style="color:#0000CC;">-</span>u${User} <span style="color:#0000CC;">-</span>h${Slave} <span style="color:#0000CC;">-</span>p${PW} <span style="color:#0000CC;">-</span>e <span style="color:#FF00FF;">"show master status;"</span><span style="color:#0000CC;">|</span>awk <span style="color:#FF00FF;">'NR==2{print $2}'</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File<span style="color:#0000CC;">=</span><span style="color:#0000CC;">$</span><span style="color:#0000CC;">(</span>mysql <span style="color:#0000CC;">-</span>u${User} <span style="color:#0000CC;">-</span>h${Slave} <span style="color:#0000CC;">-</span>p${PW} <span style="color:#0000CC;">-</span>e <span style="color:#FF00FF;">"show master status;"</span><span style="color:#0000CC;">|</span>awk <span style="color:#FF00FF;">'NR==2{print $1}'</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mysql <span style="color:#0000CC;">-</span>u${User} <span style="color:#0000CC;">-</span>h${Master} <span style="color:#0000CC;">-</span>p${PW} <span style="color:#0000CC;">-</span>e <span style="color:#FF00FF;">"stop slave;<br /> </span> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;change master to master_user='ideal',master_host='${Slave}',master_password='123456',master_log_file='${File}',master_log_pos=${Pos};<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start slave;"<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000CC;">;</span><span style="color:#0000CC;">;</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000CC;">*</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">echo</span> <span style="color:#FF00FF;">"-- error input .... exit!"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000CC;">;</span><span style="color:#0000CC;">;</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;esac<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">echo</span> <span style="color:#FF00FF;">"-- changing, please wait 3s...."</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#FF0000;">sleep</span> 3<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">echo</span> <span style="color:#FF00FF;">"-- change successfull,the new master is:${Slave},new slave is ${Master}"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SlaveIOStatus<span style="color:#0000CC;">=</span><span style="color:#0000CC;">$</span><span style="color:#0000CC;">(</span>mysql <span style="color:#0000CC;">-</span>u${User} <span style="color:#0000CC;">-</span>h${Master} <span style="color:#0000CC;">-</span>p${PW} <span style="color:#0000CC;">-</span>e <span style="color:#FF00FF;">"show slave status \G;"</span><span style="color:#0000CC;">|</span>awk <span style="color:#FF00FF;">'/Slave_IO_Running/{print $2}'</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SlaveSQLStatus<span style="color:#0000CC;">=</span><span style="color:#0000CC;">$</span><span style="color:#0000CC;">(</span>mysql <span style="color:#0000CC;">-</span>u${User} <span style="color:#0000CC;">-</span>h${Master} <span style="color:#0000CC;">-</span>p${PW} <span style="color:#0000CC;">-</span>e <span style="color:#FF00FF;">"show slave status \G;"</span><span style="color:#0000CC;">|</span>awk <span style="color:#FF00FF;">'/Slave_SQL_Running/{print $2}'</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf <span style="color:#FF00FF;">"The new master is: \n Slave_IO_Running=${SlaveIOStatus}\n"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf <span style="color:#FF00FF;">"Slave_SQL_Running=${SlaveSQLStatus}\n"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">else</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">echo</span> <span style="color:#FF00FF;">"node change failed please change again ! "</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit 1<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fi </li> </ol> </div> <br /> <br />

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

5.6 MySql主从自动切换脚本

下载Word文档到电脑,方便收藏和打印~

下载Word文档

猜你喜欢

mysql主备自动切换如何设置

要设置MySQL主备自动切换,可以使用MySQL Replication来实现主备复制和故障切换。以下是设置主备自动切换的步骤:配置主备复制:在主服务器上启用二进制日志,并在备服务器上启用复制功能。确保主服务器和备服务器之间可以互相通信,并
mysql主备自动切换如何设置
2024-04-09

mysql主备自动切换怎么实现

要实现MySQL主备自动切换,可以使用MySQL Replication和MySQL Cluster来实现高可用性。以下是一种实现方法:部署MySQL Replication:在MySQL主服务器和备用服务器之间设置主从复制,确保备用服务
mysql主备自动切换怎么实现
2024-04-09

怎么用shell脚本实现自动切换内网和外网

小编给大家分享一下怎么用shell脚本实现自动切换内网和外网,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!首先说明下我们的配置文件,都是类似格式的,假如内网是19
2023-06-09

用shell脚本实现自动切换内网和外网实现高可用

首先说明下我们的配置文件,都是类似格式的,假如内网是192.168.0.3,外网是123.123.123.123,配置文件如下: $db['salver']['hostname'] = '192.168.0.3:3306';//$db['s
2022-06-04

从mysql数据库读取数据表的信息,自动创建模型shell脚本

################################# # 从数据库读取数据表,                # # 并使用php think 命令批量创建模型    # ################################# ###
从mysql数据库读取数据表的信息,自动创建模型shell脚本
2015-07-15

编程热搜

目录