34补1-4 实现高可用mysql集群

HA Cluster基础及heartbeat实现HA

配置环境

node1:192.168.1.121 CentOS6.7

node2:192.168.1.122 CentOS6.7

node3:192.168.1.123 CentOS6.7

vip 192.168.1.88

配置前准备

   # cat /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.121           node1

192.168.1.122           node2

192.168.1.123           node3 

   #  ssh-keygen -t rsa -P ‘‘

   #  ssh-copy-id -i ~/.ssh/id_rsa.pub node1

   #  ssh-copy-id -i ~/.ssh/id_rsa.pub node2

   #  ssh-copy-id -i ~/.ssh/id_rsa.pub node3

   #  rpm -ivh epel-release-latest-6.noarch.rpm 

   #  yum -y ansible

   #  yum -y install ansible

   # cat /etc/ansible/hosts 

[ha]

192.168.1.121

192.168.1.122

192.168.1.123

   #  ansible ha -m copy -a ‘src=/etc/hosts dest=/etc‘

   #  ansible ha -m shell -a ‘ntpdate 192.168.1.62‘

   # ansible ha -m cron -a ‘minute="*/3" job="/usr/sbin/ntpdate 192.168.1.62" name="ntpdate"‘

   # ansible ha -m copy -a ‘src=/root/heartbeat2 dest=/root‘

   

   node1、node2和node3

   #yum -y install net-snmp-libs libnet PyXML perl-Time-Date

   # ls heartbeat2/

heartbeat-2.1.4-12.el6.x86_64.rpm #主程序包

heartbeat-debuginfo-2.1.4-12.el6.x86_64.rpm

heartbeat-devel-2.1.4-12.el6.x86_64.rpm

heartbeat-gui-2.1.4-12.el6.x86_64.rpm #提供hb-gui的包,可以先不装

heartbeat-ldirectord-2.1.4-12.el6.x86_64.rpm #后端健康检测的工具

heartbeat-pils-2.1.4-12.el6.x86_64.rpm

heartbeat-stonith-2.1.4-12.el6.x86_64.rpm #提供仲裁设备,节点隔离的包

# cd heartbeat2/

# rpm -ivh heartbeat-2.1.4-12.el6.x86_64.rpm heartbeat-pils-2.1.4-12.el6.x86_64.rpm heartbeat-stonith-2.1.4-12.el6.x86_64.rpm

[[email protected] ha.d]# cp /usr/share/doc/heartbeat-2.1.4/{ha.cf,haresources,authkeys} /etc/ha.d/

[[email protected] ha.d]# cd /etc/ha.d/

[[email protected] ha.d]# chmod 600 authkeys

[[email protected] ha.d]# openssl rand -base64 4

nuGXcw==

[[email protected] ha.d]# vim authkeys 

修改

#auth 1

#2 sha1 HI!

auth 2

2 sha1 nuGXcw==

[[email protected] ha.d]# vim ha.cf

修改

#logfile    /var/log/ha-log

logfile    /var/log/ha-log

修改

logfacility local0

#logfacility    local0

修改

#mcast eth0 225.0.0.1 694 1 0

mcast eth0 225.23.190.1 694 1 0

在node   kathy(212行左右)下添加

node node1 #指明所有的node结束

node node2

node node3

在#ping 10.10.10.254(223行左右)下添加

ping 192.168.1.1 #仲裁设备

修改

#compression    bz2 #是否压缩

#compression_threshold 2 #多大的文件才压缩

compression bz2

compression_threshold 2

[[email protected] ha.d]# vim haresources 

在末尾添加

node1   192.168.1.80/16/eth0/192.168.255.255 httpd

[[email protected] ha.d]# scp -p authkeys ha.cf haresources node2:/etc/ha.d

[[email protected] ha.d]# scp -p authkeys ha.cf haresources node3:/etc/ha.d

[[email protected] ~]# vim /var/www/html/index.html

<h1>node1.magedu.com</h1>

[[email protected] ~]# service httpd stop

