标签:mysql 集群
部署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 { 732233048@qq.com } notification_email_from root@localhost 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 { 732233048@qq.com } notification_email_from root@localhost 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 = root@localhost Sep 28 10:53:40 localhost Keepalived_vrrp[22750]: Email notification = 732233048@qq.com 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 = root@localhost Sep 28 10:53:40 localhost Keepalived_healthcheckers[22749]: Email notification = 732233048@qq.com 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 = root@localhost Sep 28 10:56:16 localhost Keepalived_vrrp[21541]: Email notification = 732233048@qq.com 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 = root@localhost Sep 28 10:56:17 localhost Keepalived_healthcheckers[21540]: Email notification = 732233048@qq.com 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进行写操作:
##查看其它库是否同步
本文出自 “见” 博客,请务必保留此出处http://732233048.blog.51cto.com/9323668/1698684
标签:mysql 集群
原文地址:http://732233048.blog.51cto.com/9323668/1698684