您好,登录后才能下订单哦!
一、准备工作
系统:Centos6.5
两台主机需要相互域名解析
主节点(Primary Node) 次节点(Secondary Node) 主机名 ser5.hyzc.com ser6.hyzc.com IP地址 192.168.2.10 192.168.2.11
1.安装DRBD
参考博客安装 http://pengjc.blog.51cto.com/9255463/1835186
2.安装heartbeat与mysql
安装epel扩展源:
#yum -y install epel-release
两个机器都安装heartbeat
#yum -y install heartbeat*
查看heartbeat的配置文件ha.cf
在启用Heartbeat之前,安装后要配置三个文件(如没有可手动建立):ha.cf、haresources、authkeys。这三个配置文件需要在/etc/ha.d目录下面,但是默认是没有这三个文件的,可以到官网上下这三个文件,也可以在源码包里找这三个文件,在源码目录下的DOC子目录里。
查看heartbeat的配置文件ha.cf
[root@ser5 ~]# grep -v "^#" /etc/ha.d/ha.cf
debugfile /var/log/ha-debug #错误的日志 logfile /var/log/ha-log #日志 logfacility local0 #这个是设置heartbeat的日志,这里是用的系统日志 keepalive 2 #心跳的频率 deadtime 10 #死亡时间,如果其他节点10s回应,则认为死亡 warntime 5 #如果死亡之后,5s还没有连接则把警告信息写入日志里 initdead 120 #在其他节点死掉之后,系统启动前需要等待的时间,一般为deadtime的两倍 udpport 694 #用udp协议的694端口通信 ucast eth0 192.168.2.11 #另外一个节点的ip auto_failback on #设置当死亡节点恢复正常之后是否重新启用;容易发生数据不一致的情况,必须项,不然后面hb_standby命令无法使用; node ser5.hyzc.com #节点名(通过uname -n查询) node ser6.hyzc.com #节点名(通过uname -n查询) #ping 10.1.88.254 #ping网关查看网络情况(当网络或者heartbeat失效是使用) respawn hacluster /usr/lib64/heartbeat/ipfail #这里是配置ip绑定和切换的功能, ipfail就是控制ip切换的程序根据系统64位配置lib64,32位配置为lib auto_failback on # 自动切换(主节点恢复后会自动切换回来)
2、设置节点之间的通信密钥
[root@ser5 ~]# grep -v "^#" /etc/ha.d/authkeys
auth 1
1 crc
[root@ser5 ~]# chmod 600 authkeys
3、使用heartbeat的haresources来定义资源
[root@ser5 ~]# mkdir /data
[root@ser5 ~]# grep -v "^#" /etc/ha.d/haresources
ser5.hyzc.com IPaddr::192.168.2.15/24/eth0:1 drbddisk::r0 Filesystem::/dev/drbd0::/data::ext4
解释:
ser5.hyzc.com 主节点的主机名
IPaddr::192.168.2.15/24/eth0 设置虚拟IP
drbddisk::r0 管理资源r0
Filesystem::/dev/drbd0::/data::ext4 执行umount和mount操作
次节点操作:
将ha.cf中的192.168.2.11改成192.168.2.10
启动heartbeat 2.11和2.10机器
# service heartbeat start
从结果可以看出,VIP已经出现
[root@ser5 ha.d]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:fe:2d:ab brd ff:ff:ff:ff:ff:ff
inet 192.168.2.10/24 brd 192.168.2.255 scope global eth0
inet 192.168.2.15/24 brd 192.168.2.255 scope global secondary eth0:1
inet6 fe80::20c:29ff:fefe:2dab/64 scope link
valid_lft forever preferred_lft forever
停止ser5的heartbeat服务或将网线断掉,同时监控ser6的DRBD状态,
ser6操作:
watch -n 1 /etc/init.d/drbd status
如果一切正常,可以看到状态在不断变化。
4) 恢复ser5的heartbeat服务或将网线接上,同时监控ser6的DRBD状态,如果正常ser5又变为主节点(auto_failback on 决定)了。
mysql的安装
注:DMYSQL_DATADIR=/data/mysql/data/
mysql数据目录的安装位置在drbd上面
# mkdir –pv /data/mysql/data/
# chown -R mysql.mysql /data
源码包安装(生产环境使用最新稳定版的源码安装)
1.先安装编译工具cmake
[root@ser5 Desktop]# rpm -qa gcc gcc-c++ make ncurses-devel cmake openssl-devel openss
[root@ser5 Desktop]# yum -y install gcc-c++ ncurses-devel cmake openssl-devel openss
[root@ser5]# tar -zxvf cmake-2.8.10.2.tar.gz
[root@ser5]# cd cmake-2.8.10.2
[root@ser5 cmake-2.8.10.2]# ./bootstrap --prefix=/usr/local/cmake //安装程序
[root@ser5 cmake-2.8.10.2]#make && make install
[root@ser5 cmake-2.8.10.2]# /usr/local/cmake/bin/cmake --version //验证是否已安装
cmake version 2.8.10.2
2.安装mysql
1)检查是否安装mysql,有则执行下边程序
[root@ser5 cmake-2.8.10.2]# rpm -q mysql-server
[root@ser5 cmake-2.8.10.2]# service mysqld stop
[root@ser5 cmake-2.8.10.2]# chkconfig mysqld stop
[root@ser5 cmake-2.8.10.2]# rm -rf /etc/my.cnf
[root@ser5 cmake-2.8.10.2]# rpm -e --nodeps mysql-server
2)添加数据库管理用户
[root@ser5 cmake-2.8.10.2]# useradd -s /sbin/nologin -M mysql //添加数据库管理用户
[root@ser5 cmake-2.8.10.2]# grep mysql /etc/passwd
mysql:x:501:501::/home/mysql:/sbin/nologin
3)安装数据库
[root@ser5 ]# tar -zxvf mysql-5.5.13.tar.gz
[root@ser5 ]# cd mysql-5.5.13
[root@ser5 mysql-5.5.13]#rm -rf /usr/local/mysql/
[root@ser5 mysql-5.5.13]# mv /root/Desktop/mysql.sh . // . 代表当前
[root@ser5 mysql-5.5.13]# sh mysql.sh
/usr/local/cmake/bin/cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DMYSQL_DATADIR=/data/mysql/data/ -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock -DMYSQL_USER=mysql -DEXTRA_CHARSETS=all -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1
注:若有报错信息,则根据报错信息安装相应的安装包,务必清除此目录下的CMakeCache.txt,然后再重新执行
[root@ser5 mysql-5.5.13]#make && make install
3.初始化授权库
[root@ser5 mysql-5.5.13]# cd /usr/local/mysql/
[root@ser5 mysql]# ls
bin 命令 data 目录 mysql-test scripts 初始化数据库
[root@ser5 ~]# cd /usr/local/mysql/
[root@ser5 mysql]# chown –R mysql.mysql .
[root@ser5 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql/data/ //初始化数据库
#cd /data/mysql/data
[root@ser5 data]# ls //查看初始化是否成功performance_schema
mysql performance_schema test
4.创建主配置文件,启动数据库服务
[root@ser5 Desktop]# cp lnmp+memcached/mysql-5.5.13/support-files/my-medium.cnf /etc/my.cnf //主配置模板
启动数据库服务
[root@ser5 Desktop]# netstat -anptul | grep :3306
[root@ser5 mysql]# cd /usr/local/mysql/bin/
[root@ser5 bin]# ./mysqld_safe --user=mysql --datadir=/data/mysql/data/ &
登录数据库服务
[root@ser5 ~]# /usr/local/mysql/bin/mysql -hlocalhost -uroot -p
Enter password:
5.设置快捷启动,并在所有终端有效,并设置数据库密码
# vim ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin/ //添加命令搜索路径
export PATH
#source ~/.bash_profile
设置数据库管理员从本机登录的密码 为123
[root@ser5 ~]# mysqladmin -hlocalhost -uroot password "123"
# cat /etc/ld.so.conf
#ldconfig
[root@ser5 ~]# jobs
[root@ser5 ~]# kill -9 %1
[root@ser5 ~]# ./mysqld_safe --user=mysql --datadir=/data/mysql/data/ & //重启服务
[root@ser5 ~]# mysql -uroot -p123
mysql>
6.service mysql restart给源码包写启动脚本;设置为service启动方式:
[root@ser5 ~]# cd /root/Desktop/lnmp+memcached/mysql-5.5.13/support-files/
# vim mysql.server basedir=/usr/local/mysql/ datadir=/data/mysql/data/
[root@ser5 support-files]# cp mysql.server /etc/init.d/mysqld //拷贝脚本
[root@ser5 support-files]# chmod +x /etc/init.d/mysqld //添加执行权限
[root@ser5 support-files]# pkill -9 mysql
[root@ser5 support-files]# service mysqld restart
[root@ser5 support-files]# chkconfig mysqld on
[root@ser5 support-files]# chkconfig --list mysqld
mysqld 服务支持 chkconfig,但它在任何级别中都没有被引用(运行“chkconfig --add mysqld”)
[root@ser5 support-files]# chkconfig --add mysqld
[root@ser5 support-files]# chkconfig --list mysqld
mysqld 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
同样ser6.hyzc.com也同样安装
修改ha.cf
由于现在是管理Mysql,故要将mysqld由heartbeat管理(2个节点都执行)
ser5.hyzc.com IPaddr::192.168.2.15/24/eth0:1 drbddisk::r0 Filesystem::/dev/drbd0::/data::ext4 mysqld
重新启动heartbeat
# service heartbeat restart
启动之后,进入mysql,建立数据库db,然后建立表t,插入数据
[root@ser5 local]# mysql -uroot -p
启动之后,进入mysql,建立数据库db,然后建立表t,插入数据
[root@ser5 resource.d]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.0.95 Source distribution Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | lost+found | | mysql | | test | +--------------------+ 5 rows in set (0.04 sec) mysql> create database db; Query OK, 1 row affected (0.01 sec) mysql> use db Database changed mysql> create table t (id int(10),name char(10)); Query OK, 0 rows affected (0.05 sec) mysql> insert into t values(001,"ser1"),(002,"ser2"); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from t; +------+----------+ | id | name | +------+----------+ | 1 | ser1| | 2 | ser2 | +------+----------+ 2 rows in set (0.00 sec) mysql> COMMIT; mysql> exit Bye
之后停止heartbeat,查看其它节点(ser6)里是否有mysql的数据
[root@ser5 /]# service heartbeat stop
drbd已经变为从了,drbd0已经从database里卸载了
[root@ser5 data]# cat /proc/drbd
version: 8.4.6 (api:1/proto:86-101)
GIT-hash: 833d830e0152d1e457fa7856e71e11248ccf3f70 build by root@ser5.hyzc.com, 2016-08-05 02:12:56
0: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r-----
ns:1308 nr:348 dw:1656 dr:6085 al:8 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
[root@ser5 data]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 96G 6.0G 85G 7% /
tmpfs tmpfs 415M 224K 415M 1% /dev/shm
/dev/sda1 ext4 194M 34M 151M 19% /boot
/dev/sr0 iso9660 4.2G 4.2G 0 100% /misc
在ser6里查看drbd是否为主,drbd0是否装载了database
[root@ser6 Desktop]# cat /proc/drbd
version: 8.4.6 (api:1/proto:86-101)
GIT-hash: 833d830e0152d1e457fa7856e71e11248ccf3f70 build by root@ser5.hyzc.com, 2016-08-05 02:12:56
0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
ns:348 nr:1308 dw:1656 dr:6097 al:6 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
[root@ser6 Desktop]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 96G 6.0G 85G 7% /
tmpfs 415M 224K 415M 1% /dev/shm
/dev/sda1 194M 34M 151M 19% /boot
/dev/sr0 4.2G 4.2G 0 100% /misc
/dev/drbd0 20G 203M 19G 2% /data
此时,ser6已经变为主,并且drbd0已经挂载到了database了
进入mysql里查看db数据库、t表是否已交传过来
[root@ser6 /]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.0.95 Source distribution Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db | | mysql | | test | +--------------------+ 6 rows in set (0.04 sec) mysql> use db; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from t; +------+----------+ | id | name | +------+----------+ | 1 | ser1 | | 2 | ser2 | +------+----------+ 2 rows in set (0.00 sec)
mysql已经收到了数据。这样我们的drbd+heartbeat+mysql已经实现了高可用的mysql数据库了。
遇到的问题:
报错1:
Aug 14 15:01:46 ser5.hyzc.com heartbeat: [85845]: ERROR: Bad permissions on keyfile [/etc/ha.d//authkeys], 600 recommended.
Aug 14 15:01:46 ser5.hyzc.com heartbeat: [85845]: ERROR: Authentication configuration error.
Aug 14 15:01:46 ser5.hyzc.com heartbeat: [85845]: ERROR: Configuration error, heartbeat not started.
解决方案:
chmod 600 authkeys即可
报错2
[root@test6 /]# service heartbeat start
logd is already running
Starting High-Availability services:
2012/03/14_21:36:50 INFO: Resource is stopped
[FAILED]
heartbeat[6579]: 2012/03/14_21:36:50 ERROR: Client child command [/usr/lib/heartbeat/ipfail] is not executable
heartbeat[6579]: 2012/03/14_21:36:50 ERROR: Heartbeat not started: configuration error.
heartbeat[6579]: 2012/03/14_21:36:50 ERROR: Configuration error, heartbeat not started.
解决办法:
如果发生这个问题,先查看你的系统是32还是64位的,如果是64位的,则在ha.cf
里respawn hacluster /usr/lib/heartbeat/ipfail吧这个lib改成lib64;32位的不变。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。