首页 > 代码库 > 基于NFS共享的Mysql之HA高可用集群实现
基于NFS共享的Mysql之HA高可用集群实现
192.168.139.8 作为NFS-Server ,192.168.139.2和192.168.13.4用来安装mysql
___________________________________________________________________________________________以下操作在192.168.139.8上操作
[root@www ~]# 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
[root@www ~]# pvcreate /dev/sdb1 /dev/sdb2 //创建一个pv
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created
[root@www ~]# vgcreate myvg /dev/sdb1 /dev/sdb2 //创建一个名为myvg的vg
Volume group "myvg" successfully created
[root@www ~]# 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
[root@www ~]# lvcreate -L 3G -n mylv myvg //在myvg上创建一个大小为3G的mylv
Logical volume "mylv" created.
[root@www ~]# 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
[root@www ~]# mke2fs -j /dev/myvg/mylv //格式化为ext3
[root@www ~]# groupadd -g 3306 mysql //创建mysql组
[root@www ~]# useradd -u 3306 -g mysql -s /sbin/nologin -M mysql //创建mysql用户,组为 //mysql,不能登录系统,且无指定的登录目录
[root@www ~]# mkdir /mydata
[root@www ~]# vim /etc/fstab //让开机自动挂载
/dev/myvg/mylv /mydata ext3 defaults 0 0
[root@www ~]# mount -a //重新挂载/etc/fstab中的挂载
[root@www ~]# mount //已经挂载
/dev/mapper/myvg-mylv on /mydata type ext3 (rw)
[root@www ~]# mkdir /mydata/data
[root@www ~]# chown -R mysql.mysql /mydata/data //改数据目录所属组和所属者为mysql
[root@www ~]# vim /etc/exports //做NFS共享
/mydata 192.168.139.0/24(no_root_squash,rw) //若无no_root_squash,则mysql系统初始化时,是以 //root身份操作的,root访问NFS会被映射为来宾账号,没有权限
[root@www ~]#service rpcbind start //启动rpc
[root@www ~]#service nfs start //启动NFS
[root@www ~]#showmount -e 192.168.139.2 //查看NFS有没有共享出去
___________________________________________________________________________________________
192.168.139.2
[root@www mnt]# groupadd -g 3306 mysql
[root@www mnt]# useradd -u3306 -g 3306 -s /sbin/nologin -M mysql
[root@www mnt]# mkdir /mydata
[root@www /]# mount 192.168.139.8:/mydata /mydata
[root@www /]# cd /mydata/
[root@www 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
[root@www mydata]# usermod -s /bin/bash mysql //测试一下MySQL用户能否在data目录下写
[root@www 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
[root@www mydata]# usermod -s /sbin/nologin mysql
[root@www /]# umount /mydata/
__________________________________________________________________________________________
192.18.139.4上重复以上步骤
___________________________________________________________________________________________
192.168.139.2安装MySQL
http://mirrors.sohu.com/mysql/MySQL-5.5/ mysql下载地址
[root@www tool]# tar -xf mysql-5.5.53.tar.gz -C /usr/local
[root@www tool]# cd /usr/local/
[root@www local]# ll
drwxr-xr-x. 31 7161 31415 4096 Sep 28 23:05 mysql-5.5.53
[root@www local]# ln -sv mysql-5.5.53 mysql
`mysql‘ -> `mysql-5.5.53‘
[root@www local]# cd mysql
[root@www mysql]# chown -R root:mysql ./*
[root@www 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
......
[root@www mysql]# mount 192.168.139.8:/mydata /mydata/
[root@www mysql]# script/mysql_install_db --user=mysql --datadir=/mydata/data
//MySQL初始化,指定以mysql身份运行,数据目录为/mydata/data,其中/mydata目录下挂载了192.168.139.8_NFS共享/mydata目录。
___________________________________________________________________________________________
一下初始化出现错误。网上找了很不知道为什么(^_^)
[root@www 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
[root@www mysql]# scripts/mysql_install_db.sh --user=mysql --datadir=/mydata/data
-bash: scripts/mysql_install_db.sh: Permission denied //没有权限
[root@www scripts]# chown -R mysql:mysql mysql_install_db.sh
[root@www scripts]# ll
-rw-r--r--. 1 mysql mysql 14806 Sep 28 23:01 mysql_install_db.sh
[root@www scripts]# scripts/mysql_install_db.sh --user=mysql --datadir=/mydata/data
-bash: scripts/mysql_install_db.sh: No such file or directory //还是不能初始化,难道包有问题?
[root@www opt]# find / -name mysql_install_db //仍然找不到
[root@www opt]# cd /usr/local/
[root@www local]# rm -rf mysql-5.5.53/
___________________________________________________________________________________________
重新装一个包
[root@www mysql]# rpm -qa |grep mysql //将系统原来安装的卸载
mysql-libs-5.1.73-5.el6_6.x86_64
[root@www mysql]# rpm -e --nodeps mysql-libs-5.1.73-5.el6_6.x86_64 //卸载已经安装的,要忽 //略依赖关系
[root@www local]# unzip mysql-5.5.53.zip //解压
[root@www local]# rm -rf mysql-5.5.53.zip //删除原来的包
[root@www local]# ln -sv mysql-5.5.53/ mysql
`mysql‘ -> `mysql-5.5.53/‘
[root@www tool]# cd mysql
[root@www mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data //系统 初始化mysql
[root@www mysql]# cp /usr/local/support-files/my-large.cnf /etc/my.cnf//复制mysql的配置文件
[root@www mysql]# vim /etc/my.cnf
datadir=/mydata/data //加入数据存放目录
innodb_file_per_table=1 //开启mysql的每表一个表空间
[root@www mysql]# cp support-files/mysql.server.sh /etc/init.d/mysql //复制mysql的启动脚本
[root@www mysql]# chkconfig --add mysql //将mysql加入系统服务
[root@www mysql]# chkconfig mysqld off //集群资源不能开机自启动
[root@www mysql]# service mysqld start
[root@www mysql]# service mysqld stop
[root@www mysql]# umount /mydata/
___________________________________________________________________________________________
192.168.139.4上重复安装mysql
___________________________________________________________________________________________
192.168.139.2
[root@www mysql]service heartbeat start
[root@www mysql]ssh 192.168.139.4 service heartbeat start
[root@www mysql]Hb_gui //在图形界面下将mysql,VIP(192.168.139.10),Filesystem配置成一个资 //源组,并让VIP先启动,在启动Filesystem,最后启动mysql
[root@www mysql]crm_mon
可以看到192.168.139.4被选为了DC
[root@www mysql]mysql -uroot -p -h192.168.139.10 //连接VIP,可以连接上去
让192.168.139.4坏掉
[root@www 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高可用集群实现