部署mysql高可用、读写分离集群

架构图:

部署集群:

主master和备master安装DRBD:

http://732233048.blog.51cto.com/9323668/1665979

主master和备master安装heartbeat和mysql:

http://732233048.blog.51cto.com/9323668/1670068

注意:图中 主master与备master之间采用主主复制(不是主从复制)

部署集群:

安装mysql:(主master,备master,salve1,slave2)

##Atlas要求mysql版本必须是5.1以上,这里建议安装mysql5.6
##mysql5.6软件下载地址:http://pan.baidu.com/s/1bnrzpZh
##mysql5.6采用cmake安装
yum -y install make gcc gcc-c++ cmake bison-devel  ncurses-devel kernel-devel readline-devel pcre-devel openssl-devel openssl zlib zlib-devel pcre-devel perl perl-devel   #安装依赖包
cd /usr/local/src/
tar -zxf mysql-5.6.22.tar.gz
cd mysql-5.6.22
mkdir -p /opt/mysql/data        #数据目录
cmake  -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/opt/mysql/data -DSYSCONFDIR=/usr/local/mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1  -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock  -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci  -DEXTRA_CHARSETS:STRING=utf8,gbk  -DWITH_DEBUG=0
make              ##时间会很久
make install
groupadd mysql
useradd -s /sbin/nologin -g mysql mysql
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/opt/mysql/data --defaults-file=/usr/local/mysql/my.cnf --user=mysql
chown -R mysql.mysql /opt/mysql
##修改配置文件:
mv /usr/local/mysql/my.cnf /usr/local/mysql/my.cnf.old
vi /usr/local/mysql/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/mysql/data
port = 3306
pid-file = /opt/mysql/data/mysql.pid
socket = /var/lib/mysql/mysql.sock
default_storage_engine = InnoDB
expire_logs_days = 14
max_binlog_size = 5G
binlog_cache_size = 10M
max_binlog_cache_size = 20M
slow_query_log
long_query_time = 2
slow_query_log_file = /opt/mysql/data/slow.log
open_files_limit = 65535
innodb = FORCE
innodb_buffer_pool_size = 400M
innodb_log_file_size = 1G
query_cache_size = 0
thread_cache_size = 64
table_definition_cache = 512
table_open_cache = 512
max_connections = 1000
sort_buffer_size = 10M
max_allowed_packet = 6M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[client]
socket = /var/lib/mysql/mysql.sock
port = 3306

##innodb_buffer_pool_size:
    主要作用是缓存innodb表的索引,数据,插入数据时的缓冲;
    默认值:128M;
    专用mysql服务器设置此值的大小: 系统内存的70%-80%最佳。
    如果你的系统内存不大,查看这个参数,把它的值设置小一点吧(若值设置大了,启动会报错)
##启动mysql:
cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
/etc/init.d/mysqld start
netstat -tlnp | grep mysql     ##查看是否启动

vi /etc/profile         ##修改path路径
##在最后添加:export PATH=$PATH:/usr/local/mysql/bin
source /etc/profile
##创建mysql密码:
mysqladmin -u root password "123456"

主,备master安装keepalived:

##参考文档:
    http://blog.csdn.net/jibcy/article/details/7826158
    http://bbs.nanjimao.com/thread-855-1-1.ht

(主master,备master)

cd /usr/local/src/
wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz
tar -zxf keepalived-1.2.15.tar.gz
cd keepalived-1.2.15
./configure --prefix=/usr/local/keepalived
make
make install

##拷贝文件:
cp -a /usr/local/keepalived/etc/rc.d/init.d/keepalived  /etc/init.d/
cp -a /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived/
cp -a /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp -a /usr/local/keepalived/sbin/keepalived /usr/sbin/

##注意: /etc/sysconfig/keepalived 和 /etc/keepalived/keepalived.conf 的路径一定要正确,
##因为在执行/etc/init.d/keepalived这个启动脚本时,会读取/etc/sysconfig/keepalived 和 /etc/keepalived/keepalived.conf 这两个文件

