mysql hma 分布式_mysql基础之mariadb集群双主(主主)架构

一、概念

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

单点故障解决方案:

主主架构:

互为主备,互相监控对方二进制日志文件进行同步

note:当两个sql语句发生冲突的时候主主架构有可能出现数据不一致的现象;

MHA(master high availability):

HMA可以有多个配置文件,一个配置文件监控一个主从架构

二、主主架构思路

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

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

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

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

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

三、演示步骤

环境(主1服务器端IP:192.168.11.7;主2服务器端IP:192.168.11.8)

1、修改配置文件,配置服务器编号,开启bin-log

48304ba5e6f9fe08f3fa1abda7d326ab.png

[root@bi7 ~]# vim /etc/my.cnf.d/server.cnf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

server-id=7

log_bin=mysql-bin

binlog_format=row

relay-log=relay-mysql

relay-log-index=relay-mysql.index

log_slave_updates=on

auto-increment-increment=2

auto-increment-offset=1

48304ba5e6f9fe08f3fa1abda7d326ab.png

48304ba5e6f9fe08f3fa1abda7d326ab.png

[root@bi8 ~]# vim /etc/my.cnf.d/server.cnf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

server-id=8

log_bin=mysql-bin

binlog_format=row

relay-log=relay-mysql

relay-log-index=relay-mysql.index

log_slave_updates=on

auto-increment-increment=2

auto-increment-offset=2

48304ba5e6f9fe08f3fa1abda7d326ab.png

2、重启mysql服务(两台主机)

systemctl restart mariadb

ss -tnl |grep 3306

LISTEN     0      80          :::3306                    :::*

3、创建复制用的用户(两台主机)

48304ba5e6f9fe08f3fa1abda7d326ab.png

[root@bi8 ~]# mysql -uroot -proot

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

Your MariaDB connection id is 9

Server version: 10.2.26-MariaDB-log MariaDB Server

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

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

MariaDB [(none)]> grant replication slave on *.* to 'master'@'%' identified by '123';

Query OK, 0 rows affected (0.00 sec)

48304ba5e6f9fe08f3fa1abda7d326ab.png

4、(若第一次同步可忽略此步骤)这里可以清空两台主机的master和slave日志(若之前有开启过同步的,需要先停止同步:stop slave;)

在mysql命令行中输入:

reset master;

reset slave;

5、查看二进制日志文件的位置

48304ba5e6f9fe08f3fa1abda7d326ab.png

--主1

MariaDB [(none)]> show master status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000001 | 1087 | | |

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

1 row in set (0.00 sec)

48304ba5e6f9fe08f3fa1abda7d326ab.png

48304ba5e6f9fe08f3fa1abda7d326ab.png

--主2

MariaDB [(none)]> show master status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000001 | 520 | | |

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

1 row in set (0.00 sec)

48304ba5e6f9fe08f3fa1abda7d326ab.png

6、为防止配置同步的同时又数据写入,可给数据库加读锁(两台主机):

MariaDB [(none)]> flush tables with read lock;

Query OK, 0 rows affected (0.00 sec)

7、两台服务器互相连接

48304ba5e6f9fe08f3fa1abda7d326ab.png

--主1连接主2

MariaDB [(none)]> change master to master_host='192.168.11.8',master_user='master',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=520;

Query OK, 0 rows affected (0.01 sec)

--主2连接主1

MariaDB [(none)]> change master to master_host='192.168.11.7',master_user='master',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=1087;

Query OK, 0 rows affected (0.05 sec)

48304ba5e6f9fe08f3fa1abda7d326ab.png

8、启动slave(两台主机)

MariaDB [(none)]> start slave;

Query OK, 0 rows affected (0.01 sec)

9、检查连接状态(两台主机)

48304ba5e6f9fe08f3fa1abda7d326ab.png

MariaDB [(none)]> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Connecting to master

Master_Host: 192.168.11.7

Master_User: master

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000001

Read_Master_Log_Pos: 1087

Relay_Log_File: relay-mysql.000001

Relay_Log_Pos: 4

Relay_Master_Log_File: mysql-bin.000001

Slave_IO_Running: Connecting

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: 1087

Relay_Log_Space: 256

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: NULL

Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 2003

Last_IO_Error: error connecting to master 'master@192.168.11.7:3306' - retry-time: 60 maximum-retries: 86400 message: Can't connect to MySQL server on '192.168.11.7' (113 "No route to host")

Last_SQL_Errno: 0

Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id: 0

Master_SSL_Crl:

Master_SSL_Crlpath:

Using_Gtid: No

Gtid_IO_Pos:

Replicate_Do_Domain_Ids:

Replicate_Ignore_Domain_Ids:

Parallel_Mode: conservative

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

1 row in set (0.00 sec)

48304ba5e6f9fe08f3fa1abda7d326ab.png

10、添加防火墙规则

48304ba5e6f9fe08f3fa1abda7d326ab.png

[root@bi7 ~]# firewall-cmd --add-port=3306/tcp

success

[root@bi7 ~]# firewall-cmd --add-port=3306/tcp --permanent

success

