** MySQL主主+Keepalived

**MySQL+DRBD+Heartbeat

在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用mysql主主方案,一主多从,读写分离等,但是单主存在单点故障,从库切换成主库需要作改动。因此,如果是双主或者多主,就会增加mysql入口,增加高可用。不过多主需要考虑自增长ID问题,这个需要特别设置配置文件,比如双主,可以使用奇偶,总之,主之间设置自增长ID相互不冲突就能完美解决自增长ID冲突问题。

主主方案实现思路

1、 两台mysql都可读写,互为主备。默认只使用一台masterA负责数据的写入,另一台masterB备用处于备用状态;

2、 masterA是masterB的主库,masterB又是masterA的主库,它们互为主从;

3、 两台主库之间做高可用,可以采用keepalived等方案,使用VIP对外提供服务;

4、所有提供服务的从服务器与masterB进行主从同步(双主多从);

5、建议采用高可用策略的时候,masterA或masterB均不因宕机恢复后而抢占VIP(非抢占模式);

这样做可以在一定程度上保证主库的高可用,在一台主库down掉之后,可以在极短的时间内切换到另一台主库上,尽可能减少主库宕机对业务造成的影响,减少了主从同步给生产主库带来的压力;

但是也有几个不足的地方:

  • masterB可能会一直处于空闲状态(可以用它当从库,负责部分查询);
  • 主库后面提供服务的从库要等masterB先同步完了数据后才能去masterB上去同步数据,这样可能会造成一定程度的同步延时;

设备两台MARIADB机器:

masterA:IP为192.168.200.124

masterB:IP为192.168.200.125

masterA:

[root@localhost ~]# yum install mariadb mariadb-server -y

[root@localhost ~]#systemc restart mariadb

1:修改主配置文件

[root@localhost ~]#vim /etc/my.cnf

server-id=
log-bin=mysql-binlog #打开二进制功能
log-slave-updates=true
max_binlog_size=1024M #binlog单文件最大值
auto_increment_offset =
auto_increment_increment = #奇数ID replicate-ignore-db = information_schema #忽略不同步主从的数据库
replicate-ignore-db = performance_schema
replicate-ignore-db = test
replicate-ignore-db = mysql max_connections =
max_connect_errors = skip-character-set-client-handshake #忽略应用程序想要设置的其他字符集
init-connect='SET NAMES utf8' #连接时执行的SQL
character-set-server=utf8 #服务端默认字符集
wait_timeout= #请求的最大连接时间
interactive_timeout= #和上一参数同时修改才会生效
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES #sql模式 relay-log=relay-log-bin #开启中继日志
relay-log-index=slave-relay-bin.index

MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.200.125' identified by '123456';

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show master status;
+---------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| mysql-binlog.000001 | 911 | | |
+---------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

2:测试环境,可以保证没数据写入。否则需要的步骤是:先masterA锁表-->masterA备份数据-->masterA解锁表 -->masterB导入数据-->masterB设置主从-->查看主从

MariaDB [(none)]> stop slave;

Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [(none)]> change master to master_host='192.168.200.125',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-binlog.000004',master_log_pos=670;

Query OK, 0 rows affected (0.04 sec)

 
MariaDB [(none)]> start slave;

MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.125
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-binlog.000004
Read_Master_Log_Pos: 670
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 624
Relay_Master_Log_File: mysql-binlog.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: information_schema,performance_schema,test,mysql
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: 670
Relay_Log_Space: 916
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
1 row in set (0.00 sec)

ERROR: No query specified

masterB:

[root@localhost ~]# yum install mariadb mariadb-server -y

[root@localhost ~]#systemc restart mariadb

1:修改主配置文件

[root@localhost ~]#vim /etc/my.cnf

server-id       =
log-bin=mysql-binlog
log-slave-updates=true
max_binlog_size=1024M
auto_increment_offset =
auto_increment_increment = #偶数ID replicate-ignore-db = information_schema
replicate-ignore-db = performance_schema
replicate-ignore-db = test
replicate-ignore-db = mysql max_connections =
max_connect_errors = skip-character-set-client-handshake
init-connect='SET NAMES utf8'
character-set-server=utf8
wait_timeout=
interactive_timeout=
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index

MariaDB [(none)]> grant replication slave on *.* to 'repl'@'192.168.200.124' identified by '123456';

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> show master status;
+---------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| mysql-binlog.000004 | 670 | | |
+---------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

2:测试环境,可以保证没数据写入。否则需要的步骤是:先masterA锁表-->masterA备份数据-->masterA解锁表 -->masterB导入数据-->masterB设置主从-->查看主从

