MySQL服务器:192.168.2.134,192.168.2.135
MySQL主从同步的数据库为:chaosdb
实现目的:
增加两台服务器(主备),通过LVS+Keepalived实现MySQL从库读操作负载均衡
架构规划:
操作系统:CentOS 6.X 64位
LVS主服务器:192.168.2.133
LVS备服务器:192.168.2.132
LVS虚拟服务器(VIP):192.168.2.137
部署完成之后,通过VIP:192.168.21.254,根据LVS调度算法来访问后端真实的MySQL从服务器,实现负载均衡。
第一部分:分别在两台MySQL服务器上操作,即在192.168.2.134,192.168.2.135,执行的操作都是分别执行
一、关闭SELINUX
vim /etc/selinux/config#编辑,添加以下代码
#SELINUX=enforcing #注释掉 #SELINUXTYPE=targeted #注释掉 SELINUX=disabled #增加
setenforce 0 #使配置立即生效
二、配置防火墙,开启3306端口
vi /etc/sysconfig/iptables #编辑,添加以下代码
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT #允许3306端口通过防火墙
/etc/init.d/iptables restart #重启防火墙使配置生效
或者直接关闭防火墙
#/etc/init.d/iptables status ## 查看防火墙状态
#/etc/init.d/iptable stop ## 本次关闭防火墙
#/etc/init.d/iptable restart ## 重启防火墙
永久性关闭防火墙:
#chkconfig --level 35 iptables off ## 注意中间的是两个英式小短线;重启
三、绑定LVS虚拟服务器(VIP):192.168.2.137 到lo:0
vim /etc/init.d/ivsrs #编辑,添加以下代码
#!/bin/bash # description: Config realserver lo and apply noarp #Written by :chaosju SNS_VIP=192.168.2.137 . /etc/rc.d/init.d/functions case "$1" in start) /sbin/ifconfig lo:0 $SNS_VIP broadcast $SNS_VIP netmask 255.255.255.255 up 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 echo "RealServer Start OK" ;; stop) /sbin/ifconfig lo:0 down 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
启动: /etc/init.d/ivsrs start
关闭: /etc/init.d/ivsrs stop
四、设置mysql主主复制和远程访问
参考博客 http://blog.chinaunix.net/uid-11582448-id-3026152.html
1.安装mysql
rpm -qa | grep mysql // 这个命令就会查看该操作系统上是否已经安装了mysql数据库 rpm -e mysql // 普通删除模式 rpm -e --nodeps mysql // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除 yum list | grep mysql //查看yum上提供的mysql数据库可下载的版本 yum install -y mysql-server mysql mysql-devel // 安装mysql时我们并不是安装了mysql客户端就相当于安装好了mysql数据库了,我们还需要安装mysql-server服务端才行
2.设置root密码
//第一次进入时,因为Root用户密码为空,输入: # mysql //可以直接进入 //设置Mysql root用户密码: # mysqladmin -u root password admin //有密码时进入Mysql: # mysql -u root -padmin
3.mysql默认文件路径
/etc/my.cnf:MySQL服务器配置文件。 /var/lib/mysql:MySQL数据目录,可以通过my.cnf修改。 /usr/bin/mysqlamdin:MySQL服务器命令行程序。 /usr/bin/mysql:MySQL客户端命令行程序。
4.主主复制设置
1、两台机器上都建立用户,并赋予复制权限: 192.168.2.132 mysql> CREATE USER 'ms1'@'%'; mysql> GRANT REPLICATION SLAVE ON *.* TO 'ms1'@'%' IDENTIFIED BY 'ms1'; 192.168.2.133 mysql> CREATE USER 'ms2'@'%'; mysql> GRANT REPLICATION SLAVE ON *.* TO 'ms2'@'%' IDENTIFIED BY 'ms2'; 或者直接赋予所有权限,最好赋予所有权限,keepalived需要远程访问mysql服务器 GRANT ALL ON *.* TO ms1@'%' IDENTIFIED BY 'ms1' WITH GRANT OPTION; GRANT ALL ON *.* TO ms1@'%' IDENTIFIED BY 'ms2' WITH GRANT OPTION;
2、修改两台服务器上的mysql配置文件 192.168.2.134 [mysqld] # for repl server-id = 1 log-bin = mysql-bin auto-increment-increment= 2 // 应设为整个结构中服务器的总数 auto-increment-offset = 1 // 设定数据库中自动增长的起点,避免两台服务器数据同步时出现主键冲突 192.168.2.135 [mysqld] # for repl server-id = 2 log-bin = mysql-bin auto-increment-increment= 2 auto-increment-offset = 1
3、重启两台mysql服务器上的mysql服务
service mysqld restart
设置两台mysql互为主从
4、master1上指定master2为主:
mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.2.132', -> MASTER_USER='ms2', -> MASTER_PASSWORD='ms2'; Query OK, 0 rows affected (0.19 sec) mysql> START SLAVE; 5、master2上指定master1为主: mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.2.133', -> MASTER_USER='ms1', -> MASTER_PASSWORD='ms1'; mysql> START SLAVE;
注意如果出现错误 ERROR 1200 (HY000): The server is not configured as slave; fix in config file or with CHANGE MASTER TO
解决办法 http://blog.itpub.net/27099995/viewspace-1294103/
6、现在就是要是测试是不是可以相互复制了。
操作master1,卡看maste2有同步没
操作master2,看看master2有同步没
第二部分:分别在两台LVS主备服务器上操作,即192.168.2.133、192.168.2.132
一、关闭SElinux、配置防火墙
vi /etc/sysconfig/iptables #编辑,添加以下代码
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT #允许3306端口通过防火墙
/etc/init.d/iptables restart #重启防火墙使配置生效
或者直接关闭防火墙
#/etc/init.d/iptables status ## 查看防火墙状态
#/etc/init.d/iptable stop ## 本次关闭防火墙
#/etc/init.d/iptable restart ## 重启防火墙
永久性关闭防火墙:
#chkconfig --level 35 iptables off ## 注意中间的是两个英式小短线;重启
二、安装lvs软件
安装参考 http://www.07net01.com/linux/ipvsadm_keepalivedanzhuangcuowujiejue_73067_1361863388.html
yum install libnl* libpopt* //安装依赖
安装 popt-static //安装依赖 http://www.linuxidc.com/Linux/2012-03/57386.htm
下载lvs,下载时候注意内核版版本 http://www.linuxvirtualserver.org/software/index.html
ln –s /usr/src/2.6.32-71.el6.x86_64 /usr/src/linux
cd ipvsadm-1.26
make && make install
三、安装keepalived
yum install gcc gcc-c++ make openssl-devel kernel-devel ncurses-devel #安装编译工具包
下载keeplived:http://www.keepalived.org/download.html
我放在/root目录下面
tar -zxvf keepalived-1.2.5.tar.gz
cd keepalived-1.2.5
./configure --sysconf=/etc --with-kernel-dir=/usr/src/kernels/2.6.32-573.1.1.el6.x86_64
make && make install
ln -s /usr/local/sbin/keepalived /sbin
四、安装perl、perl-DBI、DBD-mysql模块以及MySQL客户端 #perl脚本连接MySQL数据库需要,因为主备服务器检测mysql的状态是用per脚本写的
yum install perl perl-DBI perl-DBD-MySQL mysql #执行此命令安装
测试脚本是否安装成功
vi /tmp/test_perl.pl #!/usr/bin/perl print "Hello, world! "; perl /tmp/test_perl.pl #运行测试脚本,如果出现:Hello,world! 说明perl安装成功 perldoc DBI #查看DBI模块是否安装 perldoc DBD::mysql #查看DBD::mysql模块是否安装
五、配置keepalived
编辑keepalive的master(主)192.168.2.133配置文件 vim /etc/keepalived/keepalived.conf
global_defs { notification_email { chaosju@163.com } notification_email_from keepalived@chaos smtp_server 127.0.01 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.2.137 } } virtual_server 192.168.2.137 3306 { delay_loop 6 # 每隔6秒查看realserver状态 lb_algo lc # 调度算法为加权最小连接数 lb_kind DR # lvs工作模式为DR(直接路由)模式 nat_mask 255.255.255.0 persistence_timeout 10 # 同一IP 的连接50秒内被分配到同一台realserver(测试时建议改为0) protocol TCP # 用TCP监测realserver的状态 real_server 192.168.2.134 3306 { # 定义realserver weight 3 # 定义权重 MISC_CHECK { misc_path "/etc/keepalived/check_slave.pl 192.168.2.134" misc_dynamic } } real_server 192.168.2.135 3306 { weight 3 MISC_CHECK { misc_path "/etc/keepalived/check_slave.pl 192.168.2.135" misc_dynamic } } }
编辑keepalive的slave(从)192.168.2.132配置文件 vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { notification_email { chaosju@163.com } notification_email_from keepalived@chaos smtp_server 127.0.01 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 80 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.2.137 } } virtual_server 192.168.2.137 3306 { delay_loop 6 # 每隔6秒查看realserver状态 lb_algo lc # 调度算法为加权最小连接数 lb_kind DR # lvs工作模式为DR(直接路由)模式 nat_mask 255.255.255.0 persistence_timeout 10 # 同一IP 的连接50秒内被分配到同一台realserver(测试时建议改为0) protocol TCP # 用TCP监测realserver的状态 real_server 192.168.2.134 3306 { # 定义realserver weight 3 # 定义权重 MISC_CHECK { misc_path "/etc/keepalived/check_slave.pl 192.168.2.134" misc_dynamic } } real_server 192.168.2.135 3306 { weight 3 MISC_CHECK { misc_path "/etc/keepalived/check_slave.pl 192.168.2.135" misc_dynamic } } }
六、设置主从(192.168.2.133、192.168.2.132)监控mysql脚本check_slave.pl
vi /etc/keepalived/check_slave.pl #编辑,添加以下代码
#!/usr/bin/perl -w use DBI; use DBD::mysql; $SBM = 120; $db = "chaosdb"; $host = $ARGV[0]; $port = 3306; $user = "root"; $pw = "admin"; $query = "show slave status"; $dbh = DBI->connect("DBI:mysql:$db:$host:$port", $user, $pw, { RaiseError => 0,PrintError => 0 }); if (!defined($dbh)) { exit 1; } $sqlQuery = $dbh->prepare($query); $sqlQuery->execute; $Slave_IO_Running = ""; $Slave_SQL_Running = ""; $Seconds_Behind_Master = ""; while (my $ref = $sqlQuery->fetchrow_hashref()) { $Slave_IO_Running = $ref->{'Slave_IO_Running'}; $Slave_SQL_Running = $ref->{'Slave_SQL_Running'}; $Seconds_Behind_Master = $ref->{'Seconds_Behind_Master'}; } $sqlQuery->finish; $dbh->disconnect(); if ( $Slave_IO_Running eq "No" || $Slave_SQL_Running eq "No" ) { exit 1; } else { if ( $Seconds_Behind_Master > $SBM ) { exit 1; } else { exit 0; } }