huangfen 发表于 2012-7-31 14:26:07

MYSQL+heartbeat+mon双机互备

本帖最后由 huangfen 于 2012-7-31 14:32 编辑

文档说明
本文档,设计了一个MySQL主从复制,用mon对服务器的mysql服务进行监控,heartbeat提供对外的VIP,mysql从服务器通过获得主服务器mysql的日志,把数据同步到从服务器,当主服务器down机后,mon检测到主服务器down机后,会停掉主机的heartbeat服务,VIP和mon服务将会自己转移到从服务器,当主服务器处理好后,会通过从服务器的日志把数据同步到主服务器,待主服务器同步完数据后,可以手机把VIP和mon服务移回到主服务器。
网络拓朴

IP地址说明:
Master
eth0:172.20.67.13 eth1: 172.20.67.22;
Slave
eth0:172.20.67.46 eth1: 172.20.67.53;
VIP:172.20.67.23
服务器系统安装
CentOS4.6(完全安装)
主服务器mysql安装
# mv /etc/rc.d/init.d/mysqld /etc/rc.d/init.d/mysqld.bak
# mv /etc/my.cnf /etc/my.cnf.bak
# rm -rf /var/lib/mysql/
# vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=master
# service network restart
# su –
# cd /usr/local/src/
# tar zxvf mysql-5.0.51a.tar.gz
# cd mysql-5.0.51a
# ./configure --prefix=/usr/local/mysql --localstatedir=/var/lib/mysql --with-mysqld-user=mysql --without-debug --with-big-tables --with-charset=gbk --with-collation=gbk_chinese_ci --with-extra-charsets=all --with-pthread --enable-thread-safe-client --enable-assembler --without-isam --without-innodb --without-ndb-debug --with-mysqli && make && make install
# cd /usr/local/mysql/
# bin/mysql_install_db --user=mysql
#chown -R root:mysql .
# chown -R mysql /var/lib/mysql
# cp share/mysql/my-huge.cnf /etc/my.cnf
# cp share/mysql/mysql.server /etc/rc.d/init.d/mysqld
# chkconfig mysqld off
# chmod 755 /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# vi /root/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
         . ~/.bashrc
fi

# User specific environment and startup programs

PATH=/usr/local/mysql/bin: $ PATH: $ HOME/bin

export PATH
unset USERNAME
# su –
主服务器mysql配置
# service mysqld restart
# mysql
mysql> use mysql
mysql> update user set password=password('123123') where user='root';
mysql> grant replication slave on *.* to 'copy'@'%' identified by '123123';
mysql> flush privileges;
# vi /etc/my.cnf(增加以下内容)
master-host=slave
master-user=copy
master-password=123123
master-connect-retry=60
replicate-do-db=zds
log-slave-updates
binlog-do-db=zds
从服务器mysql安装
# mv /etc/rc.d/init.d/mysqld /etc/rc.d/init.d/mysqld.bak
# mv /etc/my.cnf /etc/my.cnf.bak
# rm -rf /var/lib/mysql/
# vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=slave
# service network restart
# hostname slave
# su –
# cd /usr/local/src/
# tar zxvf mysql-5.0.51a.tar.gz
# cd mysql-5.0.51a
# ./configure --prefix=/usr/local/mysql --localstatedir=/var/lib/mysql --with-mysqld-user=mysql --without-debug --with-big-tables --with-charset=gbk --with-collation=gbk_chinese_ci --with-extra-charsets=all --with-pthread --enable-thread-safe-client --enable-assembler --without-isam --without-innodb --without-ndb-debug --with-mysqli && make && make install
# cd /usr/local/mysql/
# bin/mysql_install_db --user=mysql
#chown -R root:mysql .
# chown -R mysql /var/lib/mysql
# cp share/mysql/my-huge.cnf /etc/my.cnf
# cp share/mysql/mysql.server /etc/rc.d/init.d/mysqld
# chkconfig mysqld off
# chmod 755 /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# vi /root/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
         . ~/.bashrc
fi

# User specific environment and startup programs

PATH=/usr/local/mysql/bin: $ PATH: $ HOME/bin

