技术开发 频道

MYSQL主从复制高可用实施详解

  三、安装配置MON相关文件 仅仅在主机上安装,从机无需安装

# tar zxvf Time-HiRes-01.20.tar.gz
# cd Time
-HiRes-01.20
# perl Makefile.PL
# make
# make install
# tar zxvf Period
-1.20.tar.gz
# cd Period
-1.20
# perl Makefile.PL
# make
# make install
# tar zxvf
Convert-BER-1.3101.tar.gz
# cd
Convert-BER-1.3101
# perl Makefile.PL
# make
# make install
# tar zxvf Mon
-0.11.tar.gz
# cd Mon
-0.11
# perl Makefile.PL
# make
# make install
# tar zxvf mon
-0.99.3-47.tar.gz -C /usr/lib/
# cd
/usr/lib/
# mv mon
-0.99.3-47 mon
# cd mon
# ln
-s /usr/lib/mon/etc/ /etc/mon

  mon.cf配置

  vi /etc/mon/mon.cf 添加如下:

#
# Simplified cluster "mon.cf" configuration
file
#
alertdir        
= /usr/lib/mon/alert.d
mondir          
= /usr/lib/mon/mon.d
statedir        
= /usr/lib/mon/state.d
logdir          
= /var/log/mon/logs
histlength      
= 500
dtlogging      
= yes
dtlogfile      
= /var/log/mon/logs/dtlog

hostgroup master
10.10.10.100   #主机名和虚拟IP

watch master                            #监控的主机
    service mysqld                     #监控MYSQL服务
        interval 5s
        monitor mysql.monitor             #负责监控MYSQL服务的文件
        period wd {Mon
-Sun}
        alert bring
-ha-down.alert           #负责停止HEARTBEAT的文件
            alert mail.alert fire9dingh
@gmail.com    #发送电邮的参数
            upalert mail.alert fire9dingh
@gmail.com
            alertevery 600s
            alertafter
3
# cd /usr/lib/mon
# mv mon.d
/msql-mysql.monitor mon.d/mysql.monitor
vi
/usr/lib/mon/mon.d/mysql.monitor 显示如下:
#!
/usr/bin/perl
#
# $Id: msql
-mysql.monitor 1.5 Thu, 21 Aug 2003 10:57:47 -0400 trockij $
#
# arguments:
#
#
[--mode [msql|mysql]] --username=username --password=password
#     --database=database --port=#
#    hostname
#
# a monitor
to determine if a mSQL or MySQL database server is operational
#
# Rather than
use tcp.monitor to ensure that your SQL server is responding
#
on the proper port, this attempts to connect to and list the databases
#
on a given database server.
#
# The
single argument, --mode [msql|mysql] is inferred from the script name
# if it is named mysql.monitor or msql.monitor.  Thus, the following two are
# equivalent:
#
# ln msql
-mysql.monitor msql.monitor
# ln msql
-mysql.monitor mysql.monitor
# msql.monitor hostname
# mysql.monitor hostname
#
#
and
#
# msql
-mysql.monitor --mode msql hostname
# msql-mysql.monitor --mode mysql hostname
#
#
use the syntax that you feel more comfortable with.
#
# This monitor requires the perl5 DBI, DBD::mSQL
and DBD::mysql modules,
# available
from CPAN (http://www.cpan.org)
#
#    Copyright (C)
1998, ACC TelEnterprises
#    Written
by James FitzGibbon <james@ican.net>
#
#    This program
is free software; you can redistribute it and/or modify
#    it under the terms
of the GNU General Public License as published by
#    the Free Software Foundation; either version
2 of the License, or
#    (at your
option) any later version.
#
#    This program
is distributed in the hope that it will be useful,
#    but WITHOUT
ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General
Public License for more details.
#
#    You should have received a copy
of the GNU General Public License
#    along
with this program; if not, write to the Free Software
#    Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

use DBI;
use Getopt::Long;

my
@details=();
my
@failures=();

GetOptions( \
%options, "mode=s", "port=i", "username=s", "password=s", "database=s" );

# uncomment these two lines
and provide suitable information if you don't
# want to pass sensitive information on the command line
#$options{username} ||= "username";
#$options{password} ||= "password";

if( $0 =~ m/\/msql\.monitor$/ || $options{mode} =~ m/msql/i ) {
    $mode = "mSQL";
    $options{port} = 1114 if ! $options{port};
} elsif( $0 =~ m/\/mysql\.monitor/ || $options{mode} =~ m/mysql/i) {
    $mode = "mysql";
    $options{port} = 3306 if ! $options{port};
} else {
    print "invalid mode $mode!\n";
    exit 1;
}

for $host( @ARGV ) {
    my( $dbh ) = DBI->connect( "DBI:mysql:database=fire9;host=localhost","root","123456",{ 'PrintError' => 1  } ); #仅仅需要修改这一行,改成相应的数据库名、存在的本地用户和密码,host等于的就是localhost. 主从的这个连接信息必须一致,我这里测试用了ROOT帐号
    if( ! $dbh ) {
        push( @failures, $host);
            push( @details, "$host: Could not connect to $mode server on $options{port}: " . $DBI::errstr . "\n");
        next;
    }
    @tables = $dbh->tables();
    if( $#tables < 0 ) {
        push( @failures, $host);
        push( @details, "$host: No tables found for database $options{database}\n");
    }
    $dbh->disconnect();
}

if (@failures)
{
    print join (" ", sort @failures), "\n";
    print sort @details if (scalar @details > 0);

    exit 1;

}

else
{
    exit 0;
}
# chmod 755 mon.d/mysql.monitor


# vi /usr/lib/mon/alert.d/bring-ha-down.alert 添加如下一行
/etc/rc.d/init.d/heartbeat stop
#chmod 755 /usr/lib/mon/alert.d/bring-ha-down.alert

#vi /etc/rc.d/rc.local  #添加MON自启动
/usr/lib/mon/mon -f -c /usr/lib/mon/etc/mon.cf

  需要检查文件权限

  bring-ha-down.alert # chmod 755

  mysql.monitor # chmod 755

  authkeys # chmod 600

  都配置完成就重新启动一下服务器吧。所有的配置都会自动执行的。启动服务器顺序,先启动主服务器再启动从服务器。

  通过tail /var/log/messages和tail /var/log/ha-log 来查看是否运行正常,ps –ef也可以看到进程的状态,如果主节点MYSQL服务停止就会发送EMAIL到你的邮箱里面。

0
相关文章