mysql主主复制+keepalived故障切换

Wesley13
• 阅读 785

#服务器规划: mysql1:192.168.2.153 mysql2:192.168.2.154 mysql-vip:192.168.2.157 #主主复制:

  • 创建复制账号: 主库:mysql> grant replication slave on . to repl@'192.168.2.%' identified by 'slave'; 备库:mysql> grant replication slave on . to repl@'192.168.2.%' identified by 'slave';

  • 配置主库和备库:修改my.cnf

    #mysql1 [mysqld] server-id = 26 port = 3306 user = mysql basedir = /mvtech/mysql datadir = /mvtech/mysql/data tmpdir = /mvtech/mysql/ socket = /mvtech/mysql/mysql.sock skip-external-locking skip-name-resolve lower_case_table_names=1 auto_increment_offset = 1 auto_increment_increment = 2 ########## binlog ########## log_bin = /mvtech/mysql/log/mysql-bin binlog_format = row binlog_cache_size = 2M expire-logs-days = 7 ########## error log ########## log_error = /mvtech/mysql/log/mysql-error.log ########## slow log ########## slow_query_log = 1 slow_query_log_file = /mvtech/mysql/log/mysql-slow.log long_query_time = 5 ########## per_thread_buffers ########## max_connections = 1024 max_connect_errors = 1000 key_buffer_size = 64M max_allowed_packet = 128M table_open_cache = 6144 table_definition_cache = 4096 sort_buffer_size = 512K read_buffer_size = 512K join_buffer_size = 512K tmp_table_size = 64M max_heap_table_size = 64M thread_cache_size = 64 thread_concurrency = 32 bulk_insert_buffer_size = 64M ########innodb######## innodb_data_file_path = ibdata1:76M;ibdata2:1G:autoextend innodb_buffer_pool_size = 30G innodb_log_file_size = 500M innodb_log_buffer_size = 64M innodb_flush_log_at_trx_commit = 2 innodb_file_per_table = 1 innodb_file_io_threads = 4 innodb_flush_method = O_DIRECT innodb_thread_concurrency = 0 innodb_additional_mem_pool_size = 16M [mysqlhotcopy] interactive-timeout [mysqld_safe] open_files_limit = 65535

mysql

#mysql2:
[mysqld]
server-id = 27
port = 3306
user = mysql
basedir = /mvtech/mysql
datadir = /mvtech/mysql/data
tmpdir = /mvtech/mysql/
socket = /mvtech/mysql/mysql.sock
skip-external-locking
skip-name-resolve
lower_case_table_names=1
auto_increment_offset = 2
auto_increment_increment = 2 
########## binlog ##########
log_bin = /mvtech/mysql/log/mysql-bin
binlog_format = row
binlog_cache_size = 2M
expire-logs-days = 7
########## error log ##########
log_error = /mvtech/mysql/log/mysql-error.log
########## slow log ##########
slow_query_log = 1
slow_query_log_file = /mvtech/mysql/log/mysql-slow.log
long_query_time = 5
########## per_thread_buffers ##########
max_connections = 1024
max_connect_errors = 1000
key_buffer_size = 64M
max_allowed_packet = 128M
table_open_cache = 6144
table_definition_cache = 4096
sort_buffer_size = 512K
read_buffer_size = 512K
join_buffer_size = 512K
tmp_table_size = 64M
max_heap_table_size = 64M
thread_cache_size = 64
thread_concurrency = 32
bulk_insert_buffer_size = 64M
########innodb########
innodb_data_file_path = ibdata1:76M;ibdata2:1G:autoextend
innodb_buffer_pool_size = 30G
innodb_log_file_size = 500M
innodb_log_buffer_size = 64M
innodb_flush_log_at_trx_commit = 2
innodb_file_per_table = 1
innodb_file_io_threads = 4
innodb_flush_method = O_DIRECT
innodb_thread_concurrency = 0
innodb_additional_mem_pool_size = 16M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
open_files_limit = 65535


