首页 > 代码库 > mysql+drbd+heartbeat+主从
mysql+drbd+heartbeat+主从
系统介绍:mysql+drbd+heartbeat+主从
mysql_drbdA eth0:192.168.1.233 eth1:172.20.27.10
mysql_drbdB eth0:192.168.1.234 eth1:172.20.27.20
VIP=192.168.1.235
mysql_slaveA eth0:192.168.1.232 eth1:172.20.27.30
一:DRBD安装
1.在mysql_drbdA和mysql_drbdB上面同时安装下面软件
1.1修改分别主机名为mysql_drbdA和mysql_drbdB
vi /etc/hosts增加以下内容
192.168.1.233 mysql_drbdA
192.168.1.234 mysql_drbdB
1.2安装
tar zxvf drbd-8.3.5.tar.gz
cd drbd-8.3.5
make
make install
chkconfig --add drbd
chkconfig --level 35 drbd off
1.3加载DRBD模块
modprobe drbd
lsmod |grep drbd
1.4配置/etc/drbd.conf
rm -rf /etc/drbd.conf
vi /etc/drbd.conf
global { usage-count yes; }
common { syncer { rate 100M; } }
resource r0 {
protocol C;
startup {
wfc-timeout 0;
degr-wfc-timeout 120;
}
disk {
on-io-error detach;
}
net {
cram-hmac-alg sha1;
shared-secret "FooFunFactory";
}
on mysql_drbdA {
device /dev/drbd0;
disk /dev/sdb1;
address 192.168.1.233:7898;
meta-disk internal;
}
on mysql_drbdB {
device /dev/drbd0;
disk /dev/sdb1;
address 192.168.1.234:7898;
meta-disk internal;
}
}
1.5创建一块虚拟硬盘为/dev/sdb的10G空间
fdisk /dev/sdb(进去以后输入n,然后输入p,然后输入1,然后回车,然后输入+10G,最后输入w)
mkfs.ext3 /dev/sdb1或者 dd if=/dev/zero bs=1M count=1 of=/dev/sdb1
mkdir /data/mysql
1.6在mysql_drbdA和mysql_drbdB上面同时启动及使用
1.6.1建立md设备
drbdadm create-md r0
1.6.2启动drbd服务程序
/etc/init.d/drbd start
1.6.3确认drbd状态
cat /proc/drbd
version: 8.3.5 (api:88/proto:86-91)
GIT-hash: ded8cdf09b0efa1460e8ce7a72327c60ff2210fb build by root@localhost.localdomain, 2012-06-08 17:47:52
0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r----
ns:1959800 nr:0 dw:0 dr:1959800 al:0 bm:120 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
再次确认backup的drbd状态
cat /proc/drbd
GIT-hash: ded8cdf09b0efa1460e8ce7a72327c60ff2210fb build by root@backup, 2012-06-08 18:10:58
0: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r----
ns:0 nr:1959800 dw:1959800 dr:0 al:0 bm:120 lo:0 pe:0 ua:0 ap:0 ep:1 wo:b oos:0
2.在mysql_drbdA上面验证磁盘镜像结果
初始化Primary机
第一次的话要使用
drbdsetup /dev/drbd0 primary -o
以后可以使用drbdadm primary all命令。
再次确认master的drbd状态
将新创建的设备格式化并且挂载:
mkfs.ext3 /dev/drbd0 //破坏文件系统:dd if=/dev/zero bs=1M of=/dev/sdb1;sync
mount /dev/drbd0 /data/
将主服务器降为从:
先卸载设备:
umount /data/
drbdadm secondary all
3.解决drbd脑裂问题
3.1在从节点mysql_drbdB上面执行
/etc/init.d/heartbeat stop
drbdadm -- --discard-my-data connect r0
3.2在主节点mysql_drbdA上面执行
/etc/init.d/heartbeat stop
drbdadm disconnect r0
drbdadm connect r0
drbdadm primary r0
二.安装mysql数据库
2.1在mysql_drbdA安装mysql数据库
mount /dev/drbd0 /data/
chown -R mysql.mysql /data/mysql
yum -y install ncurses ncurses-devel
groupadd mysql
useradd -g mysql -s /sbin/nologin mysql
cd /root/soft
tar zxvf mysql-5.1.41.tar.gz
chown -R mysql:mysql mysql-5.1.41
cd mysql-5.1.41
./configure \
--prefix=/usr/local/mysql \
--localstatedir=/data/mysql \
--with-unix-socket-path=/data/mysql/mysql.sock \
--with-plugins=innobase,partition,myisam,heap \
--enable-assembler \
--with-charset=utf8 \
--enable-thread-safe-client \
--with-client-ldflags=-all-static \
--with-mysqld-ldflags=-all-static \
--enable-static=yes \
--with-big-tables
make
make install
cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
sed -i ‘/myisam_/ a\ pid-file = /data/mysql/mysql.pid‘ /etc/my.cnf
sed -i ‘/pid-file/ a\ datadir = /data/mysql‘ /etc/my.cnf
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /data/mysql
/usr/local/mysql/bin/mysql_install_db \
--basedir=/usr/local/mysql \
--defaults-file=/etc/my.cnf \
--skip-external-locking \
--user=mysql
/usr/local/mysql/bin/mysqld_safe \
--defaults-file=/etc/my.cnf \
--user=mysql &
cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysqld
chmod 700 /etc/init.d/mysqld
ln -s /usr/local/mysql/bin/mysql /usr/bin/
ln -s /usr/local/mysql/bin/mysqldump /usr/bin/
service mysqld start
chkconfig mysqld off
2.2在mysql_drbdB安装mysql数据库
mount /dev/drbd0 /data/
yum -y install ncurses ncurses-devel
groupadd mysql
useradd -g mysql -s /sbin/nologin mysql
cd /root/soft
tar zxvf mysql-5.1.41.tar.gz
chown -R mysql:mysql mysql-5.1.41
cd mysql-5.1.41
./configure \
--prefix=/usr/local/mysql \
--localstatedir=/data/mysql \
--with-unix-socket-path=/data/mysql/mysql.sock \
--with-plugins=innobase,partition,myisam,heap \
--enable-assembler \
--with-charset=utf8 \
--enable-thread-safe-client \
--with-client-ldflags=-all-static \
--with-mysqld-ldflags=-all-static \
--enable-static=yes \
--with-big-tables
make
make install
cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
sed -i ‘/myisam_/ a\ pid-file = /data/mysql/mysql.pid‘ /etc/my.cnf
sed -i ‘/pid-file/ a\ datadir = /data/mysql‘ /etc/my.cnf
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /data/mysql
cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysqld
chmod 700 /etc/init.d/mysqld
ln -s /usr/local/mysql/bin/mysql /usr/bin/
ln -s /usr/local/mysql/bin/mysqldump /usr/bin/
service mysqld start
chkconfig mysqld off
三:heartbeat安装
3.1.在mysql_drbdA和mysql_drbdB安装libnet和heartbeat
tar zxvf libnet-1.1.6.tar.gz
cd libnet-1.1.6
./configure
make && make install
3.2.添加用户
groupadd haclient
useradd -g haclient hacluster
3.3.heartbeat编译
tar zxvf heartbeat-2.1.3.tar.gz
cd heartbeat-2.1.3
./ConfigureMe configure
make && make install
3.4.配置heartbeat
cd /etc/ha.d/
vi ha.cf
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 15
warntime 5
initdead 120
udpport 694
bcast eth0
auto_failback off
node mysql_drbdA
node mysql_drbdB
ucast eth0 192.168.1.234 #(对端服务器这里配置192.168.1.233 )
respawn hacluster /usr/lib64/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster
hopfudge
vi authkeys
auth 3
3 md5 Hello!
chmod 600 authkeys
cp /etc/init.d/mysqld /etc/ha.d/resource.d/
vi haresources
mysql_drbdA drbddisk Filesystem::/dev/drbd0::/data::ext3 mysqld 192.168.1.235 (对端服务器这里写mysql_drbdB)
/etc/init.d/heartbeat start
3.5在mysql_drbdA重启服务器需执行下面命令
modprobe drbd
service drbd start
drbdadm primary all
mount /dev/drbd0 /data/
service mysqld restart
/etc/init.d/heartbeat start
3.6在mysql_drbdB重启服务器需执行下面命令
modprobe drbd
service drbd start
/etc/init.d/heartbeat start
四:从向VIP数据同步
4.1在mysql_drbdA和mysql_drbdB配置
1)、确保/etc/my.cnf中有如下参数,没有的话需手工添加,并重启mysql服务。
[mysqld]
log-bin=mysql-bin #启动二进制文件
expire_logs_days = 20 #保留20的天的二进制日志
server-id=1 #服务器ID
binlog-ignore-db =mysql #不需要备份的数据库,多个写多行
binlog-ignore-db =test
binlog-ignore-db =information_schema
2)、登录mysql,在mysql中添加一个backup的账号,并授权给从服务器。
mysql -uroot -p123
grant replication slave on *.* to ‘backup‘@‘192.168.1.232‘ identified by ‘backup‘; 创建backup用户,并授权给192.168.1.232使用。
flush privileges;
quit
4.2在mysql_slaveA从服务器上操作:
1)、确保/etc/my.cnf中有log-bin=mysql-bin和server-id=1参数,并把server-id=1修改为server-id=2。修改之后如下所示:
[mysqld]
log-bin=mysql-bin #这个参数可以不用,因为他不需要双向同步
binlog_format=mixed
server-id=2
master-host = 192.168.1.235
master-user = backup
master-password = backup
master-port = 3306
master-connect-retry = 120
read-only = 1
relay-log-purge = 1
replicate-ignore-db = mysql #不需要同步数据库,多个写多行
replicate-ignore-db = test
replicate-ignore-db = information_schema
slave-skip-errors = all
2)、重启mysql服务。
service mysqld restart
3)、登录mysql,执行如下语句
mysql -uroot -p123
slave stop;
change master to
master_host=‘192.168.1.235‘,master_user=‘backup‘,master_password=‘backup‘,master_log_file=‘mysql-bin.000059‘,master_log_pos=106;
4)、启动slave同步。
mysql> start slave;
5)、检查主从同步,如果您看到Slave_IO_Running和Slave_SQL_Running均为Yes,则主从复制连接正常。
mysql> show slave status\G
验证配置是否正常,mysql主从能否正常复制。
本文出自 “成都@阿状” 博客,请务必保留此出处http://azhuang.blog.51cto.com/9176790/1440171
mysql+drbd+heartbeat+主从