美文网首页
3.Heartbeat v2高可用mysql集群

3.Heartbeat v2高可用mysql集群

作者: 像飞翔一样去飞翔 | 来源:发表于2017-02-08 23:30 被阅读0次

    在第二篇,我们通过heartbeat v2的资源管理器(crm)构建了一个web service,在此篇,我们仍然通过heartbeat v2 版本来高可用一个mysql服务。

    实验架构

    我们准备两台mysql服务器,并且将他们的数据放在一个共享设备上,此处我们使用

    • ip:192.168.99.14
    • mariadb:
    • NFS:

    1.我们首先准备一个共享存储NFS

    此处我们仍然使用192.168.99.241的机器充当NFS,并且我们创建一个lvm逻辑卷将他挂载至一个目录通过NFS来导出。

    [root@nfs ~]# fdisk /dev/sda
    
    Command (m for help): n
    All primary partitions are in use
    Adding logical partition 7
    First sector (131033088-251658239, default 131033088): 
    Using default value 131033088
    Last sector, +sectors or +size{K,M,G} (131033088-251658239, default 251658239): +30G
    Partition 7 of type Linux and of size 30 GiB is set
    Command (m for help): t
    Partition number (1-7, default 7): 7
    Hex code (type L to list all codes): 8e
    Changed type of partition 'Linux' to 'Linux LVM'
    Command (m for help): w
    The partition table has been altered!
    
    [root@nfs ~]# partx -a /dev/sda
    partx: /dev/sda: error adding partitions 1-7
    
    [root@nfs ~]# pvcreate /dev/sda7
      Physical volume "/dev/sda7" successfully created
    [root@nfs ~]# vgcreate  myvg /dev/sda7
      Volume group "myvg" successfully created
    [root@nfs ~]# lvcreate  -L 10G -n mydata myvg
      Logical volume "mydata" created.
    [root@nfs ~]# mke2fs  -t ext4 /dev/myvg/mydata 
    
    [root@nfs ~]# mkdir /mydata
    [root@nfs ~]# vim /etc/fstab 
    /dev/myvg/mydata                          /mydata                 ext4    defaults        0 0
    [root@nfs ~]# mount -a #自动挂载
    # 导出/mydata目录
    [root@nfs ~]# vim /etc/exports
    /mydata         192.168.99.0/24(rw,async,no_root_squash)
    #创建mysql用户和mysql组,并将此目录的属组属主改为mysql.mysql
    [root@nfs ~]# id mysql
    uid=27(mysql) gid=27(mysql) groups=27(mysql)
    [root@nfs ~]# mkdir /mydata/data/
    [root@nfs ~]# chown -R mysql.mysql /mydata/data/
    #重新导出
    [root@nfs ~]# exportfs  -arv
    exporting 192.168.99.0/24:/mydata
    exporting 192.168.99.0/24:/web/htdocs
    

    2.在集群节点上创建目录用于挂载共享存储

    [root@node3 ~]# mkdir /mydata ;ssh node4.magedu.com 'mkdir /mydata'
    #创建mysql系统用户和系统组
    [root@node3 ~]# groupadd  -r -g 27 mysql
    [root@node3 ~]# useradd  -r -g 27 -u 27 mysql
    [root@node4 ~]# groupadd  -r -g 27 mysql
    [root@node4 ~]# useradd  -r -g 27 -u 27 mysql
    # 分别挂载nfs文件看看是否能进行读写
    [root@node3 ~]# mount -t nfs 192.168.99.241:/mydata/  /mydata/
    [root@node3 ~]# cd /mydata/data/
    [root@node3 data]# touch a.txt
    [root@node3 ~]# su - mysql
    -bash-4.1$ touch /mydata/data/c.txt
    [root@node4 ~]# mount -t nfs 192.168.99.241:/mydata/ /mydata/
    [root@node4 ~]# touch /mydata/data/b.txt
    [root@node4 ~]# su - mysql
    -bash-4.1$ touch /mydata/data/d.txt
    

    3.在集群节点上安装mysql服务

    #节点3安装配置
    [root@node3 ~]# lftp 192.168.99.254
    lftp 192.168.99.254:~> cd pub/Sources/6.x86_64/mariadb/
    lftp 192.168.99.254:/pub/Sources/6.x86_64/mariadb> mget mariadb-5.5.43-linux-x86_64.tar.gz 
    222275456 bytes transferred in 25 seconds (8.51M/s)                                       
    lftp 192.168.99.254:/pub/Sources/6.x86_64/mariadb> exit
    [root@node3 ~]# tar xf mariadb-5.5.43-linux-x86_64.tar.gz  -C /usr/local/
    [root@node3 ~]# cd /usr/local/
    [root@node3 local]# ln -sv mariadb-5.5.43-linux-x86_64/ mysql
    `mysql' -> `mariadb-5.5.43-linux-x86_64/'
    [root@node3 local]# cd mysql/
    [root@node3 mysql]# chown -R root.mysql ./*
    #初始化(只需要初始化一起即可)
    [root@node3 mysql]# ./scripts/mysql_install_db  --datadir=/mydata/data/ --user=mysql
    #提供配置文件和启动脚本
    [root@node3 mysql]# mkdir /etc/mysql
    [root@node3 mysql]# cp support-files/my-large.cnf  /etc/mysql/my.cnf
    [root@node3 mysql]# vim /etc/mysql/my.cnf
    [mysqld]
    datadir =/mydata/data
    innodb_file_per_table = on
    skip_name_resolve = on
    [root@node3 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
    [root@node3 mysql]# chkconfig --add mysqld
    [root@node3 mysql]# service mysqld start
    # 连上数据库,创建数据库并在共享存储上验证
    [root@node3 mysql]# /usr/local/mysql/bin/mysql
    MariaDB [(none)]> create database mydb;
    [root@nfs ~]# ls /mydata/data/
    aria_log.00000001  ibdata1      ib_logfile1  mysql             mysql-bin.index       performance_schema
    aria_log_control   ib_logfile0  mydb         mysql-bin.000001  node3.magedu.com.pid  test
    
    #验证ok后将mysql服务停掉,并且禁止期开机自启
    [root@node3 mysql]# service mysqld stop
    [root@node3 mysql]# chkconfig mysqld off
    #节点4
    [root@node4 ~]# lftp 192.168.99.254
    lftp 192.168.99.254:~> cd pub/Sources/6.x86_64/mariadb/
    lftp 192.168.99.254:/pub/Sources/6.x86_64/mariadb> mget mariadb-5.5.43-linux-x86_64.tar.gz 
    [root@node4 ~]# tar -xf mariadb-5.5.43-linux-x86_64.tar.gz  -C /usr/local/
    [root@node4 ~]# cd /usr/local/
    [root@node4 local]# ln -sv mariadb-5.5.43-linux-x86_64/ mysql
    `mysql' -> `mariadb-5.5.43-linux-x86_64/'
    [root@node4 local]# cd mysql/
    [root@node4 mysql]# chown -R root.mysql ./*
    [root@node4 mysql]# mkdir /etc/mysql
    [root@node4 mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf
    [root@node4 mysql]# vim /etc/mysql/my.cn
    datadir =/mydata/data
    innodb_file_per_table = on
    skip_name_resolve = on
    [root@node4 mysql]# cp support-files/mysql.server  /etc/rc.d/init.d/mysqld
    [root@node4 mysql]# chkconfig --add mysqld
    #此处不需要初始化,直接启动即可
    [root@node4 mysql]# service mysqld start
    [root@node4 mysql]# /usr/local/mysql/bin/mysql
    [root@node4 mysql]# service mysqld stop
    [root@node4 mysql]# chkconfig mysqld off
    

    至此,我们已经准备好两台mysql节点和共享存储,接下来我们需要需要将以前挂载的nfs设备卸载,并开始通过hb_gui配置mysql高可用集群。

    [root@node3 ~]# umount /mydata/
    [root@node4 ~]# umount /mydata/
    

    4.通过hb_gui定义资源

    应因为mysql服务使我们后面安装上去的,故我们需要重启heartbeat服务才能有效。

    [root@node3 ~]# service heartbeat stop;ssh node4.magedu.com 'service heartbeat stop'
    [root@node3 ~]# service heartbeat start;ssh node4.magedu.com 'service heartbeat start'
    
    [root@node3 ~]# hb_gui &
    [1] 46648
    
    • 添加组资源和IP地址
    • 右击Resources->Add New Item -->[group]-->OK
    Paste_Image.png
    Paste_Image.png
    Paste_Image.png

    上面我们定义了两个资源一个组资源myservicese和myip两个基本资源

    • 在组上添加一个新的资源
      • 右击mystory-->Add New Item-- >[native]
        Paste_Image.png
    • 添加资源myserver
      Paste_Image.png
      注意这些资源应该按次序添加
    • 启动资源


      Paste_Image.png

    至此,高可用mysql资源已经完成,接下我们需要做的就是测试

    测试

    -让node4.magedu.com进入stanby,则组资源会切换到node3.magedu.com


    Paste_Image.png
    [root@node3 ~]# ifconfig |grep -A 1 "eth0:0"
    eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:5B:F6:94  
              inet addr:192.168.99.22  Bcast:192.168.99.255  Mask:255.255.255.0
    
    [root@node3 ~]# 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.99.241:/mydata on /mydata type nfs (rw,vers=4,addr=192.168.99.241,clientaddr=192.168.99.41)
    
    [root@client ~]# mysql -u root -h 192.168.99.22 -p'magedu'
    

    相关文章

      网友评论

          本文标题:3.Heartbeat v2高可用mysql集群

          本文链接:https://www.haomeiwen.com/subject/jegbittx.html