export PATH
unset USERNAME
# su –
从服务器mysql配置
# service mysqld start
# mysql
mysql> use mysql
mysql> update user set password=password('123123') where user='root';
mysql> grant replication slave on *.* to 'copy'@'%' identified by '123123';
mysql> flush privileges;
# vi /etc/my.cnf(增加以下内容)
master-host=master
master-user=copy
master-password=123123
report-host=slave
replicate-do-db=zds
log-slave-updates
binlog-do-db=zds
server-id       = 2(些处为修改)
修改两台服务器的hosts
# vi /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1               localhost.localdomain localhost
172.20.67.13    master
172.20.67.22    master
172.20.67.46    slave
172.20.67.53    slave
主服务器创建复制库并导出到从服务器以下步骤我省了
# mysql -p
mysql> create database zds;
mon安装(主从相同)
# rpm -ihv *
warning: mon-1.2.0-1.el4.rf.i386.rpm: V3 DSA signature: NOKEY, key ID 6b8d79e6
Preparing...                ###########################################
    1:perl-Time-Period       ########################################### [ 13%]
    2:perl-Net-SNPP          ########################################### [ 25%]
    3:perl-Math-TrulyRandom########################################### [ 38%]
    4:perl-Convert-BER       ########################################### [ 50%]
    5:perl-Mon               ########################################### [ 63%]
    6:perl-AOL-TOC         ########################################### [ 75%]
    7:mon                  ########################################### [ 88%]
    8:perl-Authen-PAM      ###########################################
# vi /etc/mon/mon.cf
### Extremely basic mon.cf file

### global options
cfbasedir   = /etc/mon
pidfile   = /var/run/mon.pid
statedir    = /var/lib/mon/state.d
logdir      = /var/lib/mon/log.d
dtlogfile   = /var/lib/mon/log.d/downtime.log
alertdir    = /usr/lib/mon/alert.d
mondir      = /usr/lib/mon/mon.d
maxprocs    = 20
histlength= 100
randstart   = 60s
authtype    = pam
userfile    = /etc/mon/userfile

### group definitions (hostnames or IP addresses)
hostgroup servers master(从机把master改为slave)

watch servers
   service mysql
         interval 10s
         monitor msql-mysql.monitor
         period wd {Mon-Sun}
               alert test.alert
      alertevery 600s

alertafter 3


### See /usr/doc for the original example...
# vi /usr/lib/mon/alert.d/test.alert
#!/bin/sh
#
#$ Id: test.alert,v 1.1.1.1 2004/06/09 05:18:07 trockij Exp$
echo "`date`$ *" >> /tmp/test.alert.log
service heartbeat stop
# chmod 777 /usr/lib/mon/alert.d/test.alert
# vi /usr/lib/mon/mon.d/msql-mysql.monitor(以下为修改的部份,本脚本来自mon的源码包)
    # $dbh = DBI->connect( "DBI: $ mode: $ database: $ host: $ options{port}",$ username,$ password);
$ dbh = DBI->connect( "DBI:mysql:mysql:slave: $ options{port}", root, 123123);
# chmod 777 /usr/lib/mon/mon.d/mysql.monitor
chkconfig mon off

heartbeat安装(主从相同)
yum install heartbeat
# cp /usr/share/doc/heartbeat-2.1.3/ha.cf /etc/ha.d/
# cp /usr/share/doc/heartbeat-2.1.3/authkeys /etc/ha.d/
# cp /usr/share/doc/heartbeat-2.1.3/haresources /etc/ha.d/
# vi /etc/ha.d/ha.cf
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility   local0
keepalive 2
deadtime 30
warntime 10
initdead 120
udpport 694
bcast   eth1
auto_failback off
node    master
node    slave
# vi /etc/ha.d/haresources
master 172.20.67.23 mon
# vi /etc/ha.d/authkeys
auth 1
1 crc
# chmod 600 /etc/ha.d/authkeys
# chkconfig heartbeat off
设置服务自动启动(主从相同)
# vi /etc/rc.local
service mysqld start
service heartbeat start




huangfen 发表于 2012-7-31 14:26:26

本帖最后由 huangfen 于 2012-7-31 14:32 编辑

copy来的,供大家参考


附件是操作步骤

huangfen 发表于 2012-7-31 14:44:02

抛砖引玉了

michaelyuan 发表于 2012-7-31 15:43:37

学习了;P:victory:

282615460 发表于 2012-8-1 18:35:11

本帖最后由 282615460 于 2012-8-1 18:38 编辑

学习学习看了说明还是有点不明白mysql放在windows下面不行吗?

htqt 发表于 2012-8-9 01:42:33

好像很强大,可是看不大懂,暂时也没硬件环境试
页: [1]
查看完整版本: MYSQL+heartbeat+mon双机互备