Stopping httpd:                                            [  OK  ]

[[email protected] ~]# chkconfig httpd off

[[email protected] ~]# vim /var/www/html/index.html

<h1>node2.magedu.com</h1>

[[email protected] ~]# service httpd stop

Stopping httpd:                                            [  OK  ]

[[email protected] ~]# chkconfig httpd off

[[email protected] ~]# vim /var/www/html/index.html

<h1>node3.magedu.com</h1>

[[email protected] ~]# service httpd stop

Stopping httpd:                                            [  OK  ]

[[email protected] ~]# chkconfig httpd off

[[email protected] ha.d]# ansible ha -m service -a ‘name=heartbeat state=started‘

03 HA Cluster概念扩展及heartbeat实现

使自己变成备用结点

[[email protected] heartbeat]# /usr/lib64/heartbeat/hb_standby

使用变回主结点

[[email protected] heartbeat]# /usr/lib64/heartbeat/hb_takeover 

[[email protected] ~]# mkdir /web/htdocs -pv

[[email protected] ~]# vim /web/htdocs/index.html

<h1>Page On NFS Server</h1>

[[email protected] ~]# vim /etc/exports 

/web/htdocs     192.168.1.0/24(rw,no_root_squash)

[[email protected] ~]# service nfs start

[[email protected] heartbeat]# service heartbeat stop;ssh node2 ‘service heartbeat stop‘

[[email protected] ~]# cd /etc/ha.d/

[[email protected] ha.d]# vim haresources 

在末尾添加

node1   192.168.1.80/16/eth0/192.168.255.255 Filesystem::192.168.1.123::/web/htdocs::/var/www/htm::nfs httpd

[[email protected] ha.d]# service heartbeat start;ssh node2 ‘service heartbeat start‘

04 实现高可用mysql集群

[[email protected] ~]# service heartbeat stop;ssh node2 ‘service heartbeat stop‘

[[email protected] ~]# cd /etc/ha.d

[[email protected] ha.d]# vim ha.cf 

添加

crm on

[[email protected] ~]# vim /etc/ha.d/ha.cf 

添加

crm on

[[email protected] ha.d]# cd /root/heartbeat2/

[[email protected] heartbeat2]# yum -y install pygtk2-libglade 

[[email protected] heartbeat2]# rpm -ivh heartbeat-gui-2.1.4-12.el6.x86_64.rpm

[[email protected] ~]# yum -y install pygtk2-libglade

[[email protected] ~]# rpm -ivh heartbeat2/heartbeat-gui-2.1.4-12.el6.x86_64.rpm 

[[email protected] ~]# ansible ha -m service -a ‘name=heartbeat state=started‘

监控集群:

[[email protected] ha.d]# crm_mon

[[email protected] ha.d]# ansible ha -m shell -a ‘echo "mageedu" | passwd --stdin hacluster‘ #图形界面默认用户

[[email protected] ha.d]# hb_gui & #打开图形界面

