一:试验目的
利用heartbeat心跳机制haresource实现高可用(HA)mysql数据库的搭建。
二:试验拓扑图
三:安装前的环境准备
规划IP为192.168.1.14为node1;IP为192.168.1.2为node2
1)修改各主机名称
修改node1主机名称 vim /etc/sysconfig/network NETWORKING=yes HOSTNAME=node1 #修改主机名称为node1 vim /etc/hosts 192.168.1.2 node2 192.168.1.14 node1 修改node2的主机名称 vim /etc/sysconfig/network NETWORKING=yes HOSTNAME=node2 #修改主机名称为node2 vim /etc/hosts 192.168.1.2 node2 #添加解析地址 192.168.1.14 node1 添加完后测试可以ping的通 [root@node2 ~]# ping node1 PING node1 (192.168.1.14) 56(84) bytes of data. 64 bytes from node1 (192.168.1.14): icmp_seq=1 ttl=64 time=7.64 ms 64 bytes from node1 (192.168.1.14): icmp_seq=2 ttl=64 time=0.731 ms 64 bytes from node1 (192.168.1.14): icmp_seq=3 ttl=64 time=0.533 ms 64 bytes from node1 (192.168.1.14): icmp_seq=4 ttl=64 time=0.501 ms [root@node2 ~]# ping node2 PING node2 (192.168.1.2) 56(84) bytes of data. 64 bytes from node2 (192.168.1.2): icmp_seq=1 ttl=64 time=4.79 ms 64 bytes from node2 (192.168.1.2): icmp_seq=2 ttl=64 time=0.154 ms 64 bytes from node2 (192.168.1.2): icmp_seq=3 ttl=64 time=0.123 ms
2)各节点之间ssh互信
node1: ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' ssh-copy-id -i .ssh/id_rsa.pub root@node2 node2: ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' ssh-copy-id -i .ssh/id_rsa.pub root@node1
3) 进行时间同步(node1和node2都要同步)
ntpdate 172.16.0.1 #后面的IP地址就是你所搭建的时间服务器IP地址 [root@node1 ~]# ssh node2 'date';date Wed Sep 10 17:44:44 CST 2014 Wed Sep 10 17:44:44 CST 2014 #时间要保持一致
四:安装与配置heartbeat
所要安装heartbeat包文件 http://down.51cto.com/data/1872114
1)安装heartbeat包文件(在centos6.5上安装包文件)
首先解决依赖关系 yum install -y perl-TimeDate PyXML libnet net-snmp-libs 用rpm安装方式依次安装(因为在centos6.5上用yum安装时会出现更新安装的情况所以用rpm安装) rpm -ivh heartbeat-pils-2.1.4-12.el6.x86_64.rpm rpm -ivh heartbeat-stonith-2.1.4-12.el6.x86_64.rpm rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm
2)配置heartbeat
说明:默认安装好heartbeat没有配置文件,但是样例文件 有可以复制古来使用
cd /usr/share/doc/heartbeat-2.1.4/ cp cp ha.cf authkeys haresources /etc/ha.d vim ha.cf #配置ha.cf文件 logfile /var/log/ha-log #启用logfile mcast eth0 225.16.16.16 694 1 0 #配置广播地址 auto_failback on #主节点,当主节点恢复后立即把资源抢夺回来 node node2 node node1 #两个节点 ping 172.16.0.1 #当只有偶数节点时,这是个仲裁机构 vim haresources node1 172.16.16.3/16/eth0:0/172.16.255.255 mysqld #添加资源 vim authkeys auth 2 2 sha1 1234567123 #这里可以添加随机数
3)复制以上三个配置文件到node2上
scp authkeys ha.cf haresources node2:/etc/ha.d/
4)启动节点
[root@node1 ha.d]# service heartbeat start Starting High-Availability services: 2014/09/10_17:03:18 INFO: Resource is stopped Done. [root@node1 ha.d]# ssh node2 'service heartbeat start' Starting High-Availability services: 2014/09/10_17:04:26 INFO: Resource is stopped Done. 检查node1端口是否在监听状态 [root@node1 ha.d]# ss -nul State Recv-Q Send-Q Local Address:Port Peer Address:Port UNCONN 0 0 *:111 *:* UNCONN 0 0 *:631 *:* UNCONN 0 0 *:647 *:* UNCONN 0 0 *:942 *:* UNCONN 0 0 225.16.16.16:694 *:* #监听状态 UNCONN 0 0 *:839 *:* UNCONN 0 0 *:50761 *:* UNCONN 0 0 *:847 *:* UNCONN 0 0 *:46824 *:* UNCONN 0 0 :::111 :::* UNCONN 0 0 :::839 :::* UNCONN 0 0 :::36945 :::* 检查node2端口是否在监听状态 [root@node2 ~]# ss -unl State Recv-Q Send-Q Local Address:Port Peer Address:Port UNCONN 0 0 *:111 *:* UNCONN 0 0 *:631 *:* UNCONN 0 0 *:47496 *:* UNCONN 0 0 *:664 *:* UNCONN 0 0 225.16.16.16:694 *:* #监听状态 UNCONN 0 0 *:52941 *:* UNCONN 0 0 *:725 *:* UNCONN 0 0 :::111 :::* UNCONN 0 0 :::664 :::* UNCONN 0 0 :::39515 :::*
五:配置NFS服务
1)在NFS文件服务器上创建服务文件
pvcreate /dev/sdb #创建物理卷 vgcreate myvg /dev/sdb #创建卷组 lvcreate -L 10G -n mydata myvg #创建逻辑卷 mke2fs -j /dev/myvg/mydata #格式化逻辑卷 lvs #查看逻辑卷 mkdir /mydata #创建挂载目录 mount /dev/myvg/mydata /mydata/ #挂载 cd /mydata/ #进入挂载目录
2)在node1和node2上创建data目录给予mysql用户和mysql组。
mkdir data chown -R mysql:mysql data
3)修改nfs配置文件
[root@localhost ~]# vim /etc/exports /mydata 192.168.1.2(no_root_squash,rw,no_all_squash) 192.168.1.14(no_root_squash,r w,no_all_squash) [root@localhost ~]# service nfs start #启动NFS
4)挂在nfs
mount -t nfs 192.168.1.16:/mydata /data [root@node2 ~]# mount #查看挂在情况 /dev/mapper/vg0-root on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw) /dev/sda1 on /boot type ext4 (rw) /dev/mapper/vg0-usr on /usr type ext4 (rw) /dev/mapper/vg0-var on /var type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) 192.168.1.16:/mydata on /data type nfs (rw,vers=4,addr=192.168.1.16,clientaddr=192.168.1.2) node1 操作如上
5)二进制安装mysql
二进制安装mysql请参考http://slayer.blog.51cto.com/4845839/1539643
因为这里用到是NFS文件系统要对挂在的磁盘进行初始化,和修改mysql的配置文件
vim /etc/my.cnf [mysqld] port = 3306 socket = /tmp/mysql.sock skip-external-locking key_buffer_size = 256M max_allowed_packet = 1M table_open_cache = 256 sort_buffer_size = 1M read_buffer_size = 1M read_rnd_buffer_size = 4M myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size= 16M # Try number of CPU's*2 for thread_concurrency thread_concurrency = 4 datadir = /data #改为挂载的所在磁盘 innodb_file_per_table = 1 #innodb表单独表空间 [root@node1 ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --da tadir=/data --user=mysql # 进行磁盘初始化 启动mysql [root@node1 mysql]# service mysqld start Starting MySQL... [ OK ] 测试能否登录mysql [root@node1 ha.d]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 1 Server version: 5.5.32-MariaDB-log MariaDB Server Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases -> ; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.06 sec) MariaDB [(none)]> 配置node2的mysql (把启动脚本和主配置文件发给node2) [root@node1 ~]# scp /etc/my.cnf node2:/etc/ my.cnf 100% 4721 4.6KB/s 00:00 [root@node1 ~]# scp /etc/init.d/mysqld node2:/etc/init.d/ mysqld 100% 11KB 10.6KB/s 00:00 测试node2能否启动 [root@node2 ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.32-MariaDB-log MariaDB Server Copyright (c) 2000, 2013, 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> mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.04 sec) mysql>
OK 我们的Heartbeat+MySQL+NFS 实现高可用(HA)的MySQL集群基于heartbeat 的haresources配置完毕。