(主master)

##修改配置:
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.old
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
    notification_email {
      [email protected]
    }
    notification_email_from [email protected]
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id mysql_ha
}

vrrp_instance VI_1 {
    state master
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    #nopreempt

    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.247.201
    }
}

virtual_server 192.168.247.201 3306 {
    delay_loop 6
    #lb_algo wrr
    #lb_kind DR
    #persistence_timeout 50
    protocol TCP
    real_server 192.168.247.134 3306 {
        #weight 3
        notify_down /etc/keepalived/killkeepalived.sh
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}

(备master)

##修改配置:
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.old
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
    notification_email {
      [email protected]
    }
    notification_email_from [email protected]
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id mysql_ha
}

vrrp_instance VI_1 {
    state backup
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    #nopreempt

    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.247.201
    }
}

virtual_server 192.168.247.201 3306 {
    delay_loop 6
    #lb_algo wrr
    #lb_kind DR
    #persistence_timeout 50
    protocol TCP
    real_server 192.168.247.135 3306 {
        #weight 3
        notify_down /etc/keepalived/killkeepalived.sh
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}

(主master,备master)

##创建/etc/keepalived/killkeepalived.sh脚本:
vi /etc/keepalived/killkeepalived.sh
#!/bin/bash
# check mysql server status

mysql_status=`/etc/init.d/mysqld status | grep -c SUCCESS`

if [ $mysql_status -eq 0 ];then
  /etc/init.d/mysqld start
  sleep 1
  mysql_status=`/etc/init.d/mysqld status | grep -c SUCCESS`
  if [ $mysql_status -eq 0 ];then
    /etc/init.d/keepalived stop
  fi
fi
chmod 755 /etc/keepalived/killkeepalived.sh

(主master,备master)

##修改keepalived的日志文件:
       ##参考:http://chenwenming.blog.51cto.com/327092/745316
       ##说明:
       ##centos6.3之后的syslog改名叫rsyslog了,默认在 /etc/rsyslog.conf

##修改/etc/sysconfig/keepalived:
vi /etc/sysconfig/keepalived
# Options for keepalived. See `keepalived --help‘ output and keepalived(8) and
# keepalived.conf(5) man pages for a list of all options. Here are the most
# common ones :
#
# --vrrp               -P    Only run with VRRP subsystem.
# --check              -C    Only run with Health-checker subsystem.
# --dont-release-vrrp  -V    Dont remove VRRP VIPs & VROUTEs on daemon stop.
# --dont-release-ipvs  -I    Dont remove IPVS topology on daemon stop.
# --dump-conf          -d    Dump the configuration data.
# --log-detail         -D    Detailed log messages.
# --log-facility       -S    0-7 Set local syslog facility (default=LOG_DAEMON)
#

#KEEPALIVED_OPTIONS="-D"
KEEPALIVED_OPTIONS="-D -d -S 0"               ##在最后添加此行

##修改/etc/rsyslog.conf:
vi /etc/rsyslog.conf
##在最后添加此行:
local0.*                                                /var/log/keepalived.log

## /etc/init.d/rsyslog restart
Shutting down system logger:                               [  OK  ]
Starting system logger:                                    [  OK  ]

(主master)

##启动keepalived:
/etc/init.d/keepalived start
##查看日志:
tail -f /var/log/keepalived.log
Sep 28 10:53:40 localhost Keepalived[22746]: Starting Keepalived v1.2.15 (09/28,2015)
Sep 28 10:53:40 localhost Keepalived[22747]: Starting Healthcheck child process, pid=22749
Sep 28 10:53:40 localhost Keepalived[22747]: Starting VRRP child process, pid=22750
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]: Initializing ipvs 2.6
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]: Netlink reflector reports IP 192.168.247.134 added
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]: Netlink reflector reports IP fe80::20c:29ff:fe8f:73ef added
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]: Registering Kernel netlink reflector
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]: Registering Kernel netlink command channel
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]: Registering gratuitous ARP shared channel
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]: Opening file ‘/etc/keepalived/keepalived.conf‘.
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]: Configuration is using : 63258 Bytes
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]: ------< Global definitions >------
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]:  Router ID = mysql_ha
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]:  Smtp server = 127.0.0.1
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]:  Smtp server connection timeout = 30
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]:  Email notification from = [email protected]
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]:  Email notification = [email protected]
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]:  VRRP IPv4 mcast group = 224.0.0.18
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]:  VRRP IPv6 mcast group = 224.0.0.18
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]: ------< VRRP Topology >------
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]:  VRRP Instance = VI_1
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]:    Want State = BACKUP
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]:    Runing on device = eth0
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]:    Gratuitous ARP repeat = 5
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]:    Gratuitous ARP refresh repeat = 1
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]:    Virtual Router ID = 51
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]:    Priority = 150
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]:    Advert interval = 1sec
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]:    Authentication type = SIMPLE_PASSWORD
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]:    Password = 1111
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]:    Virtual IP = 1
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]:      192.168.247.201/32 dev eth0 scope global
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]: Using LinkWatch kernel netlink reflector...
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]: VRRP_Instance(VI_1) Entering BACKUP STATE
Sep 28 10:53:40 localhost Keepalived_vrrp[22750]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]: Netlink reflector reports IP 192.168.247.134 added
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]: Netlink reflector reports IP fe80::20c:29ff:fe8f:73ef added
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]: Registering Kernel netlink reflector
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]: Registering Kernel netlink command channel
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]: Opening file ‘/etc/keepalived/keepalived.conf‘.
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]: Configuration is using : 11691 Bytes
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]: IPVS: Scheduler or persistence engine not found
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]: IPVS: No such process
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]: ------< Global definitions >------
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:  Router ID = mysql_ha
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:  Smtp server = 127.0.0.1
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:  Smtp server connection timeout = 30
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:  Email notification from = [email protected]
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:  Email notification = [email protected]
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:  VRRP IPv4 mcast group = 224.0.0.18
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:  VRRP IPv6 mcast group = 224.0.0.18
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]: ------< SSL definitions >------
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:  Using autogen SSL context
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]: ------< LVS Topology >------
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:  System is compiled with LVS v1.2.1
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:  VIP = 192.168.247.201, VPORT = 3306
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:    delay_loop = 6, lb_algo = 
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:    protocol = TCP
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:    alpha is OFF, omega is OFF
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:    quorum = 1, hysteresis = 0
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:    lb_kind = NAT
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:    RIP = 192.168.247.134, RPORT = 3306, WEIGHT = 1
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:      -> Notify script DOWN = /etc/keepalived/killkeepalived.sh
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]: ------< Health checkers >------
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:  [192.168.247.134]:3306
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:    Keepalive method = TCP_CHECK
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:    Connection dest = [192.168.247.134]:3306
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]:    Connection timeout = 10
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]: Using LinkWatch kernel netlink reflector...
Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]: Activating healthchecker for service [192.168.247.134]:3306
Sep 28 10:53:43 localhost Keepalived_vrrp[22750]: VRRP_Instance(VI_1) Transition to MASTER STATE
Sep 28 10:53:44 localhost Keepalived_vrrp[22750]: VRRP_Instance(VI_1) Entering MASTER STATE
Sep 28 10:53:44 localhost Keepalived_vrrp[22750]: VRRP_Instance(VI_1) setting protocol VIPs.
Sep 28 10:53:44 localhost Keepalived_healthcheckers[22749]: Netlink reflector reports IP 192.168.247.201 added
Sep 28 10:53:44 localhost Keepalived_vrrp[22750]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.247.201
Sep 28 10:53:49 localhost Keepalived_vrrp[22750]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.247.201

