1.这个环境最大的缺陷在于主机写入速度极慢,主键重复。
2.mysql最好采用5.6以上集群版本,5.5以下单线程版本不大适合。
博客中的mysql为5.5,实际测试挺差的,换成5.6之后十分完美,请各位运维在意一下。
环境:CentOS6.4 apache2.4.4 PHP5.3.27 MYSQL5.5.33
机器
vip 192.168.1.100
MASTER:192.168.1.101
BACKUP:192.168.1.102
高可用部分:
yum install ipvsadm
yum install keepalived
iptables –F
如果不允许禁用iptables
请在/etc/sysconfig/iptables里面加上
-A INPUT -d 224.0.0.18/16 -j ACCEPT (vrrp的组播)
-A INPUT -p vrrp -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEP
service iptables restart
setenforce 0
MASTER:
vi /etc/keepalived/keepalived.conf
global_defs {
notification_email {
barbytang@hotmail.com #邮件地址
}
router_id LVS1
}
vrrp_sync_group GLVS {
group {
mysqlha #组名
}
}
vrrp_instance mysqlha {
state MASTER (经过测试这里写MASTER或者BUCKUP对整个系统都没有影响)
interface eth0 (注意看当前工作网卡,我就有过遇到bond0的状况)
lvs_sync_daemon_inteface eth0
virtual_router_id 51
priority 180
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}
}
virtual_server 192.168.1.100 3306 { #mysql端口
delay_loop 6
lb_algo rr #LVS
lb_kind DR #LVS采用DR模式
persistence_timeout 20
protocol TCP
sorry_server 192.168.1.102 3306
real_server 192.168.1.101 3306 { #buckup机器也这么写,原因是因为让正常工作时只用101
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
BACKUP:
vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
barbytang@hotmail.com
}
router_id LVS1
}
vrrp_sync_group test {
group {
mysqlha
}
}
vrrp_instance mysqlha {
state BACKUP
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}
}
virtual_server 192.168.1.100 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 20
protocol TCP
sorry_server 192.168.1.102 3306
real_server 192.168.1.101 3306 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
注意:两个配置文件中,只有stat 和priority不一样 其他都一样
在MASTER和BUCKUP中都要添加realserver脚本:(注意给function执行权限)
realserver脚本:
vi /etc/rc.d/init.d/realserver.sh
#!/bin/bash
# description: Config realserver lo and apply noarp
SNS_VIP=192.168.1.100
/etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -host $SNS_VIP dev lo:0
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
route del $SNS_VIP >/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
——————分割线————————————
mysql主主同步:
主机A /etc/my.cnf
server-id = 1
log-bin=mysql-bin
binlog-do-db=utooo #同步的库,公司的库是utooo,有的服务器用的是chinamobile 注意修改
binlog-ignore-db=mysql #忽略的库
replicate-ignore-db=mysql,information_schema,test #同上,自己选择
#auto-increment-increment = 2
#auto-increment-offset = 1
log-slave-updates
slave-skip-errors=all
sync_binlog=1
注释行的作用是是否启用主键奇偶递增
主机B /etc/my.cnf
server-id = 2
log-bin=mysql-bin
binlog-do-db=utooo #同步的库,公司的库是utooo,有的服务器用的是chinamobile 注意修改
binlog-ignore-db=mysql
replicate-ignore-db=mysql,information_schema,test
#auto-increment-increment = 2
#auto-increment-offset = 2
log-slave-updates
slave-skip-errors=all
sync_binlog=1
在机器A和B上分别操作:
>#service mysql restart
>#mysql -uroot -p
mysql> GRANT REPLICATION SLAVE ON *.* TO 'forapp1'@'192.168.1.101' INDENTIFIED BY '123456'; #A和B分别添加注意主机名
mysql>flush privileges;
mysql>show master status\\G; #AB机器分别输入
*************************** 1. row ***************************
File: mysql-bin.000005
Position: 120
Binlog_Do_DB:
Binlog_Ignore_DB: mysql
Executed_Gtid_Set:
1 row in set (0.00 sec)
mysql> flush tables with read lock;
mysql> show master status\\G;
*************************** 1. row ***************************
File: mysql-bin.000005
Position: 120
Binlog_Do_DB:
Binlog_Ignore_DB: mysql
Executed_Gtid_Set:
1 row in set (0.00 sec)
#以上是两台机器输出的结果
若报错
#mysql> show master status;
#Empty set (0.00 sec)
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
#两台机器分别输入,注意都必须填写另一台机器上输出的内容
mysql> change master to master_host='192.168.1.101',master_user='forapp2',master_password='123456',master_log_file='mysql-bin.000007',master_log_pos=120;
mysql> flush privileges;
mysql> start slave;
mysql> show slave status\\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.101
Master_User: forapp2
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 204
Relay_Log_File: vipc-test2-relay-bin.000002
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql,information_schema
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 204
Relay_Log_Space: 545
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: eff28147-0906-11e3-bdb8-000c29477513
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 800
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
测试:
1.ipvsadm -ln 可以测试当前运行的是哪台机器的mysql
2.如果正在运行的是A,停掉A的mysql之后看看有没有自动切换到B
3.切换到B之后,启动A,看看是否会切回来
4.停掉A,在B中添加数据,启动A,检查数据是否同步(事实证明一定能同步)
5执行.tcpdump vrrp,从vip走的tcp数据应该是指向228.0.0.18vrrp的组播
五、处理vip无法被应用绑定
内核参数:
net.ipv4.ip_nonlocal_bind=1下载本文