LVS+KEEPALIVED+MYSQL
单点写入读负载均衡主主同步高可用方案安装教程
一、 安装环境
Ø 软件版本
CENTOS 6.5
KEEPALIVED 1.2.12 | 下载
LVS 1.2.6 | 下载
MYSQL 5.6.17(社区版) | 下载
Ø 硬件环境
两台服务器, IP分别为:192.168.214.11(主机名:MYSQL-1) / 192.168.214.12(主机名MYSQL-2)
写VIP(单点): 192.168.214.13
读VIP(负载均衡): 192.168.214.14
二、 安装流程
1. OS安装与配置
最小化安装(安装时选择Minimal)CENTOS后,分别设置两台机器的IP与主机名
Ø 修改/etc/sysconfig/network文件
1 2 3 |
|
NETWORKING=yes HOSTNAME=MYSQL-1 #主机名 GATEWAY=192.168.0.1 #网关 |
Ø 修改/etc/sysconfig/network-scripts/ifcfg-eth0文件
1 2 3 4 5 6 7 8 9 |
DEVICE=eth0 HWADDR=00:0C:29:56:8F:AD TYPE=Ethernet UUID=ba48a4c0-f33d-4e05-98bd-248b01691c20 ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static IPADDR=192.168.0.231 #本机IP NETMASK=255.255.255.0 |
Ø 修改/etc/resolv.conf文件(设置DNS)
1 2 |
nameserver 202.103.24.68 #DNS服务器地址 search localdomain |
Ø 修改/etc/hosts文件,将圈出来的部分改为自己的主机名
Ø 重启网卡&查看当前主机名是否修改
1 2 |
[root@MYSQL-1 ~]# service network restart [root@MYSQL-1 ~]# hostname |
如果未修改成功,重启系统。
* 如果发现LINUX SSH连接缓慢,在server上/etc/ssh/sshd_config文件中加入UseDNS=no。然后/etc/init.d/sshd restart重启sshd进程使配置生效
2. MYSQL安装与配置
Ø 安装依赖
123 |
[root@MYSQL-1 ~]# yum -y install wget gcc gcc-c++ ncurses ncurses-devel openssl openssl-devel libtool* libaio .......省略安装过程打印日志 Complete! <--安装成功 |
Ø 检查MySQL及相关RPM包,是否安装,如果有安装,则移除
1 2 3 |
[root@MYSQL-1 ~]# rpm -qa | grep -i mysql mysql-libs-5.1.71-1.el6.x86_64 <--说明预装了依赖库 [root@MYSQL-1 ~]# yum -y remove mysql-libs* |
Ø 创建linux用户mysql
1 2 3 4 5 6 |
[root@MYSQL-1 ~]# useradd mysql [root@MYSQL-1 ~]# passwd mysql 更改用户 mysql 的密码 。 新的 密码: 重新输入新的 密码: passwd: 所有的身份验证令牌已经成功更新。
|
Ø 将mysql用户加入sudoers组(因为安装MYSQL需要ROOT权限)
1 2 3 4 5 6 7 |
[root@MYSQL-1 ~]# chmod u+w /etc/sudoers <--给与文件可修改权限 [root@MYSQL-1 ~]# vi /etc/sudoers ...... root ALL=(ALL) ALL <--找出此行 mysql ALL=(ALL) ALL <--添加到这里. :wq保存退出 ...... [root@MYSQL-1 ~]# chmod u-w /etc/sudoers <--撤销修改权限 |
Ø 将下载完的mysql包上传到服务器中,执行以下命令安装
12
3
4
5 |
[root@MYSQL-1 ~]# su mysql <--切换用户 [mysql@MYSQL-1 ~]$ tar -xvf MySQL-5.6.17-1.linux_glibc2.5.x86_64.rpm-bundle.tar <--解压缩 [mysql@MYSQL-1 ~]$ sudo rpm -ivh MySQL-server-5.6.17-1.linux_glibc2.5.x86_64.rpm [mysql@MYSQL-1 ~]$ sudo rpm -ivh MySQL-devel-5.6.17-1.linux_glibc2.5.x86_64.rpm [mysql@MYSQL-1 ~]$ sudo rpm -ivh MySQL-client-5.6.17-1.linux_glibc2.5.x86_64.rpm |
Ø 初始化MYSQL
1 2
3
4
|
[mysql@MYSQL-1 ~]$ sudo cp /usr/share/mysql/my-default.cnf /etc/my.cnf [mysql@MYSQL-1 ~]$ service mysql start <--启动MYSQL Starting MySQL. SUCCESS! [mysql@MYSQL-1 ~]$ sudo cat /root/.mysql_secret <--查看MYSQL初始密码 # The random password set for the root user at Tue Apr 8 22:20:49 2014 (local time): T_M57CrE5vM8WfEb(初始密码) [mysql@MYSQL-1 ~]$ /usr/bin/mysql_secure_installation <--初始化MYSQL |
Ø 设置开机自动启动
1 2 |
[mysql@MYSQL-1 ~]$ sudo chkconfig mysql on [sudo] password for mysql: |
Ø 修改MYSQL默认字符集为UTF-8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
[mysql@MYSQL-1 ~]$ sudo vi /etc/my.cnf ...... [client] default-character-set=utf8 [mysqld] character_set_server=utf8 character_set_client=utf8 collation-server=utf8_general_ci init_connect='SET NAMES utf8' #(注意linux下mysql安装完后是默认:表名区分大小写,列名不区分大小写; 0:区分大小写,1:不区分大小写) lower_case_table_names=1 #(设置最大连接数,默认为 151,MySQL服务器允许的最大连接数16384; ) max_connections=1000 [mysql] default-character-set=utf8 ...... sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES <-- 此行删除否则无法启动 [mysql@MYSQL-1 ~]$ service mysql restart |
Ø 检查字符集是否正确
1 2 3 4 |
[mysql@MYSQL-1 ~]$ mysql -uroot -p Enter password: mysql>show variables like '%collation%'; mysql>show variables like '%char%'; |
3. MYSQL主主复制配置
Ø 关闭防火墙,否则两台数据库无法连接
1 |
[root@MYSQL-1 ~]# service iptables stop |
Ø My.cnf配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
[mysql@MYSQL-1 ~]$ sudo vi /etc/my.cnf ...... [MySQLd] <--在该节点下增加以下内容
skip-name-resolve #禁用DNS解析,解决远程连接很慢的问题 log-bin=mysql-bin server-id=1 #两台机器server-id不能相同
auto_increment_increment=2 #自增长每次增加2 auto_increment_offset=1 #自增长从1开始, 另外一台设置为2 #两项保证两台主机中的自增长ID不会产生重复, 一台为奇数一台为偶数
#其他设置 #binlog-do-db=mydb #需要复制的数据库名,如果复制多个数据库,重复设置这个选项即可 #binlog-ignore-db=nonedb #不需要复制的数据库苦命,如果复制多个数据库,重复设置这个选项即可 #replicate-do-db=mysql #需要复制的数据库名,如果复制多个数据库,重复设置这个选项即可 #replicate-ignore-db=mysql #不需要复制的数据库苦命,如果复制多个数据库,重复设置这个选项即可 #log-slave-updates #启用从库日志,这样可以进行链式复制 #slave-skip-errors=all #不管发生什么错误,镜像处理工作也继续进行 ......
[mysql@MYSQL-1 ~]$ service mysql restart;
|
Ø 创建数据库用户并授权
1 2 3 |
mysql> create user sartner identified by '123'; mysql> grant all privileges on *.* to user sartner; mysql> flush privileges; |
Ø 数据复制配置
1 2 |
mysql> grant replication slave on *.* to 'sartner'@'%' identified by '123456'; mysql> show master status; |
记录下File 与Position, 在另一台数据库(MYSQL-2@192.168.214.12)中将此数据库设为主数据库
1 |
mysql> change master to master_host='192.168.214.11',master_user='sartner',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=120; |
同样在另一台计算机中按照次步骤执行,设置成互为主从(可以相互复制)
设置完毕后启动slave 并查看slave状态
1 2 |
mysql> start slave; mysql> show slave status\G; |
4. LVS+KEEPALIVED安装与配置
使用ROOT安装
Ø 安装LVS
12
|
[root@MYSQL-1 ~]# yum -y install kernel-devel ipvsadm [root@MYSQL-1 ~]# ln -sv /usr/src/kernels/2.6.32-431.11.2.el6-x86_64/ /usr/src/linux <--请自行修改kernels版本号 |
Ø 安装keepalived
1 2 3 4 5 6 7 8 9 10 11 12
|
[root@MYSQL-1 ~]# wget http://keepalived.org/software/keepalived-1.2.12.tar.gz [root@MYSQL-1 ~]# tar zxvf keepalived-1.2.12.tar.gz [root@MYSQL-1 ~]# cd keepalived-1.2.12 [root@MYSQL-1 ~]# ./configure --prefix=/usr/local/keepalived [root@MYSQL-1 ~]# make [root@MYSQL-1 ~]# make install [root@MYSQL-1 ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ [root@MYSQL-1 ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ [root@MYSQL-1 ~]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ [root@MYSQL-1 ~]# mkdir /etc/keepalived/
[root@MYSQL-1 ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ [root@MYSQL-1 ~]# cp /usr/local/keepalived/sbin/keepalived /etc/keepalived/ [root@MYSQL-1 ~]# chkconfig keepalived on
|
Ø Keepalived配置/etc/keepalived/keepalived.conf(两台服务器不同之处已用红色标出并注明)
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
|
global_defs { router_id LVS1 } vrrp_sync_group test { group { loadbalance } } vrrp_instance loadbalance { state MASTER #主机(192.168.214.11)设置为MASTER,备机(192.168.214.12)设置为BACKUP interface eth0 lvs_sync_daemon_inteface eth0 virtual_router_id 51 priority 180 #主机(192.168.214.11)设置为180,备机(192.168.214.12)设置为150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.214.13 #写VIP 192.168.214.14 #读VIP } }
#写VIP配置(主备) virtual_server 192.168.214.13 3306 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 20 protocol TCP sorry_server 192.168.214.12 3306 #备 real_server 192.168.214.11 3306 { #主 weight 3 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } }
#读VIP配置(负载均衡) virtual_server 192.168.214.14 3306 { delay_loop 6 lb_algo rr lb_kind DR # persistence_timeout 20 protocol TCP real_server 192.168.214.11 3306 { weight 3 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } real_server 192.168.214.12 3306 { weight 3 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } } |
Ø realserver脚本/etc/rc.d/init.d/realserver.sh
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 |
#!/bin/bash # description: Config realserver lo and apply noarp SNS_VIP=192.168.214.13 SNS_VIP2=192.168.214.14 /etc/rc.d/init.d/functions case "$1" in start) ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP ifconfig lo:1 $SNS_VIP2 netmask 255.255.255.255 broadcast $SNS_VIP2 /sbin/route add -host $SNS_VIP dev lo:0 /sbin/route add -host $SNS_VIP2 dev lo:1 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce sysctl -p >/dev/null 2>&1 echo "RealServer Start OK" ;; stop) ifconfig lo:0 down ifconfig lo:1 down route del $SNS_VIP >/dev/null 2>&1 route del $SNS_VIP2 >/dev/null 2>&1 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce echo "RealServer Stoped" ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0 |
Ø 分配可执行权限
1 2 |
[root@MYSQL-1 ~]# chmod 755 /etc/rc.d/init.d/realserver.sh [root@MYSQL-1 ~]# chmod 755 /etc/rc.d/init.d/functions |
Ø 启动realserver以及keepalived
1 2 |
[root@MYSQL-1 ~]# /etc/rc.d/init.d/realserver.sh start [root@MYSQL-1 ~]# /etc/keepalived/keepalived start |
Ø 查看keepalived日志
1 |
[root@MYSQL-1 ~]# cat /var/log/messages |
看到MYSQL-1为主,MYSQL-2为备则成功
三、 参考资料
MYSQL高可用探究(三)Lvs+Keepalived+Mysql单点写入读负载均衡主主同步高可用方案(本安装教程基于此方案)
http://blog.chinaunix.net/uid-20639775-id-3337471.html
Centos6.0系统lvs+keepalived+mysql实现mysql数据库热备主主复制
http://lansgg.blog.51cto.com/5675165/1180305
MYSQL主主(双主)复制
http://blog.csdn.net/jeffreynicole/article/details/8309451
嫌排版太烂可以下载PDF版本
http://download.csdn.net/detail/sartner/8245901