* 复制配置
        登陆mysql1:查看show binary logs;
        mysql2上操作:根据查询结果修改下面语句mysql2上操作:
            CHANGE MASTER TO MASTER_HOST='192.168.2.153',
            MASTER_USER='repl',
            MASTER_PASSWORD='slave',
            MASTER_LOG_FILE='mysql-bin.000006',
            MASTER_LOG_POS=0;
        查看状态:show slave status\G
            Slave_IO_Running: No
                Slave_SQL_Running: No

        启动slave
            start slave
        查看mysql2状态:show slave status\G
            Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
        查看mysql2线程:
        show processlist\G
        登陆mysql2:
        查看show binary logs;
        mysql> show binary logs;
        +------------------+-----------+
        | Log_name         | File_size |
        +------------------+-----------+
        | mysql-bin.000001 |       143 |
        | mysql-bin.000002 | 229383637 |
        | mysql-bin.000003 |       120 |
        +------------------+-----------+
        3 rows in set (0.00 sec)        


        mysql1操作:
            CHANGE MASTER TO MASTER_HOST='192.168.2.154',
            MASTER_USER='repl',
            MASTER_PASSWORD='slave',
            MASTER_LOG_FILE='mysql-bin.000003',
            MASTER_LOG_POS=120;    
        启动slave
            start slave
        查看mysql1状态:show slave status\G
            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
        查看mysql1线程:
        show processlist\G

#keepalived配置:

* mysql1:keepalived.conf
! Configuration File for keepalived

global_defs {
    router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP 
    interface em1
    virtual_router_id 61
    priority 100
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.2.157
    }
}

virtual_server 192.168.2.157 3306 {
    delay_loop 2
    lb_algo wrr
    lb_kind DR 
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 192.168.2.153 3306 {
        weight 3
        notify_down /mvtech/mysql/keepalive-mysql.sh
    TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}



* mysql2:keepalived.conf
! Configuration File for keepalived

global_defs {
    router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP 
    interface em1
    virtual_router_id 61
    priority 90
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.2.157
    }
}

virtual_server 192.168.2.157 3306 {
    delay_loop 2
    lb_algo wrr
    lb_kind DR 
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 192.168.2.154 3306 {
        weight 3
        notify_down /mvtech/mysql/keepalive-mysql.sh
    TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}



#keepalive-mysql.sh
#!/usr/bin/env bash
service keepalived stop

服务启动

  • 启动 1.启动mysql1上的mysql;登陆查看mysql-slave状态:show slave status\G;启动slave:start slave; 2.启动mysql2上的mysql;登陆查看mysql-slave状态:show slave status\G;启动slave:start slave; 3.启动mysql1上的keepalived;查看vip状态:ip addr 4.启动mysql2上的keepalived。
  • 关闭 1.查看vip漂在那台机器上,例如:在mysql1上; 2.关闭mysql2的keepalived; 3.关闭mysql1的keepalived; 4.关闭mysql1上的mysql; 5.关闭mysql2上的mysql。

keepalived模拟切换

  1. 关闭mysql1上的mysql,vip漂至mysql2主机上;
  2. 启动mysql1上的mysql同时启动keepalived,vip不漂回mysql1主机上,这是由于keepalive.conf 配置文件中将两台机器都设为backup,并增加nopreempt,此模式为不抢占模式。避免高并发网站频繁切换造成数据库数据不一致问题。

#mysql主主复制测试 mysql1和mysql2上均新建数据库,检查同步现象 #取消主主复制

mysql> show slave status\G
mysql> stop slave IO_THREAD;
mysql> stop slave;
mysql> reset master;
mysql> show slave status\G
mysql> reset slave all;

此时丛库已恢复至单机状态。

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Android蓝牙连接汽车OBD设备
//设备连接public class BluetoothConnect implements Runnable {    private static final UUID CONNECT_UUID  UUID.fromString("0000110100001000800000805F9B34FB");
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这