基于NFS共享的Mysql之HA高可用集群实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于NFS共享的Mysql之HA高可用集群实现相关的知识,希望对你有一定的参考价值。


192.168.139.8 作为NFS-Server ,192.168.139.2和192.168.13.4用来安装mysql

___________________________________________________________________________________________以下操作在192.168.139.8上操作


[[email protected] ~]# fdisk -l //首先要准备一块磁盘进行分区,用来做lv,再将此lv格式化后挂载并                   //用NFS共享出去,让其他节点当做mysql的数据存放目录

Device Boot           Start         End    Blocks   Id  System

/dev/sdb1               1         132    1060258+  83  Linux

/dev/sdb2             133         394     2104515   83  Linux

/dev/sdb3             395         787     3156772+  83  Linux

[[email protected] ~]# pvcreate /dev/sdb1 /dev/sdb2 //创建一个pv

  Physical volume "/dev/sdb1" successfully created

  Physical volume "/dev/sdb2" successfully created

[[email protected] ~]# vgcreate myvg /dev/sdb1 /dev/sdb2 //创建一个名为myvg的vg

  Volume group "myvg" successfully created

[[email protected] ~]# vgdisplay //查看vg

  --- Volume group ---

  VG Name               myvg

  System ID             

  Format                lvm2

  Metadata Areas        2

  Metadata Sequence No  1

  VG Access             read/write

  VG Status             resizable

  MAX LV                0

  Cur LV                0

  Open LV               0

  Max PV                0

  Cur PV                2

  Act PV                2

  VG Size               3.01 GiB

  PE Size               4.00 MiB

  Total PE              771

  Alloc PE / Size       0 / 0   

  Free  PE / Size       771 / 3.01 GiB

  VG UUID               v4jClC-Viq1-31rQ-VDhB-ir1U-vF0y-zMHkkX

[[email protected] ~]# lvcreate -L 3G -n mylv myvg //在myvg上创建一个大小为3G的mylv

  Logical volume "mylv" created.

[[email protected] ~]# lvdisplay  //查看lv

  --- Logical volume ---

  LV Path                /dev/myvg/mylv

  LV Name                mylv

  VG Name                myvg

  LV UUID                6fKufw-r7NI-IawZ-ZCGU-TubY-F5uf-fqNm8w

  LV Write Access        read/write

  LV Creation host, time www.zxl.com, 2016-11-10 05:39:52 +0800

  LV Status              available

  # open                 0

  LV Size                3.00 GiB

  Current LE             768

  Segments               2

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     256

  Block device           253:2

[[email protected] ~]# mke2fs -j /dev/myvg/mylv //格式化为ext3

[[email protected] ~]# groupadd -g 3306 mysql //创建mysql组

[[email protected] ~]# useradd -u 3306 -g mysql -s /sbin/nologin -M mysql //创建mysql用户,组为                               //mysql,不能登录系统,且无指定的登录目录

[[email protected] ~]# mkdir /mydata

[[email protected] ~]# vim /etc/fstab //让开机自动挂载

/dev/myvg/mylv    /mydata    ext3    defaults        0 0

[[email protected] ~]# mount -a //重新挂载/etc/fstab中的挂载

[[email protected] ~]# mount //已经挂载

/dev/mapper/myvg-mylv on /mydata type ext3 (rw)

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

[[email protected] ~]# chown -R mysql.mysql /mydata/data //改数据目录所属组和所属者为mysql

[[email protected] ~]# vim /etc/exports //做NFS共享

/mydata 192.168.139.0/24(no_root_squash,rw) //若无no_root_squash,则mysql系统初始化时,是以                      //root身份操作的,root访问NFS会被映射为来宾账号,没有权限

[[email protected] ~]#service rpcbind start //启动rpc

[[email protected] ~]#service nfs start //启动NFS

[[email protected] ~]#showmount -e 192.168.139.2 //查看NFS有没有共享出去

___________________________________________________________________________________________

192.168.139.2 


[[email protected] mnt]# groupadd -g 3306 mysql

[[email protected] mnt]# useradd -u3306 -g 3306 -s /sbin/nologin -M mysql

[[email protected] mnt]# mkdir /mydata

[[email protected] /]# mount 192.168.139.8:/mydata /mydata

[[email protected] /]# cd /mydata/

[[email protected] mydata]# ll

total 20

drwxr-xr-x. 2 nobody nobody  4096 Nov 10  2016 data

drwx------. 2 nobody nobody 16384 Nov 10  2016 lost+found

[[email protected] mydata]# usermod -s /bin/bash mysql //测试一下MySQL用户能否在data目录下写

[[email protected] mydata]# su - mysql

su: warning: cannot change directory to /home/mysql: No such file or directory //因为创建                                               //MySQL用户时指定了-M

-bash-4.1$ cd /mydata/data/

-bash-4.1$ touch a

-bash-4.1$ ll

total 0

-rw-rw-r--. 1 nobody nobody 0 Nov 10  2016 a //说明可以写入

-bash-4.1$ rm a

-bash-4.1$ exit

[[email protected] mydata]# usermod -s /sbin/nologin mysql

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


__________________________________________________________________________________________


192.18.139.4上重复以上步骤