MariaDB [(none)]> stop slave;

Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> change master to master_host='192.168.200.111',master_port=3306,master_user='repl',master_password='123456',master_log_file='mysql-binlog.000001',master_log_pos=911;

Query OK, 0 rows affected (0.13 sec)

MariaDB [(none)]> start slave;

Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.124
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-binlog.000001
Read_Master_Log_Pos: 911
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 624
Relay_Master_Log_File: mysql-binlog.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: information_schema,performance_schema,test,mysql
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: 911
Relay_Log_Space: 916
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
1 row in set (0.00 sec)

ERROR: No query specified

masterA测试:

MariaDB [(none)]> create database test01;

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| client |
| cmx |
| db_test |
| liuxiang |
| mydb |
| mysql |
| performance_schema |
| shiqi |
| shiqishou |
| shuifei |
| test01 |

| test02 |
| var |
| yg |
| yqx |
+--------------------+
16 rows in set (0.00 sec)

masterB测试:

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cmx |
| db_test |
| liuxiang |
| mysql |
| performance_schema |
| shiqi |
| shiqishou |
| test01 |
| yqx |
+--------------------+
11 rows in set (0.00 sec)

MariaDB [(none)]> create database test02;

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cmx |
| db_test |
| liuxiang |
| mysql |
| performance_schema |
| shiqi |
| shiqishou |
| test01 |

| test02 |
| yqx |

MYSQL主主高可用方案:

masterA:

[root@localhost ~]# yum -y install keepalived

[root@localhost ~]# vim /etc/keepalived/keepalived.conf 

! Configuration File for keepalived

global_defs {

   router_id LVS_MASTER-A

}

vrrp_script mysql {                      

    script "/opt/mysql.sh"

    interval 

    weight -                 

    fall                  

    rise 

}

vrrp_instance VI_1 {

    state BACKUP

    interface ens33

    virtual_router_id 

priority 

nopreempt

    advert_int 

    authentication {

        auth_type PASS

        auth_pass 

    }

    track_script {

        mysql

    }

    virtual_ipaddress {

        192.168.200.254

    }

}

[root@localhost ~]# vim /opt/mysql.sh

#!/bin/bash

counter=$(netstat -na|grep "LISTEN"|grep ""|wc -l)

if [ "${counter}" -eq  ]; then

    systemctl stop keepalived

fi

[root@localhost ~]# chmod +x /opt/mysql.sh

[root@localhost ~]# systemctl start keepalived

[root@localhost ~]# ip a | grep ens33

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

inet 192.168.200.124/24 brd 192.168.200.255 scope global ens33

inet 192.168.200.254/32 scope global ens33

masterB:

[root@localhost ~]# yum -y install keepalived

[root@localhost ~]# vim /etc/keepalived/keepalived.conf 

! Configuration File for keepalived

global_defs {

   router_id LVS_MASTER-B

}

vrrp_script mysql {

    script "/opt/mysql.sh"

    interval 

    weight -                 

    fall                  

    rise 

}

vrrp_instance VI_1 {

    state BACKUP

    interface ens33

    virtual_router_id 

    priority 

    advert_int 

    authentication {

        auth_type PASS

        auth_pass 

    }

    track_script {

        mysql

    }

    virtual_ipaddress {

        192.168.200.254

    }

}

[root@localhost ~]# vim /opt/mysql.sh

#!/bin/bash

counter=$(netstat -na|grep "LISTEN"|grep ""|wc -l)

if [ "${counter}" -eq  ]; then

    systemctl stop keepalived

fi

[root@localhost ~]# chmod +x /opt/mysql.sh

[root@localhost ~]# systemctl start keepalived

[root@localhost ~]# ip a | grep ens33

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

inet 192.168.200.125/24 brd 192.168.200.255 scope global ens33

inet 192.168.200.254/32 scope global ens33

测试VIP的漂移:

masterA:

[root@localhost ~]# systemctl stop mariadb

[root@localhost ~]# ip a | grep ens33

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

inet 192.168.200.124/24 brd 192.168.200.255 scope global ens33

masterB:

[root@localhost ~]# ip a | grep ens33

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

inet 192.168.200.125/24 brd 192.168.200.255 scope global ens33

inet 192.168.200.254/32 scope global ens33

远程客户端测试:

mysql服务器授权

[root@localhost ~]#systemctl start mariadb

[root@localhost ~]# mysql -uroot -p123123

MariaDB [(none)]> grant all on *.* to 'root'@'192.168.200.%' identified by '123456';             #授权200网段的客户机通过密码登入mysql

MariaDB [(none)]> flush privileges;                                                                                            #刷新授权列表

通过vip登陆测试:masterA与masterB都需要关闭防火墙

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0

