您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
在ha的基础上搭建mysql集群
发布时间:2019-05-02 18:47:57编辑:雪饮阅读()
前面讲过通过gui管理ha集群管理节点资源,今天来讲讲mysql集群资源的部署。
准备mysql共享存储
在nfs资源节点上需要准备mysql集群的公共存储
配置nfs主机名
[root@localhost ~]# hostname nfs.magedu.com
[root@localhost ~]# cat /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=yes
HOSTNAME=nfs.magedu.com
[root@nfs ~]# cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
192.168.2.189 nfs.magedu.com
该步骤非必要,但为了后面方便,特别是最后这个hosts也要配置,否则可能会导致io错误
准备共享存储磁盘
[root@nfs ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
The number of cylinders for this disk is set to 2610.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
Partition number (1-4): 4
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-2610, default 2610):
Using default value 2610
Command (m for help): p
Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb4 1 2610 20964793+ 5 Extended
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-2610, default 2610): +20G
Command (m for help): p
Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb4 1 2610 20964793+ 5 Extended
/dev/sdb5 1 2433 19543009+ 83 Linux
Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@nfs ~]# partprobe /dev/sdb
将物理卷创建为卷组
[root@nfs ~]# pvcreate /dev/sdb5
Writing physical volume data to disk "/dev/sdb5"
Physical volume "/dev/sdb5" successfully created
[root@nfs ~]# vgcreate myvg /dev/sdb5
Volume group "myvg" successfully created
从卷组中创建一个逻辑卷
[root@nfs ~]# lvcreate -L 10G -n mydata myvg
Logical volume "mydata" created
格式化逻辑卷
[root@nfs ~]# mke2fs -j /dev/myvg/mydata
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
1310720 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
80 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
挂载
[root@nfs ~]# cat /etc/fstab
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
/dev/myvg/mydata /mydata ext3 defaults 0 0
[root@nfs ~]# mount -a
启动服务
[root@nfs ~]# service nfs restart
Shutting down NFS mountd: [FAILED]
Shutting down NFS daemon: [FAILED]
Shutting down NFS quotas: [FAILED]
Shutting down NFS services: [ OK ]
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS daemon: [ OK ]
Starting NFS mountd: [ OK ]
准备mysql对文件的权限
[root@nfs ~]# groupadd -g 3306 mysql
[root@nfs ~]# useradd -u 3306 -g mysql -s /sbin/nologin -M mysql
[root@nfs ~]# mkdir /mydata/data
[root@nfs ~]# chown -R mysql.mysql /mydata/data/
[root@nfs ~]# cat /etc/exports
/web/htdocs 192.168.2.0/255.255.255.0(ro)
/mydata 192.168.2.0/255.255.255.0(no_root_squash,rw)
[root@nfs ~]# exportfs -arv
exporting 192.168.2.0/255.255.255.0:/web/htdocs
exporting 192.168.2.0/255.255.255.0:/mydata
no_root_squash:登入 NFS 主机使用分享目录的使用者,如果是 root 的话,那么对于这个分享的目录来说,他就具有 root 的权限!这个项目『极不安全』,建议在生产环境中通过iptables等加以约束。
集群主节点准备
这里虽然说是集群主节点,其实任何一个都可以,主要是为了初始化一次数据库
[root@node1 ~]# groupadd -g 3306 mysql
[root@node1 ~]# useradd -g 3306 -u 3306 -s /sbin/nologin -M mysql
[root@node1 ~]# mkdir /mydata
[root@node1 ~]# tar zxvf /usr/local/src/mysql-5.5.28-linux2.6-i686.tar.gz -C /usr/local
[root@node1 ~]# ln -sv /usr/local/mysql-5.5.28-linux2.6-i686 /usr/local/mysql
create symbolic link `/usr/local/mysql' to `/usr/local/mysql-5.5.28-linux2.6-i686'
[root@node1 ~]# mount -t nfs 192.168.2.189:/mydata /mydata
[root@node1 ~]# chown -R root:mysql /usr/local/mysql/*
[root@node1 ~]# cd /usr/local/mysql
[root@node1 mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
[root@node1 mysql]# cp support-files/my-large.cnf /etc/my.cnf
打开文件/etc/my.cnf将其中“thread_concurrency = 8”下面插入:
datadir = /mydata/data
innodb_file_per_table = 1
其中“innodb_file_per_table = 1”意思是让每个表独立一个文件作为表空间
[root@node1 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@node1 mysql]# chkconfig --add mysqld
[root@node1 mysql]# chkconfig mysqld off
配置远程访问(该步骤在生产环境中慎用,或者结合其它策略用,这里为了方便测试)
为了要测试mysql集群的节点主从切换后照样能访问,所以这里为了方便测试就直接开启了远程访问
[root@node1 mysql]# /usr/local/mysql/bin/mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.28-log MySQL Community Server (GPL)
Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant all on *.* to 'root'@'%' identified by 'root';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
停止mysqld服务并去除挂载
[root@node1 mysql]# service mysqld stop
Shutting down MySQL. [ OK ]
You have new mail in /var/spool/mail/root
[root@node1 mysql]# umount /mydata
非主集群节点准备
[root@node2 ~]# groupadd -g 3306 mysql
[root@node2 ~]# useradd -g 3306 -u 3306 -s /sbin/nologin -M mysql
[root@node2 ~]# mkdir /mydata
[root@node2 ~]# tar zxvf /usr/local/src/mysql-5.5.28-linux2.6-i686.tar.gz -C /usr/local
[root@node2 ~]# ln -sv /usr/local/mysql-5.5.28-linux2.6-i686 /usr/local/mysql
create symbolic link `/usr/local/mysql' to `/usr/local/mysql-5.5.28-linux2.6-i686'
[root@node1 ~]# chown -R root:mysql /usr/local/mysql/*
从主节点下载
[root@node2 ~]# scp node1.magedu.com:/etc/my.cnf /etc/
[root@node2 mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@node2 mysql]# chkconfig --add mysqld
[root@node2 mysql]# chkconfig mysqld off
通过gui将mysql资源添加
首先从主节点依次启动集群
[root@node1 mysql]# service heartbeat start
logd is already running
Starting High-Availability services:
2019/04/29_04:04:37 INFO: Resource is stopped
[ OK ]
[root@node1 mysql]# ssh node2.magedu.com 'service heartbeat start'
logd is already running
Starting High-Availability services:
2019/04/29_04:05:19 INFO: Running OK
2019/04/29_04:05:19 CRITICAL: Resource IPaddr::192.168.2.139/24/eth0 is active, and should not be!
2019/04/29_04:05:19 CRITICAL: Non-idle resources can affect data integrity!
2019/04/29_04:05:19 info: If you don't know what this means, then get help!
2019/04/29_04:05:19 info: Read the docs and/or source to /usr/share/heartbeat/ResourceManager for more details.
CRITICAL: Resource IPaddr::192.168.2.139/24/eth0 is active, and should not be!
CRITICAL: Non-idle resources can affect data integrity!
info: If you don't know what this means, then get help!
info: Read the docs and/or the source to /usr/share/heartbeat/ResourceManager for more details.
2019/04/29_04:05:19 CRITICAL: Non-idle resources will affect resource takeback!
2019/04/29_04:05:19 CRITICAL: Non-idle resources may affect data integrity!
[ OK ]
通过crm_mon命令确保各节点都已经online
Refresh in 6s...
============
Last updated: Mon Apr 29 04:06:26 2019
Current DC: node1.magedu.com (74ce6c11-f5a4-408d-ab48-dc661a90aa8b)
2 Nodes configured.
3 Resources configured.
============
Node: node2.magedu.com (084771bc-320a-432c-9428-4fe6377d5ca9): online
Node: node1.magedu.com (74ce6c11-f5a4-408d-ab48-dc661a90aa8b): online
webip (ocf::heartbeat:IPaddr): Started node2.magedu.com
webstore (ocf::heartbeat:Filesystem): Started node2.magedu.com
httpd (lsb:httpd): Started node2.magedu.com
Failed actions:
webstore_start_0 (node=node1.magedu.com, call=6, rc=1): complete
登陆gui并清除干扰因素
因为之前添加过位置约束、顺序约束、排列约束
位置约束、顺序约束、排列约束都删除了,防止影响我们的实验
另外资源节点也都删除了重新以资源组的形式来建立
[root@node1 mysql]# hb_gui &
[1] 6052
添加sql资源集群
vip资源和存储资源之前已经讲过,这里就不演示了,这里只给演示下最后一个资源即mysql资源的添加
测试
经过测试,通过主从切换的确可以实现高可用,但是此时若直接断电某个节点,则会导致之前该节点资源到其他节点上之后mysqld服务无法启动了,个人认为这是mysqld这边的问题,因为你断电之后之前节点所占用mysqld的pid还存在于共享存储中呢。
关键字词:ha,mysql,集群