##启动正常
##检测134
##vip 201绑定

(备master)

##启动:
/etc/init.d/keepalived start
##查看日志:
tail -f /var/log/keepalived.log 
Sep 28 10:56:16 localhost Keepalived[21538]: Starting Keepalived v1.2.15 (09/28,2015)
Sep 28 10:56:16 localhost Keepalived[21539]: Starting Healthcheck child process, pid=21540
Sep 28 10:56:16 localhost Keepalived[21539]: Starting VRRP child process, pid=21541
Sep 28 10:56:16 localhost Keepalived_healthcheckers[21540]: Initializing ipvs 2.6
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]: Netlink reflector reports IP 192.168.247.135 added
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]: Netlink reflector reports IP fe80::20c:29ff:fe2c:ace5 added
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]: Registering Kernel netlink reflector
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]: Registering Kernel netlink command channel
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]: Registering gratuitous ARP shared channel
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]: Opening file ‘/etc/keepalived/keepalived.conf‘.
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]: Configuration is using : 63258 Bytes
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]: ------< Global definitions >------
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]:  Router ID = mysql_ha
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]:  Smtp server = 127.0.0.1
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]:  Smtp server connection timeout = 30
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]:  Email notification from = [email protected]
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]:  Email notification = [email protected]
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]:  VRRP IPv4 mcast group = 224.0.0.18
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]:  VRRP IPv6 mcast group = 224.0.0.18
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]: ------< VRRP Topology >------
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]:  VRRP Instance = VI_1
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]:    Want State = BACKUP
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]:    Runing on device = eth0
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]:    Gratuitous ARP repeat = 5
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]:    Gratuitous ARP refresh repeat = 1
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]:    Virtual Router ID = 51
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]:    Priority = 100
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]:    Advert interval = 1sec
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]:    Authentication type = SIMPLE_PASSWORD
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]:    Password = 1111
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]:    Virtual IP = 1
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]:      192.168.247.201/32 dev eth0 scope global
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]: Using LinkWatch kernel netlink reflector...
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]: VRRP_Instance(VI_1) Entering BACKUP STATE
Sep 28 10:56:16 localhost Keepalived_vrrp[21541]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]: Netlink reflector reports IP 192.168.247.135 added
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]: Netlink reflector reports IP fe80::20c:29ff:fe2c:ace5 added
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]: Registering Kernel netlink reflector
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]: Registering Kernel netlink command channel
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]: Opening file ‘/etc/keepalived/keepalived.conf‘.
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]: Configuration is using : 11691 Bytes
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]: IPVS: Scheduler or persistence engine not found
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]: IPVS: No such process
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]: ------< Global definitions >------
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:  Router ID = mysql_ha
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:  Smtp server = 127.0.0.1
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:  Smtp server connection timeout = 30
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:  Email notification from = [email protected]
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:  Email notification = [email protected]
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:  VRRP IPv4 mcast group = 224.0.0.18
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:  VRRP IPv6 mcast group = 224.0.0.18
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]: ------< SSL definitions >------
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:  Using autogen SSL context
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]: ------< LVS Topology >------
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:  System is compiled with LVS v1.2.1
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:  VIP = 192.168.247.201, VPORT = 3306
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:    delay_loop = 6, lb_algo = 
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:    protocol = TCP
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:    alpha is OFF, omega is OFF
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:    quorum = 1, hysteresis = 0
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:    lb_kind = NAT
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:    RIP = 192.168.247.135, RPORT = 3306, WEIGHT = 1
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:      -> Notify script DOWN = /etc/keepalived/killkeepalived.sh
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]: ------< Health checkers >------
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:  [192.168.247.135]:3306
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:    Keepalive method = TCP_CHECK
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:    Connection dest = [192.168.247.135]:3306
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]:    Connection timeout = 10
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]: Using LinkWatch kernel netlink reflector...
Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]: Activating healthchecker for service [192.168.247.135]:3306