[[email protected] ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to

         switch off the mode (command ‘c‘) and change display units to

         sectors (command ‘u‘).

Command (m for help): n

Command action

   e   extended

   p   primary partition (1-4)

p

Selected partition 4

First cylinder (2898-15665, default 2898): 

Using default value 2898

Last cylinder, +cylinders or +size{K,M,G} (2898-15665, default 15665): +30G

Command (m for help): t

Partition number (1-4): 4

Hex code (type L to list codes): 8e

Changed system type of partition 4 to 8e (Linux LVM)

Command (m for help): w

[[email protected] ~]# partx -a /dev/sda

BLKPG: Device or resource busy

error adding partition 1

BLKPG: Device or resource busy

error adding partition 2

BLKPG: Device or resource busy

error adding partition 3

BLKPG: Device or resource busy

error adding partition 4

[[email protected] ~]# pvcreate /dev/sda4

  Physical volume "/dev/sda4" successfully created

[[email protected] ~]# vgcreate myvg /dev/sda4

  Volume group "myvg" successfully created

You have new mail in /var/spool/mail/root

[[email protected] ~]# lvcreate -L 10G -n mydata myvg

  Logical volume "mydata" created.

[[email protected] ~]# mke2fs -t ext4 /dev/myvg/mydata 

[[email protected] ~]# mkdir /mydata

[[email protected] ~]# vim /etc/fstab

添加

/dev/myvg/mydata        /mydata                 ext4    defaults        0 0

[[email protected] ~]# vim /etc/exports

添加

/mydata         192.168.1.0/24(rw,no_root_squash)

[[email protected] ~]# groupadd -r -g 306 mysql

[[email protected] ~]# useradd -r -g 306 -u 306 mysql

[[email protected] ~]# mkdir /mydata/data

[[email protected] ~]# chown -R mysql.mysql /mydata/data/

[[email protected] ~]# exportfs -arv

exporting 192.168.1.0/24:/mydata

exporting 192.168.1.0/24:/web/htdocs

[[email protected] ~]# mkdir /mydata

[[email protected] ~]# mkdir /mydata

[[email protected] ~]# mount -t nfs 192.168.1.123:/mydata /mydata/

[[email protected] ~]# groupadd -r -g 306 mysql

[[email protected] ~]# useradd -r -g 306 -u 306 mysql

[[email protected] ~]# su - mysql

-bash-4.1$ cd /mydata/data/

-bash-4.1$ touch a.txt

-bash-4.1$ ll

total 0

-rw-rw-r-- 1 mysql mysql 0 Sep 27 08:44 a.txt

-bash-4.1$ rm a.txt

bash-4.1$ exit

[[email protected] ~]# groupadd -r -g 306 mysql

[[email protected] ~]# useradd -r -g 306 -u 306 mysql

[[email protected] ~]# mount -t nfs 192.168.1.123:/mydata /mydata

[[email protected] ~]# su - mysql

-bash-4.1$ cd /mydata/data/

-bash-4.1$ touch b.txt

-bash-4.1$ exit

[[email protected] ~]# ll /mydata/data/

total 0

-rw-rw-r-- 1 mysql mysql 0 Sep 27 08:46 b.txt

[[email protected] ~]# touch /mydata/data/c.txt

[[email protected] ~]# ll /mydata/data/

total 0

-rw-rw-r-- 1 mysql mysql 0 Sep 27 08:46 b.txt

-rw-r--r-- 1 root  root  0 Sep 27 08:50 c.txt

[[email protected] ~]# rm /m-f ydata/data/*txt

Node1配置mysql

[[email protected] ~]# tar xf mariadb-5.5.43-linux-x86_64.tar.gz -C /usr/local/

[[email protected] ~]# cd /usr/local/

[[email protected] local]# ln -sv mariadb-5.5.43-linux-x86_64 mysql

[[email protected] local]# cd mysql

[[email protected] mysql]# chown -R root.mysql ./*

[[email protected] mysql]# ./scripts/mysql_install_db --datadir=/mydata/data/ --user=mysql

[[email protected] mysql]# mkdir /etc/mysql

[[email protected] mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf

[[email protected] mysql]# vim /etc/mysql/my.cnf 

在 thread_concurrency = 8(41行左右)后添加

datadir = /mydata/data

innodb_file_per_table = on

skip_name_resolv = on

[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld

[[email protected] mysql]# chkconfig --add mysqld

[[email protected] mysql]# service mysqld start

[[email protected] mysql]# /usr/local/mysql/bin/mysql

MariaDB [(none)]> create database mydb;

MariaDB [(none)]> \q

[[email protected] mysql]# service mysqld stop

[[email protected] mysql]# chkconfig mysqld off

[[email protected] ~]# umount /mydata

Node2配置mysql

[[email protected] ~]# tar xf mariadb-5.5.43-linux-x86_64.tar.gz -C /usr/local/

[[email protected] ~]# cd /usr/local/

[[email protected] local]# ln -sv mariadb-5.5.43-linux-x86_64 mysql

[[email protected] local]# cd mysql/

[[email protected] mysql]# chown -R root.mysql ./*

[[email protected] mysql]# mkdir /etc/mysql

[[email protected] ~]# scp /etc/mysql/my.cnf node2:/etc/mysql

[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld

[[email protected] mysql]# chkconfig --add mysqld

[[email protected] mysql]# chkconfig mysqld off

[[email protected] mysql]# service mysqld start

[[email protected] mysql]# /usr/local/mysql/bin/mysql

MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mydb               |

| mysql              |

| performance_schema |

| test               |

+--------------------+

5 rows in set (0.02 sec)

MariaDB [(none)]> \q

[[email protected] mysql]# service mysqld stop

[[email protected] mysql]# umount /mydata/

[[email protected] ~]# yum -y install mysql

[[email protected] ~]# hb_gui &

一、在myservices组里添加myip

添加myservices组

           类型选择“group

组名填写myservices


在myservice组里添加myip

在myservices组里添加mystore

在myservices组里添加myserver

启动myservices组

测试1:myip、mystore、myserver

myip:

[[email protected] mysql]# ip addr show

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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:88:49:f0 brd ff:ff:ff:ff:ff:ff

inet 192.168.1.122/24 brd 192.168.1.255 scope global eth0

inet 192.168.1.88/24 brd 192.168.1.255 scope global secondary eth0

inet6 fe80::20c:29ff:fe88:49f0/64 scope link 

  valid_lft forever preferred_lft forever

结果:成功

mystore:

[[email protected] mysql]# mount

/dev/sda2 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)

none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

//192.168.1.31/software on /mnt type cifs (rw)

sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

192.168.1.123:/mydata on /mydata type nfs (rw,vers=4,addr=192.168.1.123,clientaddr=192.168.1.122)

结果:成功

myserver:

[[email protected] mysql]# service mysqld status

MySQL running (21693)                                      [  OK  ]

结果:成功

测试2:设置node2结点为standby

测试myip、mystore、myserver均成功

[[email protected] ~]# /usr/local/mysql/bin/mysql

MariaDB [(none)]> grant all on *.* to ‘root‘@‘192.168.1.%‘ identified by ‘mageedu‘ ;

MariaDB [(none)]> flush privileges;

[[email protected] ~]# mysql -uroot -p -h192.168.1.88

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mydb               |

| mysql              |

| performance_schema |

| test               |

+--------------------+

mysql> use mydb;

mysql> create table t1(id int,name char(30));

mysql> show tables;

+----------------+

| Tables_in_mydb |

+----------------+

| t1             |

+----------------+

mysql> \q

重新在node3上登录mysql

[[email protected] ~]# mysql -uroot -p -h192.168.1.88

此时激活node2,测试node3上的mysql

测试结果:mysql没有断线,数据正常

时间: 2024-10-19 01:45:16

34补1-4 实现高可用mysql集群的相关文章

高可用mysql集群搭建

对web系统来说,瓶颈大多在数据库和磁盘IO上面,而不是服务器的计算能力.对于系统伸缩性我们一般有2种解决方案,scale-up(纵向扩展)和scale-out(横向扩展).前者如扩内存,增加单机性能,更换ssd等,虽然看似指标不治本而且比较昂贵,但确实是非常有效的,大多数应用的数据规模不是很大,当内存足够缓存下所有数据的时候,磁盘就没有什么压力了:后者譬如各类分布式解决方案,冗余磁盘阵列等. 在我看来,mysql读写分离是一个scale-up和scale-out的结合体,通过多个机器服务来提升

103-MHA 高可用mysql集群

一.MHA简介 1.1.简介 MHA(Master HA ) 是一款开源的 MySQL高可用程序,它为MYSQL 主从复制架构提供了automating master failover 主节点自动迁移功能.MHA在监控到master节点故障时候,会自动提升拥有的数据最近进于主节点的其他从节点为主节点, 并且在此期间,会通过其他节点获取额外的信息来避免数据不一致性的问题.MHA也提供master节点的在线切换功能, 即按需切换master/slave节点. 1.2.MHA服务角色 MHA Mana

drbd+corosync+pacemaker构建高可用MySQL集群

一.drbd简介 drbd全称Distributed Replicated Block Device,为分布式复制块设备,基于软件实现的,不共享任何东西的,通过复制的方式构建镜像模式工作的磁盘,类似于raid1,但不同于raid的是,drbd实现了跨主机镜像块数据.drbd工作原理:由工作于内核层次的drbd,将要写入本地磁盘的数据镜像一份发往本地网卡,由本地网卡发往另一台drbd主机的本地磁盘存储.因此,drbd的两个主机的,磁盘存储一模一样,从而实现分布式复制块设备的实现.drbd进程对磁盘

Amoeba搭建高可用Mysql集群(实现Mysql主从复制、读写分离、负载均衡)

Amoeba是什么? Amoeba(变形虫)项目,该开源框架于2008年 开始发布一款 Amoeba for Mysql软件.这个软件致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的时候充当SQL路由功能,专注于分布式数据库代理层(Database Proxy)开发,它位于与Client.DBServer(s)之间,对客户端透明.具有 负载均衡.高可用性.SQL过滤.读写分离.可路由相关的到目标数据库.可并发请求多台数据库合并结果 . 通过Amoeba你能够完成多数据源的

mysql主主复制+keepalived 打造高可用mysql集群

为了响应公司需求,打造出更安全的mysql集群,能够实现mysql故障后切换,研究了几天终于有了成果,一起分享一下. 首先介绍一下这套集群方案实现的功能 1.mysql服务器故障后自动转移,修好后自动切回 2.mysql服务故障自动转移,修好后自动切回 3.可以实现在几秒钟内转移 以下内容均是实验环境,请根据实际情况修改响应参数 实验环境: mysql1 ip:10.1.1.20 mysql2  ip:10.1.1.21 mysql vip:10.1.1.25 三台机器均安装centos 6.5

MySql-MMM搭建高可用mysql集群

简介 MMM(Master-Master replication manager for MySQL)是一套支持双主故障切换和双主日常管理的脚本程序.MMM使用Perl语言开发,主要用来监控和管理MySQL Master-Master(双主)复制,虽然叫做双主复制,但是业务上同一时刻只允许对一个主进行写入,另一台备选主上提供部分读服务,以加速在主主切换时刻备选主的预热,可以说MMM这套脚本程序一方面实现了故障切换的功能,另一方面其内部附加的工具脚本也可以实现多个slave的read负载均衡. M

MMM搭建高可用mysql集群

简介 MMM(Master-Master replication manager for MySQL)是一套支持双主故障切换和双主日常管理的脚本程序.MMM使用Perl语言开发,主要用来监控和管理MySQL Master-Master(双主)复制,虽然叫做双主复制,但是业务上同一时刻只允许对一个主进行写入,另一台备选主上提供部分读服务,以加速在主主切换时刻备选主的预热,可以说MMM这套脚本程序一方面实现了故障切换的功能,另一方面其内部附加的工具脚本也可以实现多个slave的read负载均衡. M

Haproxy Mysql cluster 高可用Mysql集群

-----client-----------haproxy---------mysql1----------mysql2------192.168.1.250 192.168.1.1 192.168.1.10 192.168.1.20 一.安装mysql[[email protected] ~]#tar -zxvf bison-2.5.tar.gz[[email protected] ~]#./configure && make&& make install[[email 

HA 高可用mysql集群

注意问题: 1.保持mysql用户和组的ID号是一致的: 2.filesystem 共享存储必须要有写入权限: 3.删除资源必须先删除约束,在删除资源: 1.安装数据库,这里使用maridb数据库: node1节点配置: tar -xf maridb-xx.tar.gz -C /usr/local ln -sv maridb-xx mysql cd /usr/local/mysql 初始化数据库,第二台node2不需要在进行初始化直接copy相关配置文件即可 2.配置filesystem NFS