mysql高可用方案之HeartBeat+nfs

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:

环境规划:

node1  192.168.1.105  10.10.10.1

node2  192.168.1.106  10.10.10.2

集群软件:heartbeat-3.0.4

数据库:mysql-5.7.4二进制包



 

1.节点上的网络配置

node1节点:

[root@node1 ~]# hostname 
node1
[root@node1 ~]# ifconfig  eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:79:7F:C7  
          inet addr:192.168.1.105  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe79:7fc7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:21417 errors:0 dropped:0 overruns:0 frame:0
          TX packets:15243 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:4313797 (4.1 MiB)  TX bytes:2401808 (2.2 MiB)
          Interrupt:19 Base address:0x2000

[root@node1 ~]# vim /etc/hosts

192.168.1.105 node1
192.168.1.106 node2
10.10.10.1 node1
10.10.10.2 node2

[root@node1 ~]#

 

node2节点:

[root@node2 ha.d]# hostname 
node2
[root@node2 ha.d]# ifconfig  eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:2E:A0:37  
          inet addr:192.168.1.106  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe2e:a037/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:16702 errors:0 dropped:0 overruns:0 frame:0
          TX packets:11660 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:3872149 (3.6 MiB)  TX bytes:1574050 (1.5 MiB)
          Interrupt:19 Base address:0x2000

[root@node2 ha.d]# vim /etc/hosts

192.168.1.105 node1
192.168.1.106 node2
10.10.10.1 node1
10.10.10.2 node2

[root@node2 ha.d]#

 

2.创建mysql用户和mysql数据共享目录

node1节点:

[root@node1 ~]# groupadd  -g 502 mysql

[root@node1 ~]# useradd  -r -s /sbin/nologin -u 502 -g mysql mysql

[root@node1 ~]# mkdir /mysql/data -p

 

node2节点:

[root@node2 ~]# groupadd  -g 502 mysql

[root@node2 ~]# useradd  -r -s /sbin/nologin -u 502 -g mysql mysql

[root@node2 ~]# mkdir /mysql/data -p

 

3.安装mysql数据库软件

node1节点:

[root@node1 ~]# wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.4-m14-linux-glibc2.5-i686.tar.gz

[root@node1 ~]# tar xvf mysql-5.7.4-m14-linux-glibc2.5-i686.tar.gz

[root@node1 ~]# mv mysql-5.7.4-m14-linux-glibc2.5-i686 /usr/local/mysql-5.7.4

[root@node1 ~]# cd /usr/local/mysql-5.7.4

[root@node1 mysql-5.7.4]# cp -a support-files/my-default.cnf  /etc/my.cnf

[root@node1 mysql-5.7.4]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql-5.7.4/ --datadir=/mysql/data/

[root@node1 mysql-5.7.4]# ll /mysql/data/
total 122904
-rw-rw----. 1 mysql mysql       56 Dec 29 00:06 auto.cnf
-rw-rw----. 1 mysql mysql 12582912 Dec 29 00:06 ibdata1
-rw-rw----. 1 mysql mysql 50331648 Dec 29 00:06 ib_logfile0
-rw-rw----. 1 mysql mysql 50331648 Dec 29 00:03 ib_logfile1
-rw-rw----. 1 mysql mysql 12582912 Dec 29 00:06 ibtmp1
drwx------. 2 mysql mysql     4096 Dec 29 00:04 mysql
-rw-rw----. 1 root  root         6 Dec 29 00:06 mysqld_safe.pid
-rw-r-----. 1 mysql root      3919 Dec 29 00:06 node1.err
-rw-rw----. 1 mysql mysql        6 Dec 29 00:06 node1.pid
drwx------. 2 mysql mysql     4096 Dec 29 00:04 performance_schema

[root@node1 mysql-5.7.4]# vim /etc/my.cnf

basedir = /usr/local/mysql-5.7.4
datadir = /var/lib/mysql
port = 3306

