系统环境:CentOS 6.5 mysql5.1.73 Keepalived v1.2.13
一:MYSQL主从配置
1.1 部署环境
主(master_mysql): 192.168.89.66 OS:CentOS 6.5
从(slave_mysql): 192.168.89.76 OS:CentOS 6.5
1.2 安装mysql
在两台机器上都安装mysql:
yum install mysql-server mysql mysql-devel
1.3 配置
1.3.1 主配置(master_mysql配置)
[root@mysql-ha1 sh]vim /etc/my.cnf
server-id=200 #设置主服务器的ID
innodb_flush_log_at_trx_commit=2 #
sync_binlog=1 #开启binlog日志同步功能
log-bin=mysql-bin-200 #binlog日志文件名
#这个表示只同步test库(如果没有此项,表示同步所有的库)
binlog-do-db=test
1.3.2 配置完后,重启主库的mysql
[root@mysql-ha1 sh]service mysqld resart;
[root@mysql-ha1 sh] mysql -uroot -p ##登录mysql
Enter password:
##授权给从数据库服务器192.168.89.76,用户名repl_user,密码slave@76
mysql>grant replication slave on *.* to 'repl_user'@'192.168.89.76' identified by 'slave@76';
##查看主库的状态 file,position这两个值很有用。要放到slave配置中
mysql>show master status ; ##内容如下:
+----------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+----------------------+----------+--------------+------------------+
| mysql-bin-200.000002 | 106 | test | |
+----------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
1.3.3 配置从库服务器
[root@mysql-ha2 sh]vim /etc/my.cnf
server-id=201 #设置主服务器的ID
innodb_flush_log_at_trx_commit=2 #
sync_binlog=1 #开启binlog日志同步功能
log-bin=mysql-bin-201 #binlog日志文件名
#这个表示只同步test库(如果没有此项,表示同步所有的库)
binlog-do-db=test
1.3.4 配置完后,重启从库的mysql
[root@mysql-ha2 sh]service mysqld resart;
[root@mysql-ha2 sh] mysql -uroot -p ##登录mysql
Enter password:
##授权给另一台数据库服务器192.168.89.66,用户名repl_user,密码slave@66
mysql>grant replication slave on *.* to 'repl_user'@'192.168.89.66' identified by 'slave@66';
##查看主库的状态 file,position这两个值很有用。要放到slave配置中
mysql>change master to master_host='192.168.89.66',master_user='repl_user',master_password='slave@66',
master_log_file='mysql-bin-200.000002' ,master_log_pos=106;
mysql> start slave; ##开启从库 (stop slave:关闭从库)
mysql> show slave status\G;
###Slave_IO_Running,Slave_SQL_Running 都为Yes的时候表示配置成功 内容如下图:
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.89.66
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin-200.000002
Read_Master_Log_Pos: 106
Relay_Log_File: mysqld-relay-bin.000009
Relay_Log_Pos: 255
Relay_Master_Log_File: mysql-bin-200.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
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: 106
Relay_Log_Space: 560
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:
1 row in set (0.00 sec;
ERROR:
No query specified
mysql>show master status\G
+----------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+----------------------+----------+--------------+------------------+
| mysql-bin-201.000002 | 734 | test | |
+----------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
1.3.5 配置完后到原主库中也启动同步功能
mysql>change master to master_host='192.168.89.76',master_user='repl_user',master_password='slave@76',
master_log_file='mysql-bin-200.000002' ,master_log_pos=734;
mysql> start slave; ##开启从库 (stop slave:关闭从库)
mysql> show slave status\G; ###Slave_IO_Running,Slave_SQL_Running 都为Yes的时候表示配置成功 内容如下所示:
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.89.67
Master_User: repl_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin-200.000002
Read_Master_Log_Pos: 106
Relay_Log_File: mysqld-relay-bin.000009
Relay_Log_Pos: 255
Relay_Master_Log_File: mysql-bin-201.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
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: 734
Relay_Log_Space: 560
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:
1 row in set (0.00 sec;
ERROR:
No query specified
1.3.5 验证同证功能量不是可以正确运行
在两边的库中都做数据的修改或新增,看另一个库是不会可以查到相同的数据.
二、keepalive高可用配置
2.1 keepalived安装
分别在master,slave上安装keepalived,两台服务器同时执行如下命令进行安将
yum -y install keepalived;
分别在master,slave上/root/sh目录下新建检查脚本,内容如下:
vi keepalived_check_mysql.sh
#!/bin/bash
MYSQL=/usr/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=root
CHECK_TIME=3
#mysql is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
MYSQL_OK=1
function check_mysql_helth (){
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status;" >/dev/null 2>&1
if [ $? = 0 ] ;then
MYSQL_OK=1
else
MYSQL_OK=0
fi
return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check_mysql_helth
if [ $MYSQL_OK = 1 ] ; then
CHECK_TIME=0
exit 0
fi
if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]
then
/etc/init.d/keepalived stop
exit 1
fi
sleep 1
done
保存后退出,并给其增加执行权限 chmod +x keepalived_check_mysql.sh
2.2、编写Keepalived的配置文件
vi /etc/keepalived/keepalived.conf并输入如下内容:
master配置内容:
###########################master##########################
! Configuration File for keepalived
global_defs {
router_id zsz
}
vrrp_script check_run {
script "/root/sh/check_mysql.sh"
interval 5
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state BACKUP #都设置成backup,这个当down机后重启不会抢占,会根据后面的priority自动处理
interface eth0
virtual_router_id 11
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_run
}
virtual_ipaddress {
192.168.89.86
}
}
###########################end##########################
slave配置内容:
###########################backup##########################
! Configuration File for keepalived
global_defs {
router_id zsz
}
vrrp_script check_run {
script "/root/sh/check_mysql.sh"
interval 5
}
vrrp_sync_group VG1 {
group {
VI_1
}
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 11
priority 90
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_run
}
virtual_ipaddress {
192.168.89.86
}
}
###########################end##########################
2.3、启动Keepalived测试
在主备机上分别执行如下命令:service keepalived start;
三:说明及问题处理:
3.1、防火墙需要放开,本文是直接关掉
3.2、如果check脚本有问题,执行不成功会引起无法绑定VIP
3.3、当请求并发量太大时,这种方案可能会导致自增长主键会冲突无法同步,这里需要人工介入处理,这里需要用跳跃主键的方法,主机配置为同步时加如下两个配置:
auto_increment_offset=1
auto_increment_increment=2
从库增加如下两个配置:
auto_increment_offset=2
auto_increment_increment=2
3.4、在做同步前,两个数据库内容要一致,且配置过程需要停服务或锁表
3.5、当执行show slave status \G时Slave_IO_Running: NO时,有可能是给的备份用户名和密码不一致,ip不对,或是主库没有启动。