[root@bi8 ~]# firewall-cmd --add-port=3306/tcp

success

[root@bi8 ~]# firewall-cmd --add-port=3306/tcp --permanent

success

48304ba5e6f9fe08f3fa1abda7d326ab.png

11、再次检查连接状态(两台主机)

48304ba5e6f9fe08f3fa1abda7d326ab.png

MariaDB [(none)]> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.11.8

Master_User: master

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000001

Read_Master_Log_Pos: 520

Relay_Log_File: relay-mysql.000002

Relay_Log_Pos: 555

Relay_Master_Log_File: mysql-bin.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: 520

Relay_Log_Space: 860

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: 8

Master_SSL_Crl:

Master_SSL_Crlpath:

Using_Gtid: No

Gtid_IO_Pos:

Replicate_Do_Domain_Ids:

Replicate_Ignore_Domain_Ids:

Parallel_Mode: conservative

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

1 row in set (0.00 sec)

48304ba5e6f9fe08f3fa1abda7d326ab.png

12、测试

--解锁(两台主机)

MariaDB [(none)]> unlock tables;

Query OK, 0 rows affected (0.00 sec)

48304ba5e6f9fe08f3fa1abda7d326ab.png

--查看主1的原本数据库

MariaDB [(none)]> show databases;

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

| Database |

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

| information_schema |

| mysql |

| performance_schema |

| bi |

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

4 rows in set (0.00 sec)

--在主1上创建新库yang

MariaDB [(none)]> create database if not exists yang default character set utf8;

Query OK, 1 row affected (0.00 sec)

48304ba5e6f9fe08f3fa1abda7d326ab.png

48304ba5e6f9fe08f3fa1abda7d326ab.png

--查看主2是否有新建的数据库

MariaDB [(none)]> show databases;

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

| Database |

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

| information_schema |

| mysql |

| performance_schema |

| yang |

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

4 rows in set (0.10 sec)

--使用yang数据库

MariaDB [(none)]> use yang;

Database changed

--在主2上创建新表hello,并插入一条数据

MariaDB [yang]> create table hello(id tinyint unsigned primary key auto_increment not null,name varchar(20));

Query OK, 0 rows affected (0.03 sec)

MariaDB [yang]> insert into hello values(0,'李国祥');

Query OK, 1 row affected (0.00 sec)

MariaDB [yang]> select * from hello;

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

| id | name |

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

| 2 | 李国祥 |

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

1 row in set (0.00 sec)

48304ba5e6f9fe08f3fa1abda7d326ab.png

48304ba5e6f9fe08f3fa1abda7d326ab.png

--查看主1是否新插入的数据

MariaDB [(none)]> use yang;

Database changed

MariaDB [yang]> select * from hello;

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

| id | name |

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

| 2 | 李国祥 |

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

1 row in set (0.01 sec)

48304ba5e6f9fe08f3fa1abda7d326ab.png

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/502952.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

c++十六进制转十进制_一文帮你详细图解二进制、八进制、十进制、十六进制之间的转换...

1、背景(Contexts)之前使用SQL把十进制的整数转换为三十六进制,SQL代码请参考:SQL Server 进制转换函数,其实它是基于二、八、十、十六进制转换的计算公式的,进制之间的转换是很基础的知识,但是我发现网络上没有一篇能…

python进程暂停_如何在Python中暂停多进程?

我希望用户能够在怎么开始的实现它?在我的代码是:# -*- coding: utf-8 -*-from PySide import QtCore, QtGuifrom Ui_MainWindow import Ui_MainWindowfrom queue import Queueimport sysimport multiprocessing, os, timedef do_work():print (Work Sta…

ceph存储原理_赠书 | Linux 开源存储全栈详解——从Ceph到容器存储

//留言点赞赠书我有书,你有故事么?留言说出你的存储故事留言点赞前两名,免费送此书截止日期12.27号12.30号公布名单//内容简介本书致力于帮助读者形成有关Linux开源存储世界的细致的拓扑,从存储硬件、Linux存储堆栈、存储加速、存…

mysql双主 绿色_mysql (双主,互主)