##启动正常
##检测135

查看vip绑定在哪台机器上:

(主master)

# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:8f:73:ef brd ff:ff:ff:ff:ff:ff
    inet 192.168.247.134/24 brd 192.168.247.255 scope global eth0
    inet 192.168.247.201/32 scope global eth0
    inet6 fe80::20c:29ff:fe8f:73ef/64 scope link 
       valid_lft forever preferred_lft forever
       
##vip 201绑定

(备master)

# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2c:ac:e5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.247.135/24 brd 192.168.247.255 scope global eth0
    inet6 fe80::20c:29ff:fe2c:ace5/64 scope link 
       valid_lft forever preferred_lft forever
       
##未绑定vip

测试主,备之间的keepalived:

测试一:

主master把mysql服务stop掉:

/etc/init.d/mysqld stop

##查看日志:
Sep 28 11:02:29 localhost Keepalived_healthcheckers[22749]: TCP connection to [192.168.247.134]:3306 failed !!!
Sep 28 11:02:29 localhost Keepalived_healthcheckers[22749]: Removing service [192.168.247.134]:3306 from VS [192.168.247.201]:3306
Sep 28 11:02:29 localhost Keepalived_healthcheckers[22749]: IPVS: Service not defined
Sep 28 11:02:29 localhost Keepalived_healthcheckers[22749]: Executing [/etc/keepalived/killkeepalived.sh] for service [192.168.247.134]:3306 in VS [192.168.247.201]:3306
Sep 28 11:02:29 localhost Keepalived_healthcheckers[22749]: Lost quorum 1-0=1 > 0 for VS [192.168.247.201]:3306
Sep 28 11:02:29 localhost Keepalived_healthcheckers[22749]: Remote SMTP server [127.0.0.1]:25 connected.
Sep 28 11:02:30 localhost Keepalived_healthcheckers[22749]: SMTP alert successfully sent.
Sep 28 11:02:41 localhost Keepalived_healthcheckers[22749]: TCP connection to [192.168.247.134]:3306 success.
Sep 28 11:02:41 localhost Keepalived_healthcheckers[22749]: Adding service [192.168.247.134]:3306 to VS [192.168.247.201]:3306
Sep 28 11:02:41 localhost Keepalived_healthcheckers[22749]: IPVS: Service not defined
Sep 28 11:02:41 localhost Keepalived_healthcheckers[22749]: Gained quorum 1+0=1 <= 1 for VS [192.168.247.201]:3306
Sep 28 11:02:41 localhost Keepalived_healthcheckers[22749]: Remote SMTP server [127.0.0.1]:25 connected.
Sep 28 11:02:42 localhost Keepalived_healthcheckers[22749]: SMTP alert successfully sent.