[root@node1 mysql-5.7.4]# chown mysql:mysql /var/lib/mysql/

[root@node1 mysql-5.7.4]# cp -a support-files/mysql.server  /etc/init.d/mysqld

[root@node1 mysql-5.7.4]# chkconfig --add mysqld

[root@node1 mysql-5.7.4]# chkconfig  mysqld off

[root@node1 mysql-5.7.4]# vim /etc/exports

/mysql/data       192.168.1.0/24(no_root_squash,rw)

[root@node1 mysql-5.7.4]# exportfs  -arv
exporting 192.168.1.0/24:/mysql/data
[root@node1 mysql-5.7.4]# mount.nfs 192.168.1.105:/mysql/data/ /var/lib/mysql/

[root@node1 mysql-5.7.4]# /etc/init.d/mysqld restart
 ERROR! MySQL server PID file could not be found!
Starting MySQL..... SUCCESS! 
[root@node1 mysql-5.7.4]# netstat -antp | grep 3306
tcp        0      0 :::3306                     :::*                        LISTEN      27729/mysqld     

[root@node1 mysql-5.7.4]# /usr/local/mysql-5.7.4/bin/mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.7.4-m14 MySQL Community Server (GPL)

Copyright (c) 2000, 2014, 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> exit
Bye 

[root@node1 mysql-5.7.4]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS! 
[root@node1 mysql-5.7.4]# umount /var/lib/mysql/
[root@node1 mysql-5.7.4]# scp /etc/my.cnf node2:/etc/
root@node2's password: 
my.cnf                                                                              100%           1142        1.1KB/s         00:00    
[root@node1 mysql-5.7.4]# scp /etc/init.d/mysqld node2:/etc/init.d/mysqld
root@node2's password: 
mysqld                                                                             100%           11KB        10.7KB/s       00:00    
[root@node1 mysql-5.7.4]#

 

node2节点:

[root@node2 ha.d]# showmount  -e node1
Export list for node1:

/mysql/data       192.168.1.0/24(no_root_squash,rw)

[root@node2 ha.d]# mount.nfs 192.168.1.105:/mysql/data/ /var/lib/mysql/
[root@node2 ha.d]# /etc/init.d/mysqld restart
 ERROR! MySQL server PID file could not be found!
Starting MySQL........ SUCCESS! 
[root@node2 ha.d]# netstat -antup | grep 3306
tcp        0      0 :::3306                     :::*                        LISTEN      7355/mysqld         
[root@node2 ha.d]#

 

4.安装和配置HeartBeat集群软件

node1节点:

[root@node1 ~]# yum install heartbeat*

[root@node1 ~]# cp -a /usr/share/doc/heartbeat-3.0.4/{authkeys,haresources,ha.cf} /etc/ha.d/

[root@node1 ~]# cd /etc/ha.d/
[root@node1 ha.d]# vim authkeys

auth 3
3 md5 Hello!

[root@node1 ha.d]# vim ha.cf

bcast   eth1 

node node1
node node2

[root@node1 ha.d]# vim haresources

node1 IPaddr::192.168.1.100/24/eth0 Filesystem::192.168.1.105:/mysql/data::/var/lib/mysql::nfs mysqld

[root@node1 ha.d]#

 

node2节点:

[root@node2 ~]# yum install heartbeat*

[root@node2 ~]# scp node1:/etc/ha.d/{authkeys,haresources,ha.cf} /etc/ha.d/

 

5.启动heartbea服务

node1节点:

[root@node1 ha.d]# /etc/init.d/mysqld stop
 ERROR! MySQL server PID file could not be found!
[root@node1 ha.d]# /etc/init.d/heartbeat stop
Stopping High-Availability services: Done.

[root@node1 ha.d]# /etc/init.d/heartbeat  start
Starting High-Availability services: INFO:  Resource is stopped
Done.

[root@node1 ha.d]#

 

node2节点:

[root@node2 ~]# /etc/init.d/mysqld stop
Shutting down MySQL. SUCCESS! 
[root@node2 ~]# /etc/init.d/heartbeat stop
Stopping High-Availability services: Done.

[root@node2 ~]# /etc/init.d/heartbeat start
Starting High-Availability services: INFO:  Resource is stopped
Done.

[root@node2 ~]#

 

6.创建mysql远程连接帐号

node1节点:

[root@node1 ha.d]# /usr/local/mysql-5.7.4/bin/mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.7.4-m14 MySQL Community Server (GPL)

Copyright (c) 2000, 2014, 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> grant all privileges on *.* to 'remote'@'%' identified by 'system';      --创建远程帐号

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

[root@node1 ha.d]# /usr/local/mysql-5.7.4/bin/mysql -u remote -p -h 192.168.1.100
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.4-m14 MySQL Community Server (GPL)

Copyright (c) 2000, 2014, 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> create database tong;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| tong               |
+--------------------+
4 rows in set (0.01 sec)

mysql>

 

7.资源切换,测试mysqk服务是否正常

node1节点:

[root@node1 ~]# netstat -antup | grep 3306
tcp        0      0 :::3306                     :::*                        LISTEN      28750/mysqld        
[root@node1 ~]# /usr/share/heartbeat/hb_standby 
Going standby [all].
[root@node1 ~]# netstat -antup | grep 3306
[root@node1 ~]#

 

node2节点:

[root@node2 ~]# netstat -antp | grep 3306
tcp        0      0 :::3306                     :::*                        LISTEN      11091/mysqld        

[root@node2 ~]# /usr/local/mysql-5.7.4/bin/mysql -u remote -p -h 192.168.1.100
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.22 Source distribution

Copyright (c) 2000, 2014, 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 |
| mysql              |
| performance_schema |
| tong               |
+--------------------+
4 rows in set (0.03 sec)

mysql>

 










本文转自 z597011036 51CTO博客,原文链接:http://blog.51cto.com/tongcheng/1596959,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
3月前
|
监控 关系型数据库 MySQL
HeartBeat监控Mysql状态
HeartBeat监控Mysql状态
|
3月前
|
存储 关系型数据库 MySQL
Mysql高可用|索引|事务 | 调优
Mysql高可用|索引|事务 | 调优
|
4月前
|
SQL 容灾 关系型数据库
rds容灾与高可用
rds容灾与高可用
30 4
|
4月前
|
关系型数据库 MySQL
电子好书发您分享《MySQL MGR 8.0高可用实战》
电子好书发您分享《MySQL MGR 8.0高可用实战》 电子好书发您分享《MySQL MGR 8.0高可用实战》
92 1
|
4月前
|
Oracle 关系型数据库 MySQL
MySQL相关(六)- 事务隔离级别的实现方案(MVCC)
MySQL相关(六)- 事务隔离级别的实现方案(MVCC)
40 0
|
3月前
|
SQL 存储 关系型数据库
MySQL索引(二)索引优化方案有哪些
MySQL索引(二)索引优化方案有哪些
52 0
|
21天前
|
存储 SQL 分布式计算
搭建Mysql Cluster集群实现高可用
搭建Mysql Cluster集群实现高可用
18 0
|
21天前
|
关系型数据库 MySQL Linux
centos7下 Mysql+Keepalived 双主热备高可用图文配置详解
centos7下 Mysql+Keepalived 双主热备高可用图文配置详解
21 0
|
2月前
|
canal 消息中间件 关系型数据库
【分布式技术专题】「分布式技术架构」MySQL数据同步到Elasticsearch之N种方案解析,实现高效数据同步
【分布式技术专题】「分布式技术架构」MySQL数据同步到Elasticsearch之N种方案解析,实现高效数据同步
90 0
|
2月前
|
SQL 关系型数据库 MySQL
【MySQL技术之旅】(7)总结和盘点优化方案系列之常用SQL的优化
【MySQL技术之旅】(7)总结和盘点优化方案系列之常用SQL的优化
71 1