Master-Master(双主)1、测试环境Master/Slave Master1/Slave1IP 192.168.1.13 192.168.1.10为了保持干净的环境:两边服务器rm -rf /var/lib/mysql/*service mysqld restartIP:192.168.1.13IP:192.168.1.102、…

学习在UE中通过Omniverse实现对USD文件的Live-Sync(实时同步编辑)

目标 前一篇 学习了Omniverse的一些基础概念。本篇在了解这些概念的基础上,我想体验下Omniverse的一些具体的能力,特别是 Live-Sync (实时同步) 相关的能力。 本篇实践了使用Omniverse的力量在UE中建立USD文件的 Live-Sync 编辑。由于相关的知识我是从…

keras卷积处理rgb输入_CNN卷积神经网络模型搭建

前言前段时间尝试使用深度学习来识别评测过程中的图片,以减少人力成本。目前是在深度学习框架Keras(后端使用TensorFlow)下搭建了一个CNN卷积神经网络模型,下面就如何搭建一个最简单的数字图像识别模型做下介绍。模型的建立(1) 卷积层(convolution layer…

python装饰器应用论文_Python装饰器的应用场景代码总结

装饰器的应用场景 附加功能 数据的清理或添加: 函数参数类型验证 require_ints 类似请求前拦截 数据格式转换 将函数返回字典改为 json/YAML 类似响应后篡改 为函数提供额外的数据 mock.patch 函数注册 在任务中心注册一个任务 注册一个带信号处理器的函数不同应用场景下装饰器…

python中try命令_Python 异常处理 Python 基础教程 try..except

异常处理在之前的学习中我们一直没有接触过。 哦对,我们甚至还不知道怎么向程序输入一段字符串。那么我们在这里提供一个小例子。 在命令行中,我们输入 s raw_input(Enter something --> )好了,我们已经知道如何输入一个字符串了&#xf…

python读取大文件性能_强悍的Python读取大文件的解决方案

Python 环境下文件的读取问题,请参见拙文 Python基础之文件读取的讲解这是一道著名的 Python 面试题,考察的问题是,Python 读取大文件和一般规模的文件时的区别,也即哪些接口不适合读取大文件。1. read() 接口的问题f open(filen…

python mysql 保存csv_使用Python将csv文件快速转存到Mysql

因为一些工作需要,我们经常会做一些数据持久化的事情,例如将临时数据存到文件里,又或者是存到数据库里。对于一个规范的表文件(例如csv),我们如何才能快速将数据存到Mysql里面呢?这个时候,我们可以使用pyth…

python分词_Python 结巴分词实现关键词抽取分析

1 简介 关键词抽取就是从文本里面把跟这篇文档意义最相关的一些词抽取出来。这个可以追溯到文献检索初期,当时还不支持全文搜索的时候,关键词就可以作为搜索这篇论文的词语。因此,目前依然可以在论文中看到关键词这一项。 除了这些&#xff0…

redis 如何 mysql_Redis 如何保持和 MySQL 数据一致

一、需求起因在高并发的业务场景下,数据库大多数情况都是用户并发访问最薄弱的环节。所以,就需要使用redis做一个缓冲操作,让请求先访问到redis,而不是直接访问MySQL等数据库。这个业务场景,主要是解决读数据从Redis缓…

truncate python是删除文件内容吗_在Python中操作文件之truncate()方法的使用教程

truncate()方法截断该文件的大小。如果可选的尺寸参数存在,该文件被截断(最多)的大小。 大小默认为当前位置。当前文件位置不改变。注意,如果一个指定的大小超过了文件的当前大小,其结果是依赖于平台。 注意:此方法不会在当文件工…

sqlserver mysql时间格式化_SqlServer时间格式化

最近用的SqlServer比较多, 时间 格式化 老是忘记,现整理如下:(来源于网上,具体来源地址忘记了,归根到底MSDN吧) SELECT CONVERT(varchar(50), GETDATE(), 0): 05 16 2006 10:57AM SELECT CONVERT(varchar(50), GETDATE…

iframe 跨域_【梯云纵】搞定前端跨域

韦陀掌法,难陀时间善恶;梯云纵,难纵过乱世纷扰。现在开始写代码o(╯□╰)o什么是跨域1.跨域的定义广义的跨域是指一个域下对的文档或者脚本试图去请求另外一个域下的资源。a链接、重定向、表单提交、、、等标签background:url()、font-face()ajax 跨域请求……狭义的…

java中exception_Java中的异常 Exceptions

1. 概念exception是“exceptional event”的缩写,是指执行程序中发生的事件,破坏了程序的正常执行流程。Java 异常处理机制使程序更加健壮易于调试,它可以告诉程序员三个问题:错误的类型、位置、原因,帮助程序员解决错…

python异步asy_Python 异步编程之asyncio【转载】

一、协程的认识 协程(Coroutine),也可以被称为微线程,是一种用户态内的上下文切换技术。 简而言之,其实就是通过一个线程实现代码块相互切换执行。例如:deffunc1():print(1) ...print(2)deffunc2():print(3…

bitcount java_Java源码解释之Integer.bitCount

Java中的Integer.bitCount(i)的返回值是i的二进制表示中1的个数。源码如下:public static int bitCount(int i) {// HD, Figure 5-2i i - ((i >>> 1) & 0x55555555);i (i & 0x33333333) ((i >>> 2) & 0x33333333);i (i (i >&…

python自定义全局异常_如何在python中进行全局异常捕获

使用sys.excepthook函数进行全局异常的获取。 首先定义异常处理函数, 并使用该函数接收系统异常信息。 import wx import sys class TestFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, -1, test) btn wx.Button(self, -1, test) btn.Bind(w…

git merge 冲突_卧槽!小姐姐用动画图解 Git 命令,这也太秀了吧?!

公众号关注 “GitHubDaily”设为 “星标”,每天带你逛 GitHub!大家好,我是小 G。在座的各位应该都知道,Git 作为居家必备、团队协作之利器,自从 Linus Torvalds 发布这款工具后,便一直受到各路开发者的喜爱…