##mysql服务stop后,keepalived又把mysql启动起来了

测试二:

主master把keepalived服务stop掉:

 /etc/init.d/keepalived stop
##主master日志:
Sep 28 11:05:12 localhost Keepalived[22747]: Stopping Keepalived v1.2.15 (09/28,2015)
Sep 28 11:05:12 localhost Keepalived_vrrp[22750]: VRRP_Instance(VI_1) sending 0 priority
Sep 28 11:05:12 localhost Keepalived_vrrp[22750]: VRRP_Instance(VI_1) removing protocol VIPs.
Sep 28 11:05:12 localhost Keepalived_healthcheckers[22749]: Removing service [192.168.247.134]:3306 from VS [192.168.247.201]:3306
Sep 28 11:05:12 localhost Keepalived_healthcheckers[22749]: IPVS: Service not defined
Sep 28 11:05:12 localhost Keepalived_healthcheckers[22749]: IPVS: No such service

##移除vip 201
##备master日志:
Sep 28 11:05:13 localhost Keepalived_vrrp[21541]: VRRP_Instance(VI_1) Transition to MASTER STATE
Sep 28 11:05:14 localhost Keepalived_vrrp[21541]: VRRP_Instance(VI_1) Entering MASTER STATE
Sep 28 11:05:14 localhost Keepalived_vrrp[21541]: VRRP_Instance(VI_1) setting protocol VIPs.
Sep 28 11:05:14 localhost Keepalived_healthcheckers[21540]: Netlink reflector reports IP 192.168.247.201 added
Sep 28 11:05:14 localhost Keepalived_vrrp[21541]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.247.201
Sep 28 11:05:19 localhost Keepalived_vrrp[21541]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.247.201

