四、配置NFS服务
1.创建LVM逻辑卷(用来存放MySQL数据文件)
[root@nfs ~]# pvcreate /dev/sdb #创建物理卷
[root@nfs ~]# vgcreate myvg /dev/sdb #创建卷组
[root@nfs ~]# lvcreate -L 10G -n mydata myvg #创建逻辑卷
[root@nfs ~]# mke2fs -j /dev/myvg/mydata #格式化逻辑卷
[root@nfs ~]# lvs #查看逻辑卷
[root@nfs ~]# mkdir /mydata #创建挂载目录
[root@nfs ~]# mount /dev/myvg/mydata /mydata/ #挂载
[root@nfs ~]# cd /mydata/ #进入挂载目录
[root@nfs ~]# mkdir data #创建数据目录
2.创建mysql的用户与mysql组
node1,node2,nfs:(三个节点都要创建相同的用户与组)
(1).创建mysql组
[root@nfs ~]# groupadd -g 3306 mysql
(2).创建mysql用户
[root@nfs ~]# useradd -u 3306 -g mysql -s /sbin/nologin -M mysql
(3).查看
[root@nfs ~]# id mysql
uid=3306(mysql) gid=3306(mysql) groups=3306(mysql)
说明:在node1与node2也要创建相同的用户和组
3.修改数据目录的用户与组
[root@nfs ~]# chow -R mysql.mysql /mydata/data/
[root@nfs ~]# ll /mydata/
总计 12
drwxr-xr-x 6 mysql mysql 4096 08-12 13:40 data
4.修改nfs配置文件
[root@nfs ~]# vim /etc/exports
/mydata 192.168.18.0/24(no_root_squash,rw,async)
5.重新输出一下nfs
[root@nfs ~]# exportfs -arv
exporting 192.168.18.0/24:/mydata
6.查看输出的nfs共享存储
[root@nfs ~]# showmount -e 192.168.18.208
Export list for 192.168.18.208:
/mydata 192.168.18.0/24
7.测试挂载
node1:
[root@node1 ~]# mkdir /mydata
[root@node1 ~]# mount -t nfs 192.168.18.208:/mydata/ /mydata/
[root@node1 ~]# ll /mydata/
total 12
drwxr-xr-x 6 mysql mysql 4096 Aug 12 13:40 data
node2:
[root@node2~]# mkdir /mydata
[root@node2 ~]# mount -t nfs 192.168.18.208:/mydata/ /mydata/
[root@node2 ~]# ll /mydata/
total 12
drwxr-xr-x 6 mysql mysql 4096 Aug 12 13:50 data
五、安装与配置mysql
node1:
1.解压并链接mysql
[root@node1 ~]# tar -zxvf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local/ #直接解压到/usr/local目录下
[root@node1 ~]# ln -sv mysql-5.5.33-linux2.6-x86_64 mysql #设置一个软链接
[root@node1 ~]# cd /usr/local/mysql
[root@node1 mysql]# ll
total 204
drwxr-xr-x 2 root mysql 4096 Aug 11 14:45 bin
-rw-r--r-- 1 root mysql 17987 Jul 15 20:01 COPYING
drwxr-xr-x 3 root mysql 4096 Aug 12 13:11 data
drwxr-xr-x 2 root mysql 4096 Aug 11 14:45 docs
drwxr-xr-x 3 root mysql 4096 Aug 11 14:45 include
-rw-r--r-- 1 root mysql 134493 Jul 15 20:01 INSTALL-BINARY
drwxr-xr-x 3 root mysql 4096 Aug 11 15:27 lib
drwxr-xr-x 4 root mysql 4096 Aug 11 14:45 man
drwxr-xr-x 10 root mysql 4096 Aug 11 14:45 mysql-test
-rw-r--r-- 1 root mysql 2496 Jul 15 20:01 README
drwxr-xr-x 2 root mysql 4096 Aug 11 14:45 scripts
drwxr-xr-x 27 root mysql 4096 Aug 11 14:45 share
drwxr-xr-x 4 root mysql 4096 Aug 11 14:45 sql-bench
drwxr-xr-x 3 root mysql 4096 Aug 11 14:45 support-files
2.初始化mysql
[root@node1 ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/data/ --user=mysql
3.提供mysql配置文件
[root@node1 ~]# cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
[root@node1 ~]# vim /etc/my.cnf
datadir = /mydata/my.cnf #指定数据目录
innodb_file_per_table = 1 #innodb表单独表空间
4.提供mysql启动脚本
[root@node1 ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@node1 ~]# chmod +x /etc/init.d/mysqld
5.启动mysql
[root@node1 mysql]# service mysqld start
Starting MySQL... [ OK ]
6.查看数据目录
[root@node1 ~]# mount
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda3 on /data type ext3 (rw)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (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.18.208:/mydata/ on /mydata type nfs (rw,addr=192.168.18.208)
[root@node1 ~]# cd /mydata/data/
[root@node1 data]# ll
total 28784
-rw-rw---- 1 mysql mysql 18874368 Aug 12 13:40 ibdata1
-rw-rw---- 1 mysql mysql 5242880 Aug 12 14:27 ib_logfile0
-rw-rw---- 1 mysql mysql 5242880 Aug 12 08:05 ib_logfile1
-rw-r--r-- 1 root root 4721 Aug 12 07:55 my.cnf
drwx------ 2 mysql mysql 4096 Aug 12 08:07 mydb
drwx------ 2 mysql root 4096 Aug 12 07:39 mysql
-rw-rw---- 1 mysql mysql 126 Aug 12 13:40 mysql-bin.000001
-rw-rw---- 1 mysql mysql 19 Aug 12 13:18 mysql-bin.index
-rw-r----- 1 mysql root 18748 Aug 12 14:28 node1.test.com.err
-rw-rw---- 1 mysql mysql 6 Aug 12 14:27 node1.test.com.pid
drwx------ 2 mysql mysql 4096 Aug 12 07:39 performance_schema
drwx------ 2 mysql root 4096 Aug 12 07:39 test
7.登录测试一下
[root@node1 data]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.33-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.02 sec)
注:node1节点mysql配置完成,下面我们来配置node2
node2:
1.卸载数据文件目录
[root@node1 data]# service mysqld stop
Shutting down MySQL. [ OK ]
[root@node1 data]# cd
[root@node1 ~]# umount /mydata/
[root@node1 ~]# mount
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda3 on /data type ext3 (rw)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
2.挂载数据目录到node2上
[root@node2 ~]# mount
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda3 on /data type ext3 (rw)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
[root@node2 ~]# mount -t nfs 192.168.18.208:/mydata/ /mydata/
[root@node2 ~]# mount
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda3 on /data type ext3 (rw)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (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.18.208:/mydata/ on /mydata type nfs (rw,addr=192.168.18.208)
[root@node2 ~]# cd /mydata/data/
[root@node2 data]# ll
total 28780
-rw-rw---- 1 mysql mysql 18874368 Aug 12 14:30 ibdata1
-rw-rw---- 1 mysql mysql 5242880 Aug 12 14:30 ib_logfile0
-rw-rw---- 1 mysql mysql 5242880 Aug 12 08:05 ib_logfile1
-rw-r--r-- 1 root root 4721 Aug 12 07:55 my.cnf
drwx------ 2 mysql mysql 4096 Aug 12 08:07 mydb
drwx------ 2 mysql root 4096 Aug 12 07:39 mysql
-rw-rw---- 1 mysql mysql 126 Aug 12 13:40 mysql-bin.000001
-rw-rw---- 1 mysql mysql 19 Aug 12 13:18 mysql-bin.index
-rw-r----- 1 mysql root 19162 Aug 12 14:30 node1.test.com.err
-rw-r----- 1 mysql root 4442 Aug 12 13:40 node2.test.com.err
drwx------ 2 mysql mysql 4096 Aug 12 07:39 performance_schema
drwx------ 2 mysql root 4096 Aug 12 07:39 test
3.将配置文件与启动脚本复制到node2上
[root@node1 ~]# scp /etc/my.cnf node2:/etc/
my.cnf 100% 4721 4.6KB/s 00:00
[root@node1 ~]# scp /etc/init.d/mysqld node2:/etc/init.d/
mysqld 100% 11KB 10.6KB/s 00:00
4.启动mysql
[root@node2 data]# service mysqld start
Starting MySQL.. [ OK ]
[root@node2 data]# ll
total 28784
-rw-rw---- 1 mysql mysql 18874368 Aug 12 14:30 ibdata1
-rw-rw---- 1 mysql mysql 5242880 Aug 12 14:33 ib_logfile0
-rw-rw---- 1 mysql mysql 5242880 Aug 12 08:05 ib_logfile1
-rw-r--r-- 1 root root 4721 Aug 12 07:55 my.cnf
drwx------ 2 mysql mysql 4096 Aug 12 08:07 mydb
drwx------ 2 mysql root 4096 Aug 12 07:39 mysql
-rw-rw---- 1 mysql mysql 126 Aug 12 13:40 mysql-bin.000001
-rw-rw---- 1 mysql mysql 19 Aug 12 13:18 mysql-bin.index
-rw-r----- 1 mysql root 19162 Aug 12 14:30 node1.test.com.err
-rw-r----- 1 mysql root 5738 Aug 12 14:33 node2.test.com.err
-rw-rw---- 1 mysql mysql 5 Aug 12 14:33 node2.test.com.pid
drwx------ 2 mysql mysql 4096 Aug 12 07:39 performance_schema
drwx------ 2 mysql root 4096 Aug 12 07:39 test
5.登录并查看
[root@node2 data]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.33-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.01 sec)
6.停止mysql并卸载数据目录
[root@node2 data]# service mysqld stop
Shutting down MySQL. [ OK ]
[root@node2 data]# cd
[root@node2 ~]# umount /mydata/
[root@node2 ~]# mount
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda3 on /data type ext3 (rw)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
注:到这里mysql配置全部完成,下面我们来具体配置一下,高可用的mysql集群