2322312 发表于 2016-11-21 12:44:55

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

一、简述HA高可用集群

    高可用集群就是当集群中的一个节点发生各种软硬件及人为故障时,集群中的其他节点能够自动接管故障节点的资源并向外提供服务。以实现减少业务中断时间,为用户提供更可靠,更高效的服务。

二、基于nfs实现mysql的高可用集群配置

   环境准备接上文 heartbeat-gui部署

   实验环境:
   
nfs server准备

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
1、在nfs server准备LVM存储空间
# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x61284c6a.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (1-1305, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +10G
Value out of range.
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +5G

Command (m for help): t
Selected partition 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

# partx -a /dev/sdb

# pvcreate /dev/sdb3
Physical volume "/dev/sdb3" successfully created
# vgcreate myvg /dev/sdb3
Volume group "myvg" successfully created
# lvcreate -L 5G -n mydata myvg
Logical volume "mydata" created.
# mke2fs -t ext4 /dev/myvg/mydata
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736

Writing inode tables: done                           
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 36 mounts or
180 days, whichever comes first.Use tune2fs -c or -i to override.


2、开机自动挂载,并nfs导出
# mkdir/mydata
# vim /etc/fstab
/dev/myvg/mydata      /mydata               ext4    defaults      0 0
# mount -a
# mount | grep /mydata
/dev/mapper/myvg-mydata on /mydata type ext4 (rw)

# vim /etc/exports
/mydata         192.168.0.0/24(rw,no_root_squash)
#共享给192.168.0.0/24网段,可读可写,允许root用户登录便于初始化,配置结束可取消root用户登录


3、导出nfs共享目录
#创建mysql用户,指明uid,gid。各节点的mysql用户uid,gid一致。
# groupadd -r -g 306 mysql
# useradd -r -g 306 -u 306 mysql

#创建共享目录,并修改属主属组。
# mkdir /mydata/data
# chown -R mysql.mysql /mydata/data

#导出nfs共享目录
# exportfs -arv
exporting 192.168.0.0/24:/mydata





各节点准备mysql,并测试nfs。以下步骤,各节点一致。

node1配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# mkdir /mydata

# showmount -e 192.168.0.20
Export list for 192.168.0.20:
/mydata 192.168.0.0/24
# mount -t nfs 192.168.0.20:/mydata /mydata
# mount | grep /mydata
192.168.0.20:/mydata on /mydata type nfs (rw,vers=4,addr=192.168.0.20,clientaddr=192.168.0.15)

# groupadd -r -g 306 mysql
# useradd -r -g 306 -u 306 mysql

#验证mysql用户是否拥有共享目录权限
# su - mysql
su: warning: cannot change directory to /home/mysql: No such file or directory
-bash-4.1$
-bash-4.1$
-bash-4.1$ cd /mydata/data
-bash-4.1$ touch node1.txt
-bash-4.1$ ls
node1.txt
-bash-4.1$ rm node1.txt
-bash-4.1$ exit
logout


#在nfs server端验证
# cd /mydata/data
# ll
total 0
-rw-rw-r--. 1 mysql mysql 0 Nov 19 19:59 node2.txt

#验证root用户对目录是否有权限
# touch /mydata/data/node.txt
# ll /mydata/data
total 0
-rw-r--r--. 1 root root 0 Nov 19 20:02 node.txt

#安装mariadb
# tar xf mariadb-5.5.46-linux-x86_64.tar.gz -C /usr/local
# cd /usr/local
# ln -sv mariadb-5.5.46-linux-x86_64 mysql
`mysql' -> `mariadb-5.5.46-linux-x86_64'
# cd mysql/
# chown -R root.mysql ./*


#初始化mysql至nfs,此步骤只需一个节点操作即可,本文node1执行,那么node2就不需要执行
#初始化操作后,可以将nfs server的共享选项中的no_root_squash移除了
# ./scripts/mysql_install_db --datadir=/mydata/data/ --user=mysql

#nfs server验证
# ls
aria_log.00000001aria_log_controlmysqlperformance_schematest


#为mysql节点准备配置文件
# mkdir /etc/mysql
# cp support-files/my-large.cnf /etc/mysql/my.cnf
#编辑配置文件,加入以下三行
# vim /etc/mysql/my.cnf
datadir = /mydata/data
innodb_file_per_table = on
skip_name_resolve = on


#为mysql节点准备服务脚本,并禁止mysqld开机自启
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# chkconfig mysqld off

#启动mysql,创建mydb数据库
# service mysqld start
# /usr/local/mysql/bin/mysql
Welcome to the MariaDB monitor.Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.46-MariaDB-log MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CREATE DATABASE mydb;
Query OK, 1 row affected (0.03 sec)

MariaDB [(none)]> exit
Bye

# service mysqld stop





node2配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# tar xf mariadb-5.5.46-linux-x86_64.tar.gz-C /usr/local
# cd /usr/local/
# ln -sv mariadb-5.5.46-linux-x86_64 mysql
`mysql' -> `mariadb-5.5.46-linux-x86_64'
# cd mysql/
# chown root.mysql ./*

#将node1的mysql配置文件复制给node2
# scp /etc/mysql/my.cnf node2:/etc/mysql/
#准备服务脚本,并禁止开机自启
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# chkconfig mysqld off

#启动服务并查看共享数据库
# service mysqld start
# /usr/local/mysql/bin/mysql
Welcome to the MariaDB monitor.Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.46-MariaDB-log MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database         |
+--------------------+
| information_schema |
| mydb               |
| mysql            |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.02 sec)

MariaDB [(none)]> EXIT
Bye

#授权root用户远程访问mysql
# /usr/local/mysql/bin/mysql
Welcome to the MariaDB monitor.Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.46-MariaDB-log MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> grant all on *.* to 'root'@'192.168.%.%' identified by '123456';
Query OK, 0 rows affected (0.06 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)

# service mysqld stop




在node1和node2卸载共享目录

1
# umount /mydata





三、在heartbeat-gui界面配置
1、添加组资源,以及ip资源



2、添加共享文件系统资源



3、添加mysql-server资源


注意:定义的次序就是启动的次序。


页: [1]
查看完整版本: heatbeat-gui实现基于nfs的mysql高可用集群