基于Corosync和NFS服务器实现MySQL的高可用
规划:
准备工作:根据规划图,设置三台主机的主机名和地址
(这里不再给出具体过程)
一、配置nfs服务器
#fdisk /dev/sda 创建一个大小为20G 类型为8e的分区 本文为/dev/sda5 #partprobe /dev/sda #pvcreate /dev/sda5 #vgcreate myvg /dev/sda5 #lvcreate -L 10G -n mydata myvg #mke2fs -j -L MYDATA /dev/myvg/mydata #mkdir /mydata #vim /etc/fstab 添加 LABEL=MYDATA /mydata ext3 defaults 0 0 #mount -a 添加用户 # groupadd -g 306 -r mysql # useradd -g mysql -r -u 306 -s /sbin/nologin mysql # id mysql uid=306(mysql) gid=306(mysql) groups=306(mysql) context=root:system_r:unconfined_t:SystemLow-SystemHigh 让mysql用户对/mydata目录有读写的权限 # chown -R mysql:mysql /mydata/ 通过nfs共享/mydata #vim /etc/exports /mydata 172.16.220.11(rw,no_root_squash) 172.16.220.12(rw,no_root_squash) #service nfs start # showmount -e localhost Export list for localhost:
/mydata 172.16.220.12,172.16.220.11
二、配置mysql的高可用
1 配置node1 node2的时间同步和ssh互联
node1: #hwclock -s #ssh-keygen -i rsa #vim /etc/hosts 172.16.220.11 node1 172.16.220.12 node2 # ssh-copy-id -i .ssh/id_rsa.pub root@node2 # ssh node2 'ifconfig' node2: #hwclock -s #ssh-keygen -i rsa #vim /etc/hosts 172.16.220.11 node1 172.16.220.12 node2 # ssh-copy-id -i .ssh/id_rsa.pub root@node1 # ssh node1 'ifconfig'
2 添加用户
node1 node2: # groupadd -r -g 306 mysql # useradd -g mysql -u 306 -r mysql # mkdir /mydata # mount -t nfs 172.16.220.22:/mydata /mydata/ # ls /mydata/ lost+found 验证mysql用户是否对/mydata目录有写的权限 # su - mysql su: warning: cannot change directory to /home/mysql: No such file or directory -bash-3.2$ cd /mydata -bash-3.2$ mkdir data
//创建这个目录,作为mysql的数据目录-bash-3.2$ ls data lost+found
3 安装mysql
node1 : 安装 mysql-5.5.22-linux2.6-i686.tar.gz # tar xf mysql-5.5.22-linux2.6-i686.tar.gz -C /usr/local/ # cd /usr/local/ # ln -sv mysql-5.5.22-linux2.6-i686 mysql #cd mysql 初始化mysql #chown -R mysql:mysql . # scripts/mysql_install_db --user=mysql --datadir=/mydata/data/ 提供配置文件 # cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf #vim /etc/my.cnf [mysqld]中修改 thread_concurrency = 2 datadir = /mydata/data 提供脚本 # cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld # chkconfig --add mysqld # service mysqld start Starting MySQL........ [ OK ] # /usr/local/mysql/bin/mysql 连上mysql 停止服务 #service mysqld stop # chkconfig mysqld off # chkconfig --list mysqld //保证都是off node2:
安装 mysql-5.5.22-linux2.6-i686.tar.gz # tar xf mysql-5.5.22-linux2.6-i686.tar.gz -C /usr/local/ # cd /usr/local/ # ln -sv mysql-5.5.22-linux2.6-i686 mysql #cd mysql #chown -R root:mysql # cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf #vim /etc/my.cnf [mysqld]中修改 thread_concurrency = 2 datadir = /mydata/data # cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld # chkconfig --add mysqld # service mysqld start Starting MySQL........ [ OK ] # /usr/local/mysql/bin/mysql 连上mysql #service mysqld stop # chkconfig mysqld off # chkconfig --list mysqld //保证都是off 到此两个mysql服务器可以实现nfs的共享存储了