1、配置两台Mysql主主同步
[root@master ~]# yum install mysql-server mysql -y
[root@master ~]# service mysqld start
[root@master ~]# mysqladmin -u root password 123.com
[root@master ~]# vi /etc/my.cnf  #在[mysqld]段添加
[mysqld]

server-id=1001  
log-bin = mysql-bin
relay-log=mysql-relay-bin
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=information_schema.%
binlog-ignore-db=mysql,information_schema
auto-increment-increment=2
auto-increment-offset=1
[root@master ~]# service mysqld restart


backup服务器设置

[root@backup ~]# vi /etc/my.cnf  #在[mysqld]段添加
[mysqld]

server-id=1002  
log-bin = mysql-bin
relay-log=mysql-relay-bin
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=information_schema.%
binlog-ignore-db=mysql,information_schema
auto-increment-increment=2
auto-increment-offset=2
[root@backup ~]# service mysqld restart



两台服务器查看bin-log日志和position位置

mysql> show master status;
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000004 |      345 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)

授权

mysql> grant replication slave on *.* to 'replication'@'%' identified by 'replication';

mysql> flush privileges;



mysql> change master to master_host='172.23.145.12', master_user='replication', master_password='replication', master_log_file='mysql-bin.000008', master_log_pos=58978938, MASTER_CONNECT_RETRY=10;


mysql> start slave;    #启动同步


mysql> change master to master_host='172.23.146.147', master_user='replication', master_password='replication', masterr_log_file='mysql-bin.000004', master_log_pos=345, MASTER_CONNECT_RETRY=10;


mysql> start slave;   


主主同步配置完毕,查看同步状态Slave_IO和Slave_SQL是YES说明主主同步成功。

show slave status /G;


   Slave_IO_Running: Yes

Slave_SQL_Running: Yes




配置keepalived实现热备


yum install -y pcre-devel openssl-devel popt-devel #安装依赖包


wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz

tar zxvf keepalived-1.2.7.tar.gz

cd keepalived-1.2.7

./configure --prefix=/usr/local/keepalived

make

make install


优化路径

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived  /etc/init.d/

cp /usr/local/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/
mkdir /etc/keepalived/

cp /usr/local/keepalived/etc/keepalived/keepalived.conf  /etc/keepalived/

cp /usr/local/keepalived/sbin/keepalived  /usr/sbin/


修改keepalived的配置文件


vi /etc/keepalived/keepalived.conf


! Configuration File for keepalived
global_defs {
   router_id Mysql_HA
}

vrrp_script  chk_mysql {
    script "killall -0 mysqld"
    interval 5
}

vrrp_instance mysql {
    state BACKUP
    interface eth0
    garp_master_delay 10
    virtual_router_id 53
    mcast_src_ip 172.23.146.147
    priority 100
    advert_int 1
    nopreempt

    authentication {
        auth_type PASS
        auth_pass hipiao
    }
    virtual_ipaddress {
        172.23.146.150
    }

track_script {
       chk_mysql
    }
}





写脚本

vi /usr/local/keepalived/mysql.sh

#!/bin/bash
pkill keepalived


chmod 755 /usr/local/keepalived/mysql.sh

/etc/init.d/keepalived start



backup服务器只修改priority为90、nopreempt不设置、real_server设置本地IP!


授权两台Mysql服务器允许root远程登录,用于在其他服务器登陆测试!

mysql> grant all on *.* to'root'@'%' identified by '123456';
mysql> flush privileges;



通过Mysql客户端通过VIP连接,看是否连接成功。

mysql -uroot -h172.23.146.150 -p


停止master这台mysql服务,是否能正常切换过去,可通过ip addr命令来查看VIP在哪台服务器上。