MySQL服務器:192.168.2.134,192.168.2.135html
MySQL主從同步的數據庫爲:chaosdbnode
實現目的:mysql
增長兩臺服務器(主備),經過LVS+Keepalived實現MySQL從庫讀操做負載均衡linux
架構規劃:c++
操做系統:CentOS 6.X 64位算法
LVS主服務器:192.168.2.133sql
LVS備服務器:192.168.2.132數據庫
LVS虛擬服務器(VIP):192.168.2.137vim
部署完成以後,經過VIP:192.168.21.254,根據LVS調度算法來訪問後端真實的MySQL從服務器,實現負載均衡。後端
第一部分:分別在兩臺MySQL服務器上操做,即在192.168.2.134,192.168.2.135,執行的操做都是分別執行
1、關閉SELINUX
vim /etc/selinux/config#編輯,添加如下代碼
#SELINUX=enforcing #註釋掉 #SELINUXTYPE=targeted #註釋掉 SELINUX=disabled #增長
setenforce 0 #使配置當即生效
2、配置防火牆,開啓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 ## 注意中間的是兩個英式小短線;重啓
3、綁定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
4、設置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.主主複製設置
一、兩臺機器上都創建用戶,並賦予複製權限: 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;
二、修改兩臺服務器上的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
三、重啓兩臺mysql服務器上的mysql服務
service mysqld restart
設置兩臺mysql互爲主從
四、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; 五、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/
六、如今就是要是測試是否是能夠相互複製了。
操做master1,卡看maste2有同步沒
操做master2,看看master2有同步沒
第二部分:分別在兩臺LVS主備服務器上操做,即192.168.2.13三、192.168.2.132
1、關閉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 ## 注意中間的是兩個英式小短線;重啓
2、安裝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
3、安裝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
4、安裝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!\n"; perl /tmp/test_perl.pl #運行測試腳本,若是出現:Hello,world! 說明perl安裝成功 perldoc DBI #查看DBI模塊是否安裝 perldoc DBD::mysql #查看DBD::mysql模塊是否安裝
5、配置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 } } }
6、設置主從(192.168.2.13三、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; } }