##绑定vip 201

测试三:

把主master的mysql服务stop掉,并不再让它起来

测试四:

把备master的mysql服务stop

测试五:

把备master的keepalived服务stop

测试六:

把备master的mysql服务stop掉,并不再让它起来

注意:一定要保证此时的vip 201绑定在主master上,才继续进行下面的主从复制配置

配置mysql的主从复制:

(主master)

##开启二进制文件:
vi /usr/local/mysql/my.cnf
在[mysqld]下添加:
server-id = 1
log-bin = /opt/mysql/binlog/mysql-binlog      ##二进制文件最好单独放在一个目录下

mkdir /opt/mysql/binlog/                   ##创建日志目录
chown -R mysql.mysql /opt/mysql
/etc/init.d/mysqld  restart        ##必须是restart

(备master)

vi /usr/local/mysql/my.cnf
在[mysqld]下添加:
server-id = 2

/etc/init.d/mysqld  restart        ##必须是restart

(slave1)

vi /usr/local/mysql/my.cnf
在[mysqld]下添加:
server-id = 3

/etc/init.d/mysqld  restart        ##必须是restart

(slave2)

vi /usr/local/mysql/my.cnf
在[mysqld]下添加:
server-id = 4

/etc/init.d/mysqld  restart        ##必须是restart

(主master)

##给每台从库创建一个数据库账号(授权)
mysql -uroot -p123456
grant replication slave on *.* to ‘slave‘@‘192.168.247.135‘ identified by ‘123456‘;
grant replication slave on *.* to ‘slave‘@‘192.168.247.136‘ identified by ‘123456‘;
grant replication slave on *.* to ‘slave‘@‘192.168.247.137‘ identified by ‘123456‘;
flush privileges;

(主master)

##备份数据:
mysql -uroot -p123456
flush tables with read lock;         #锁表,只读
show master status;                  #查看此时的binlog位置和pos值,这个要记录下来
+---------------------+----------+--------------+------------------+-------------------+
| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| mysql-binlog.000001 |      838 |              |                  |                   |
+---------------------+----------+--------------+------------------+-------------------+

打开另外一个终端:mysqldump -u root -p123456 --all-databases > /tmp/mysqldump.sql
回到之前终端:unlock tables;         #解表
##拷贝数据到所有从库:
scp /tmp/mysqldump.sql 192.168.247.135:/tmp/
scp /tmp/mysqldump.sql 192.168.247.136:/tmp/
scp /tmp/mysqldump.sql 192.168.247.137:/tmp/

(备master)

##导入数据:
mysql -uroot -p123456 < /tmp/mysqldump.sql

##开始同步:
mysql -uroot -p123456

##注意:这里指定的ip是主master的真实ip,不是vip##
change master to master_host=‘192.168.247.134‘,master_user=‘slave‘,master_password=‘123456‘,master_log_file=‘mysql-binlog.000001‘,master_log_pos=838,master_port=3306;
start slave;         ##启动slave
show slave status\G;            ##查看状态,两个YES则正常
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.247.134
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-binlog.000001
          Read_Master_Log_Pos: 838
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 286
        Relay_Master_Log_File: mysql-binlog.000001
             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: 838
              Relay_Log_Space: 459
              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: d4e9615d-65dd-11e5-a5c7-000c298f73ef
             Master_Info_File: /opt/mysql/data/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: 86400
                  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)

ERROR: 
No query specified

(slave1,slave2)

##导入数据:
mysql -uroot -p123456 < /tmp/mysqldump.sql

