ホームページ >データベース >mysql チュートリアル >mysql+drbd+heartbeat高可用配置说明
环境描述: 系统版本: Red Hat Enterprise Linux Server release 5.5 (Tikanga) x86_64 2.6.18-164.el5 mysql 版本: mysql-5.1.49.tar.gz drbd 版本: drbd83-8.3.15-2.el5.centos.rpm heartbeat 版本: heartbeat.x86_64 0:2.1.3-3.el5.centos.rpm 主机名
环境描述:
系统版本:Red Hat Enterprise Linux Server release 5.5 (Tikanga)x86_64 2.6.18-164.el5
mysql版本:mysql-5.1.49.tar.gz
drbd版本:drbd83-8.3.15-2.el5.centos.rpm
heartbeat版本:heartbeat.x86_64 0:2.1.3-3.el5.centos.rpm
主机名 |
Eth0 |
Eth1 |
备注 |
ln-master |
10.10.206.193 |
192.168.1.10 |
主 |
ln-slave |
10.10.206.194 |
192.168.1.11 |
备 |
VIP:10.10.206.211
前期工作(两台都要改):
修改主机名:
[root@ln-master ~]# cat /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=ln-master
更改hosts文件(两台都要改)
[root@ln-master ~]# cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
10.10.206.193 ln-master
192.168.1.10 ln-master
10.10.206.194 ln-slave
192.168.1.11 ln-slave
确保ping主机名能通,例如ping ln-slave
优化开机启动项,开基础的四项即可
[root@ln-master ~]#
[root@ln-master ~]# for A in `chkconfig --list |grep 3:on|awk '{print $1}'`;do chkconfig $A off;done
[root@ln-master ~]# for B in sshd crond syslog network;do chkconfig $B on;done
[root@ln-master ~]# chkconfig --list |grep 3:on
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
syslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
重启
reboot
安装配置DRBD:
注意:事先配置好YUM源
1. 通过yum安装drbd(两台都要安装)
注:drbd的版本和系统内核的对应 不同的版本对应不同的内核可以去官网看看
[root@ln-master ~]#yum install drbd83* -y #安装drbd
[root@ln-master ~]#yum -y install kmod-drbd83* #安装内核模块
[root@ln-master ~]#modprobe drbd #加载drbd内核模块
[root@ln-master ~]# lsmod |grep drbd
drbd 273204 0 #看到这个,证明加载drbd内核模块了。
2. 修改drbd配置文件(两台主机配置一样)
[root@ln-master ~]# cat /etc/drbd.conf
#
# please have a a look at the example configuration file in
# /usr/share/doc/drbd83/drbd.conf
#
global{
usage-count no; #这个问你让不让官网统计
}
common {
syncer {rate 100M;} #传输速度
}
resource r0 {
protocol C; #传输协议(下面进行协议说明)
startup {
}
disk {
on-io-error detach;
}
net {
cram-hmac-alg "sha1"; #两台服务器通信间的算法
shared-secret "FooFunFactory"; #通信密码
# after-sb-0pri disconnect;
# after-sb-1pri disconnect;
# after-sb-2pri disconnect;
after-sb-0pri discard-younger-primary;
after-sb-1pri discard-secondary;
after-sb-2pri call-pri-lost-after-sb;
rr-conflict disconnect;
}
syncer {
rate 100M;
al-extents 257;
}
on ln-master{ #这里一定要是hosts定义的名称
device /dev/drbd0; #逻辑设备路径
disk /dev/sdb1; #真实设备路径
address 192.168.1.10:7789; #maser传输数据的IP地址
meta-disk internal;
}
on ln-slave{
device /dev/drbd0;
disk /dev/sdb1;
address 192.168.1.11:7789; #slave传输数据的IP地址
meta-disk internal;
}
}
3. DRBD一些说明
DRBD 协议说明:
A协议:数据一旦写入磁盘并发送到网络中就认为完成了写入操作
B协议:收到接收确认就认为完成了写入操作
C协议:收到写入确认就认为完成了写入操作
DRBD设备的三个进程:
每个drbd设备都有三个进程:
1) drbd0_worker是drbd0的主进程
2) drbd0_asender是primary上的drbd0的数据发送进程
3) drbd0_receiver是secondary上的drdb0的数据写入进程
DRBD几点注意的地方:
1) mount drbd设备以前必须把设备切换到primary状态
2) 两个节点中,同一时间只能有一台处于primary状态,另一台是secondary状态
3) 处于secondary状态的节点不可以挂载
4) 主备最好使用大小一样的分区。
DRBD使用时建议使用Innodb存储引擎,同时打开binlog日志,设置innodb_flush_log_at_commit=1。MyISAM存储引擎在DRBD上容易造成数据丢失,及文件损坏。
DRBD脑裂后处理:
在drdb1-master上
drbdadm secondary r0
drbdadm — –discard-my-data connect r0
drbdadm connect r0
在drbd2-slave上
drbdadm disconnect r0
drbdadm connect r0
4. 创建DRBD设备
我这里用的是虚拟机,我两台都添加了一块30G硬盘。
对新的磁盘分区格式化(两台都要操作):
[root@ln-master ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-652, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-652, default 652):
Using default value 652
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
创建drbd设备(两台都要执行)
[root@ln-slave ~]# drbdadm create-md r0
Writing meta data...
initializing activity log
NOT initialized bitmap
New drbd meta data block successfully created. #看到这些信息表示创建设备成功。
错误
Command 'drbdmeta 0 v08 /dev/sdb internal create-md' terminated with exit code 40
drbdadm create-md r0: exited with code 40
这是因为我们只把以前的分区删除了,并没有重新创建分区,创建分区即可
如果还出现这个错误提示运行
dd if=/dev/zero of=/dev/sdb1 bs=1M count=1
即可
启动drbd服务(两台都要启动)
[root@ln-master ~]# /etc/init.d/drbd start
Starting DRBD resources: [ d(r0) s(r0) n(r0) ]..........
[root@ln-master ~]# /etc/init.d/drbd status
drbd driver loaded OK; device status:
version: 8.3.15 (api:88/proto:86-97)
GIT-hash: 0ce4d235fc02b5c53c1c52c53433d11a694eab8c build by mockbuild@builder17.centos.org, 2013-03-27 16:04:08
m:res cs ro ds p mounted fstype
0:r0 Connected Secondary/Secondary Inconsistent/Inconsistent C
错误
Starting DRBD resources: Can not load the drbd module.
没有加载drbd内核模块,安装内核模块并加载即可。
6. 将master设置为主节点(只在master上执行)
在master主机执行命令 设置为主节点
[root@ln-master ~]# drbdadm -- --overwrite-data-of-peer primary r0 #设置为主节点
[root@ln-master ~]# mkfs.ext3 /dev/drbd0 #格式化
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
655360 inodes, 1309240 blocks
65462 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 33 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@ln-master /]# mkdir /data
[root@ln-master /]# mount /dev/drbd0 /data/ #挂载
[root@ln-master /]# df -h
文件系统 容量 已用可用已用% 挂载点
/dev/mapper/VolGroup00-LogVol00
45G 14G 30G 32% /
/dev/sda1 99M 24M 70M 26% /boot
tmpfs 3.9G 0 3.9G 0% /dev/shm
/dev/drbd0 30G 1.4G 27G 5% /data
[root@ln-master /]# drbd-overview
0:r0 Connected Primary/Secondary UpToDate/UpToDate C r-----
注:看到都是UpToDate了,证明两边已经同步完成了。
7. 测试
mater操作:
[root@ln-master /]# cd /data/
[root@ln-master data]# ll
total 16
drwx------ 2 root root 16384 Aug 6 10:39 lost+found
[root@ln-master data]# touch test #创建test文件
[root@ln-master data]# ll
total 16
drwx------ 2 root root 16384 Aug 6 10:39 lost+found
-rw-r--r-- 1 root root 0 Aug 6 12:35 test
[root@ln-master data]# cd ..
[root@ln-master /]# umount /data/
[root@ln-master /]# df -h
文件系统 容量 已用可用已用% 挂载点
/dev/mapper/VolGroup00-LogVol00
45G 14G 30G 32% /
/dev/sda1 99M 24M 70M 26% /boot
tmpfs 3.9G 0 3.9G 0% /dev/shm
[root@ln-master /]# drbdadm secondary r0 #将自己切换成从
[root@ln-master /]# drbd-overview #检查
0:r0 Connected Secondary/Secondary UpToDate/UpToDate C r-----
slave操作:
[root@ln-slave /]# drbd-overview
0:r0 Connected Secondary/Secondary UpToDate/UpToDate C r-----
[root@ln-slave /]# drbdadm primary r0 #将自己提升为主
[root@ln-slave /]# drbd-overview
0:r0 Connected Primary/Secondary UpToDate/UpToDate C r-----
[root@ln-slave /]# mount /dev/drbd0 /data/
[root@ln-slave /]# cd /data/
[root@ln-slave data]# ll
total 16
drwx------ 2 root root 16384 Aug 6 2014 lost+found
-rw-r--r-- 1 root root 0 Aug 6 2014 test
以上如果在slave端看到了test文件,证明数据已经同步了。
注:将master 还原成主,再做下面的操作。
安装配置mysql(两台都要装)
安装mysql:
#创建mysql用户
useradd mysql -s /sbin/nologin –M
id mysql
#安装
tar zxf mysql-5.1.49.tar.gz
cd mysql-5.1.49
#配置
./configure --prefix=/usr/local/mysql-5.1.49 --with-unix-socket-path=/usr/local/mysql-5.1.49/tmp/mysql.sock --localstatedir=/data --enable-assembler --with-charset=utf8 --with-collation=utf8_general_ci --with-plugins=innobase --enable-thread-safe-client --with-mysqld-user=mysql --with-big-tables --without-debug --with-pthread --enable-assembler --with-extra-charsets=complex --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=partition,innobase --with-mysqld-ldflags=-all-static --with-client-ldflags=-all-static
#拷贝启动脚本,配置文件
/bin/cp support-files/mysql.server /etc/init.d/mysqld
/bin/cp support-files/my-small.cnf /etc/my.cnf
chmod 700 /etc/init.d/mysqld
#做软连接
ln -s /usr/local/mysql-5.1.49/ /usr/local/mysql
#主库初始化
/application/tools/mysql-5.1.49/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/data --user=mysql #备库不需要初始化
mysql程序路径:/usr/local/mysql
mysql data目录:/data/mysql/data
my.cnf配置文件(两边相同),根据机器配置优化,以下配置仅供参考
[root@ln-master mysql]# cat /etc/my.cnf
[client]
port = 3306
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
sync_binlog = 0
binlog_format = ROW
skip-locking
skip-name-resolve
skip-host-cache
default-character-set=utf8
default-collation=utf8_general_ci
skip-character-set-client-handshake
max_allowed_packet = 16M
table_cache = 128
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 2M
default-storage-engine=INNODB
log-bin=mysql-bin
max_connections=5000
max_connect_errors=100000
log_slow_queries=slow.log
long_query_time=2
log_queries_not_using_indexes=0
#skip-federated
server-id= 10
table_lock_wait_timeout=180
innodb_lock_wait_timeout=180
innodb_data_file_path = ibdata1:1000M:autoextend
#innodb_buffer_pool_size = 1G
innodb_additional_mem_pool_size = 8M
innodb_log_file_size = 100M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 2
innodb_thread_concurrency=0
transaction-isolation=READ-COMMITTED
innodb_doublewrite=1
innodb_flush_method=O_DIRECT
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[isamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
安装配置heartbeat
1.添加主机路由
master:
route add -host 192.168.1.11 dev eth1
slave:
route add -host 192.168.1.10 dev eth1
添加到/etc/rc.local.
2.安装heartbeat
yum install -y heartbeat
3.配置heartbeat
heartbeat会有三个配置文件ha.cf,haresources,authkeys(本实验主备节点三个配置文件必须相同)
ha.cf主配置文件:
[root@ln-master ha.d]# cat ha.cf
logfile /var/log/ha-log #日志名字及存放位置
keepalive 2 #设定心跳检测时间2秒
deadtime 15 #死亡时间15秒,备用节点15秒没有检测到主节点心跳,确认对方故障
warntime 10 #警告次数
initdead 30 #守护进程启动30s后启动服务资源
udpport 694 #使用ucast或bcast的udp通讯端口,默认694
bcast eth1 #广播通讯接口,ucast得指定ip地址
auto_failback off #当主节点切换到备份节点后,主节点又恢复正常,此处定义不进行回切操作,因为回切一次,NFS和mysql等成本很高
#watchdog /dev/watchdog
node ln-master
node ln-slave
ping 10.10.206.1
authkeys验证文件:
[root@ln-master ha.d]# cat authkeys
auth 1 #使用crc验证方式,这种方式不需要秘钥,因此性能比较好,还有其他两种安全依次增高,性能依次降低
1 crc
[root@ln-master ha.d]# ll authkeys #必须为600权限
-rw------- 1 root root 31 08-12 17:49 authkeys
haresources配置文件:
[root@ln-master ha.d]# cat haresources
ln-master IPaddr::10.10.206.211 drbddisk::r0 Filesystem::/dev/drbd0::/data::ext3 mysqld
说明:
IPaddr::10.10.206.211 #VIP
drbddisk::r0 #启动drbd r0资源,相当于执行/etc/ha.d/resource.d/drbddisk r0 stop/start操作
Filesystem::/dev/drbd0::/data::ext3 #drbd分区挂载到/data目录,相当于执行/etc/ha.d/resource.d/Filesystem /dev/drbd0 /data ext3 stop/start
mysqld #启动mysql服务脚本,相当于/etc/init.d/mysqld stop/start
4.启动heartbeat(两边都要启动)
[root@ln-master ~]# /etc/init.d/heartbeat start
Starting High-Availability services:
2014/08/13_10:25:01 INFO: Resource is stopped
[确定]
[root@ln-master ~]# ip add
1: lo:
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0:
link/ether 00:50:56:97:39:7c brd ff:ff:ff:ff:ff:ff
inet 10.10.206.193/24 brd 10.10.206.255 scope global eth0
inet 10.10.206.211/24 brd 10.10.206.255 scope global secondary eth0:0 #VIP也起来了
inet6 fe80::250:56ff:fe97:397c/64 scope link
valid_lft forever preferred_lft forever
测试(启动heartbeat后)
master:
drbd自动成为了主,mysql也启动了。
root@ln-master ~]# ip add|grep "10.10"
inet 10.10.206.193/24 brd 10.10.206.255 scope global eth0
inet 10.10.206.211/24 brd 10.10.206.255 scope global secondary eth0:0
[root@ln-master ~]# drbd-overview
0:r0 Connected Primary/Secondary UpToDate/UpToDate C r----- /data ext3 30G 1.