[root@localhost ~]# mysql -uroot -p123456 -h 192.168.200.254                                             #通过授权用户及密码登录Mysql

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 5796

Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test               |

| test01             |

| test02             |

+--------------------+

6 rows in set (0.00 sec)

MTSQL主主同步方案的更多相关文章

  1. MySQL高可用(一)主备同步:MySQL是如何保证主备一致的

    主备同步,也叫主从复制,是MySQL提供的一种高可用的解决方案,保证主备数据一致性的解决方案. 在生产环境中,会有很多不可控因素,例如数据库服务挂了.为了保证应用的高可用,数据库也必须要是高可用的. ...

  2. MySQL双主(主主)架构方案

    在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用mysql主从方案,一主多从,读写分离等,但是单主存在单点故障,从库切换成主库需要作改动.因此,如果是双主或者多主,就会增加mysql入 ...

  3. MySQL 主主同步配置和主从配置步骤

    ★预备知识 : 1.双机热备 对于双机热备这一概念,我搜索了很多资料,最后,还是按照大多数资料所讲分成广义与狭义两种意义来说. 从广义上讲,就是对于重要的服务,使用两台服务器,互相备份,共同执行同一服 ...

  4. 搭建MySQL的主从、半同步、主主复制架构

    复制其最终目的是让一台服务器的数据和另外的服务器的数据保持同步,已达到数据冗余或者服务的负载均衡.一台主服务器可以连接多台从服务器,并且从服务器也可以反过来作为主服务器.主从服务器可以位于不同的网络拓 ...

  5. mysql高可用架构方案之中的一个(keepalived+主主双活)

    Mysql双主双活+keepalived实现高可用           文件夹 1.前言... 4 2.方案... 4 2.1.环境及软件... 4 2.2.IP规划... 4 2.3.架构图... ...

  6. mysql主主同步

    Mysql 主主同步方案 第一台机器主 [root@master ~]# vim /etc/my.cnf [mysqld] server-id=1 log-bin=mysql-binlog log-s ...

  7. mysql数据库负载均衡高可用之主从、主主备份,实时同步

    一:MySQL Replication 什么是MySQL Replication Replication可以实现将数据从一台数据库服务器(master)复制到一或多台数据库服务器(slave) 默认情 ...

  8. 4.MySQL 主主(m-m) 同步生产库标准同步操作实施流程

    通过MySQL参数配置使用主主前提: 1.表的主键自增. ################################################################# #m1-m ...

  9. MySQL数据的主从复制、半同步复制和主主复制详解

    一.MySQL复制概述 ⑴.MySQL数据的复制的基本介绍 目前MySQL数据库已经占去数据库市场上很大的份额,其一是由于MySQL数据的开源性和高性能,当然还有重要的一条就是免费~不过不知道还能免费 ...

随机推荐

  1. 并查集-G - 食物链

    G - 食物链 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A.现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种 ...

  2. SQL Server经典sql语句大全(转)

    一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份数 ...

  3. c# DPI SCale

    public class Screen { /// Primary Screen #region Win32 API [DllImport("user32.dll")] stati ...

  4. webpack4.41.0配置一(基础配置webpack文件,入口出口,实现打包)

    1.查看node.js版本.npm版本和webpack版本(使用webpack4时,请确保node.js的版本>=8.9.4) 2.我先重新卸载了webpack和webpack-cli(全局) ...

  5. 求int型正整数在内存中存储时1的个数 && int型的数到底最大是多少?

    输入一个int型的正整数(十位数之内!嘞!),计算出该int型数据在内存中存储时1的个数. #include<bits/stdc++.h> using namespace std; int ...

  6. 【Thinkphp】记录一次分页的实现

    thinkphp分页非常简单 1,控制器渲染数据: $studentList = StudentDb::paginate(5); $this->view->assign('list',$s ...

  7. C# 程序集(Assembly)

    程序集 程序集是代码进行编译是的一个逻辑单元,把相关的代码和类型进行组合,然后生成PE文件.程序集只是逻辑上的划分,一个程序集可以只由一个文件组成,也可由多个文件组成.不管是单文件程序集还是多文件程序 ...

  8. Spark编程基础_RDD初级编程

    摘要:Spark编程基础_RDD初级编程 RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素 ...

  9. mysql存储表情报错

    数据库版本:mysql  Ver 8.0.16 数据库字符集:utf8 原因:mysql的utf8编码的一个字符最多3个字节,但是一个emoji表情为4个字节,所以utf8不支持存储emoji表情.但 ...

  10. Some series and integrals involving the Riemann zeta function binomial coefficients and the harmonic numbers

    链接:http://pan.baidu.com/s/1eSNkz4Y