##开始同步:
mysql -uroot -p123456

##注意:这里指定的ip是vip 201##
change master to master_host=‘192.168.247.201‘,master_user=‘slave‘,master_password=‘123456‘,master_log_file=‘mysql-binlog.000001‘,master_log_pos=838,master_port=3306;
start slave;         ##启动slave
show slave status\G;            ##查看状态,两个YES则正常
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.247.134
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-binlog.000001
          Read_Master_Log_Pos: 838
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 286
        Relay_Master_Log_File: mysql-binlog.000001
             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: 838
              Relay_Log_Space: 459
              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: d4e9615d-65dd-11e5-a5c7-000c298f73ef
             Master_Info_File: /opt/mysql/data/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: 86400
                  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)

ERROR: 
No query specified

主,备master之间配置mysql主主复制:

(备master)

##开启二进制文件:
vi /usr/local/mysql/my.cnf
在[mysqld]下添加:
log-bin = /opt/mysql/binlog/mysql-binlog      ##二进制文件最好单独放在一个目录下(且与主master的日志名一致)

mkdir /opt/mysql/binlog/                   ##创建日志目录
chown -R mysql.mysql /opt/mysql
/etc/init.d/mysqld  restart        ##必须是restart

(主master)

##给主master创建一个数据库账号(授权)
mysql -uroot -p123456
grant replication slave on *.* to ‘slave‘@‘192.168.247.134‘ identified by ‘123456‘;
flush privileges;

(备master)

mysql -uroot -p123456
show master status;        ##记录日志文件和pos值
+---------------------+----------+--------------+------------------+-------------------+
| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| mysql-binlog.000001 |      120 |              |                  |                   |
+---------------------+----------+--------------+------------------+-------------------+
1 row in set (0.07 sec)

(主master)

##开始同步:
change master to master_host=‘192.168.247.135‘,master_user=‘slave‘,master_password=‘123456‘,master_log_file=‘mysql-binlog.000001‘,master_log_pos=120;
start slave;     ##启动slave
show slave status\G;         ##两个YES,正常
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.247.135
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-binlog.000001
          Read_Master_Log_Pos: 120
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 286
        Relay_Master_Log_File: mysql-binlog.000001
             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: 120
              Relay_Log_Space: 459
              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: 2
                  Master_UUID: df0bb90c-65e0-11e5-a5db-000c292cace5
             Master_Info_File: /opt/mysql/data/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: 86400
                  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)

ERROR: 
No query specified

测试mysql的主从复制:

测试一:

在主master进行写操作:

##查看其它所有从库是否同步

测试二:

把主master的keepalived服务stop掉,并在备master进行写操作:

##查看其它库是否同步
时间: 2024-10-14 20:54:56

部署mysql高可用、读写分离集群的相关文章

【视频分享】基于MyCat的MySQL高可用读写分离集群

# [视频分享]基于MyCat的MySQL高可用读写分离集群 ## 获取方式 **方式一:****链接:**[百度网盘](https://pan.baidu.com/s/137KFcoCE-i75vA8FE_OYFQ)==关注公众号极客萧(xiaoyxyj),并且回复关键字:mycat 即可获取下载链接和提取码(注意大小写别错)====如果链接失效,请及时联系我== 原文地址:https://www.cnblogs.com/icefirebug/p/11784738.html

MySQL高可用读写分离集群视频课程 MyCat教程

课程大纲:第1节MySQL源码编译安装(CentOS-6.6+MySQL-5.6)38分钟第2节MySQL主从复制的配置(CentOS-6.6+MySQL-5.6)58分钟第3节MyCat在MySQL主从复制基础上实现读写分离-0130分钟第4节MyCat在MySQL主从复制基础上实现读写分离-0219分钟第5节MyCat集群部署(HAProxy+MyCat)65分钟第6节MyCat高可用负载均衡集群实现(HAProxy+Keepalived+MyCat)51分钟 下载地址:百度网盘下载    