___________________________________________________________________________________________


192.168.139.2安装MySQL


http://mirrors.sohu.com/mysql/MySQL-5.5/  mysql下载地址

[[email protected] tool]# tar -xf mysql-5.5.53.tar.gz -C /usr/local

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

[[email protected] local]# ll

drwxr-xr-x. 31 7161 31415 4096 Sep 28 23:05 mysql-5.5.53

[[email protected] local]# ln -sv mysql-5.5.53 mysql

`mysql‘ -> `mysql-5.5.53‘

[[email protected] local]# cd mysql

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

[[email protected] mysql]# ll

total 248

drwxr-xr-x.  2 root mysql  4096 Sep 28 23:05 BUILD

-rw-r--r--.  1 root mysql  8528 Sep 28 23:01 BUILD-CMAKE

......

[[email protected] mysql]# mount 192.168.139.8:/mydata /mydata/

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

           //MySQL初始化,指定以mysql身份运行,数据目录为/mydata/data,其中/mydata目录下挂载了192.168.139.8_NFS共享/mydata目录。

___________________________________________________________________________________________


一下初始化出现错误。网上找了很不知道为什么(^_^)

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

-bash: scripts/mysql_install_db: No such file or directory  //发现scripts目录下没有                                      //mysql_install_db 有/mysql_install_db.sh

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

-bash: scripts/mysql_install_db.sh: Permission denied //没有权限

[[email protected] scripts]# chown -R mysql:mysql mysql_install_db.sh 

[[email protected] scripts]# ll

-rw-r--r--. 1 mysql mysql  14806 Sep 28 23:01 mysql_install_db.sh

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

-bash: scripts/mysql_install_db.sh: No such file or directory  //还是不能初始化,难道包有问题?

[[email protected] opt]# find / -name mysql_install_db //仍然找不到

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

[[email protected] local]# rm -rf mysql-5.5.53/


___________________________________________________________________________________________


重新装一个包


[[email protected] mysql]# rpm -qa |grep mysql //将系统原来安装的卸载

mysql-libs-5.1.73-5.el6_6.x86_64

[[email protected] mysql]# rpm -e --nodeps  mysql-libs-5.1.73-5.el6_6.x86_64 //卸载已经安装的,要忽                                           //略依赖关系

[[email protected] local]# unzip mysql-5.5.53.zip   //解压

[[email protected] local]# rm -rf mysql-5.5.53.zip  //删除原来的包

[[email protected] local]# ln -sv mysql-5.5.53/ mysql

`mysql‘ -> `mysql-5.5.53/‘

[[email protected] tool]# cd mysql

[[email protected] mysql]# scripts/mysql_install_db  --user=mysql --datadir=/mydata/data                                               //系统 初始化mysql

[[email protected] mysql]# cp /usr/local/support-files/my-large.cnf /etc/my.cnf//复制mysql的配置文件

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

datadir=/mydata/data //加入数据存放目录

innodb_file_per_table=1 //开启mysql的每表一个表空间

[[email protected] mysql]# cp support-files/mysql.server.sh /etc/init.d/mysql //复制mysql的启动脚本

[[email protected] mysql]# chkconfig --add mysql //将mysql加入系统服务

[[email protected] mysql]# chkconfig mysqld off //集群资源不能开机自启动

[[email protected] mysql]# service mysqld start

[[email protected] mysql]# service mysqld stop

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


___________________________________________________________________________________________


192.168.139.4上重复安装mysql


___________________________________________________________________________________________


192.168.139.2


[[email protected] mysql]service heartbeat start

[[email protected] mysql]ssh 192.168.139.4 service heartbeat start

[[email protected] mysql]Hb_gui //在图形界面下将mysql,VIP(192.168.139.10),Filesystem配置成一个资               //源组,并让VIP先启动,在启动Filesystem,最后启动mysql

[[email protected] mysql]crm_mon

可以看到192.168.139.4被选为了DC

[[email protected] mysql]mysql -uroot -p -h192.168.139.10 //连接VIP,可以连接上去

让192.168.139.4坏掉

[[email protected] mysql]mysql -uroot -p -h192.168.139.10 //仍然可以连接上去

  主要点在于,mysql的数据存放目录是NFS-Server共享的目录,这样在两台主机上装上mysql会公用一个数据存放目录,使数据保持一致,这样就让mysql实现了高可用,但NFS-Server仍然是单节点,容易成为集群故障点










本文出自 “11097124” 博客,请务必保留此出处http://11107124.blog.51cto.com/11097124/1871489

以上是关于基于NFS共享的Mysql之HA高可用集群实现的主要内容,如果未能解决你的问题,请参考以下文章

heatbeat-gui实现基于nfs的mysql高可用集群

Heartbeat+Haresources+NFS配置一个简单的HA高可用+资源共享集群

CentOS6.4 高可用集群之基于heartbeat(crm)和nfs的mysql高可用

高可用集群之Corosync+Pacemaker及用CRM命令和NFS-server构建一个HA高可用集群

Linux 高可用(HA)集群基本概念详解

Linux高可用集群方案之配置heartbeat v2基于crm+hb_gui接口,配置http+msyql+nfs高可用集群