基於NFS共用的Mysql之HA高可用叢集實現

來源:互聯網
上載者:User

標籤:基於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高可用叢集實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.