MySQL-MMM实现MySQL高可用读写分离

1.实验环境 1.1.实验拓扑 1.2.主机配置环境说明 主机名 IP地址 角色/用途 MySQL Server_ID master1 192.168.80.101 MySQL主节点,可读写操作 11 master2 192.168.80.102 MySQL备节点,可读写操作 12 slave1 192.168.80.103 MySQL从节点,仅能读 13 slave2 192.168.80.104 MySQL从节点,仅能读 14 monitor 192.168.80.105 MMM管理端 无

MMM架构实现MySQL高可用读写分离(进阶版,包含Amoeba)

前两天逛博客偶然发现了某大神写的关于MMM的文章,看完就迫不及待的自己试了一下,大神写的很顺畅,以为自己也能操作的很顺畅,但是实际情况是手脚太不麻利,碰到很多坑,如果大神的文章是"平地起高楼",那我这篇则是"平房起别墅",因为我生产环境中已经有两台Mysql做双主同步,这篇文章说的是如何改造升级现有架构! 环境介绍: IP 操作系统 数据库 软件 角色/VIP 192.168.6.109 CentOS7.2 Mysql5.6-34 mysql-mmm2.2.1-15

MySQL高可用MHA集群

MHA 简介 MHA(Master High Availability)它由日本DeNA公司youshimaton开发,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件.在MySQL故障切换过程中,MHA能做到在0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,MHA能在最大程度上保证数据的一致性,以达到真正意义上的高可用.MHA软件由两部分组成:MHA Manager(管理节点)和MHA Node(数据节点).MHA Manager可以单独部署在一台

企业中MySQL高可用集群架构三部曲之MM+keepalived

各位老铁们,老张与大家又见面了.看到各位在博客里面给我的留言和访问量的情况,我很是欣慰,也谢谢大家对我的认可.我写这些博客,就是想把自己对于MySQL数据库的一些看法和自己平时的实战经验分享出来,我们可以一起探讨,共同进步.也保证今后只要一有空就更新博文,推出更多的干货. 我的学生经常对我说:"张老师,每次我遇到报错,有时还是会百度,但是最烦的是不知道百度哪篇帖子说的是正确的".其实这些呢,都是因为自己还没有对MySQL数据库核心知识的不熟悉,和对技术掌握的不牢固.平时下得功夫还是不到

MySQL高可用架构-MHA环境部署记录

一.MHA介绍 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Facebook公司)开发,是日本的一位 MySQL专家采用Perl语言编写的一个脚本管理工具,该工具仅适用于MySQLReplication(二层)环境,目的在于维持Master主库的高可用性.是一套优秀的作为MySQL高可用性 环境下故障切

mysql高可用集群方案

这里有一篇关于Mysql高可用方案的干货文章:[干货分享] 一文了解数据库高可用容灾方案的设计与实现 网友们公司中的使用方案讨论:想问各位大大 MySQL 是怎么做高可用的? 一.Mysql高可用解决方案 方案一:共享存储 一般共享存储采用比较多的是 SAN/NAS 方案. 方案二:操作系统实时数据块复制 这个方案的典型场景是 DRBD,DRBD架构(MySQL+DRBD+Heartbeat) 方案三:主从复制架构 主从复制(一主多从) MMM架构(双主多从) MHA架构(多主多从) 方案四:数

# IT明星不是梦 # MySQL高可用集群之MMM

MySQL高可用集群之MMM 一.MMM简介 MMM即Multi-Master Replication Manager for MySQL(mysql多主复制管理器),基于perl实现,关于mysql主主复制配置的监控.故障转移和管理的一套可伸缩的脚本套件(在任何时候只有一个节点可以被写入),MMM也能对从服务器进行读负载均衡,所以可以用它来在一组用于复制的服务器启动虚拟IP,除此之外,它还有实现数据备份.节点之间重新同步功能的脚本.MySQL本身没有提供replication failover