mysql/mairadb双主复制

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介:
mysql/mairadb双主复制

node5: 172.16.92.5/16 mariadb主服务器1
node6: 172.16.92.6/16 mariadb主服务器2
以上节点均为CentOS 7.1

配置环境
1. 配置好光盘yum源
2. 关闭selinux和iptables

============ 一. 安装mariadb-server并配置好文件 ===========

node5: mariadb主服务器

[root@node5 ~]# yum -y install mariadb-server
[root@node5 ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

#######以下的内容为添加########
#二进制变更日志
log-bin=mysql-bin
#二进制日志格式为混合模式
binlog_format=mixed
#为主服务器node5的ID值
server-id = 5
relay-log=relay-bin
#第一个变量名 auto_increment_offset 指自增字段的起始值。
auto_increment_offset=1
#第二个变量名 auto_increment_increment 就是指字段一次递增多少;
auto_increment_increment=2
log_slave_updates = 1

port = 3306
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 4
innodb_file_per_table = on
skip_name_resolve = on
###############################

###### 以下的内容可选 ########
[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
#############################

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
############### End for my.cnf #################


node6: mariadb主服务器2

[root@node6 ~]# yum -y install mariadb-server
[root@node6 ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

########## 添加以下内容 ##########
log-bin=mysql-bin
binlog_format=mixed
server-id = 6
relay-log = relay-bin
#第一个变量名 auto_increment_offset 指自增字段的起始值。
auto_increment_offset=2
#第二个变量名 auto_increment_increment 就是指字段一次递增多少;
auto_increment_increment=2
log_slave_updates = 1

port = 3306
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 4
innodb_file_per_table = on
skip_name_resolve = on
###################################

######### 以下内容可选 ############
[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout
####################################

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

############# End of my.cnf ###############


============ 二. 开启mariadb服务, 添加授权用户 ===========

node5 : 172.16.92.5 主服务器1 
[root@node5 ~]# systemctl start mariadb
[root@node5 ~]# mysql
MariaDB [(none)]> grant replication client,replication slave on *.* to 'repluser'@'172.16.%.%' identified by 'replpass';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> show master status\G
*************************** 1. row ***************************
            File: mysql-bin.000003
        Position: 506
    Binlog_Do_DB: 
Binlog_Ignore_DB: 
##### 记下mysql-bin.000003 和 506 , 设置主服务器2中继日志时有用 ####


node6 : 172.16.92.6 主服务器2
[root@node6 ~]# systemctl start mariadb
[root@node6 ~]# mysql
MariaDB [(none)]> grant replication client,replication slave on *.* to 'repluser'@'172.16.%.%' identified by 'replpass';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> show master status\G
*************************** 1. row ***************************
            File: mysql-bin.000003
        Position: 506
    Binlog_Do_DB: 
Binlog_Ignore_DB: 
##### 记下mysql-bin.000003 和 506 , 设置主服务器1中继日志时有用 #####


============ 三. 相互设置对方为主节点, 并开启同步线程 ===========
node5 : 172.16.92.5 主服务器1 
MariaDB [(none)]> change master to master_host='172.16.92.6',master_user='repluser',master_password='replpass',master_log_file='mysql-bin.000003',master_log_pos=506;
MariaDB [(none)]> start slave;

node6 : 172.16.92.6 主服务器2
MariaDB [(none)]> change master to master_host='172.16.92.5',master_user='repluser',master_password='replpass',master_log_file='mysql-bin.000003',master_log_pos=506;
MariaDB [(none)]> start slave;



============ 四. 查看主主复制的两个节点同步进程状态 ===========
node5 : 172.16.92.5 主服务器1 
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.92.6
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 506
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
    ......其余信息省略........

MariaDB [(none)]> show processlist\G
*************************** 3. row ***************************
   State: Slave has read all relay log; waiting for the slave I/O thread to update it
    ......其余信息省略........
*************************** 4. row ***************************
   State: Master has sent all binlog to slave; waiting for binlog to be updated
    ......其余信息省略........
#说明: 双主节点已相互发送中继日志



node6 : 172.16.92.6 主服务器2
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.92.5
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 506
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
    ......其余信息省略........

MariaDB [(none)]> show processlist\G
*************************** 2. row ***************************
   State: Master has sent all binlog to slave; waiting for binlog to be updated
    ......其余信息省略........
*************************** 4. row ***************************
   State: Slave has read all relay log; waiting for the slave I/O thread to update it
    ......其余信息省略........



最后测试mysql双主模型的两个节点能否相互同步数据
node5 mariadb1
MariaDB [(none)]> create database mydb;
MariaDB [(none)]> use mydb;
MariaDB [mydb]> create table tb1 (id int unsigned not null auto_increment primary key, name char(30));
MariaDB [mydb]> insert into tb1 (name) values ('tom');
MariaDB [mydb]> insert into tb1 (name) values ('jerry');
MariaDB [mydb]> select * from tb1;
+----+-------+
| id | name  |
+----+-------+
|  1 | tom   |
|  3 | jerry |
+----+-------+

node6 mariadb2
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |        #能看到node5创建的数据库
| mysql              |
| performance_schema |
| test               |
+--------------------+
MariaDB [(none)]> use mydb;
MariaDB [mydb]> insert into tb1 (name) values ('andy');
MariaDB [mydb]> insert into tb1 (name) values ('allen');
MariaDB [mydb]> select * from tb1;
+----+-------+
| id | name  |
+----+-------+
|  1 | tom   |
|  3 | jerry |
|  4 | andy  |
|  6 | allen |
+----+-------+

node5 mariadb1
MariaDB [mydb]> select * from tb1;
+----+-------+
| id | name  |
+----+-------+
|  1 | tom   |
|  3 | jerry |
|  4 | andy  |
|  6 | allen |
+----+-------+

从上面的测试可确定两个节点能相互同步mysql的数据

############# mysql双主复制结束 ##############







本文转自 zhuhc1988 51CTO博客,原文链接:http://blog.51cto.com/changeflyhigh/1711199,如需转载请自行联系原作者
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
9月前
|
SQL 关系型数据库 MySQL
面试官:说一下MySQL主从复制的原理?
面试官:说一下MySQL主从复制的原理?
196 0
面试官:说一下MySQL主从复制的原理?
|
SQL 存储 关系型数据库
MySQL主从复制之原理&一主一从部署流程—2023.04
MySQL主从复制之原理&一主一从部署流程—2023.04
790 0
|
9月前
|
SQL 关系型数据库 MySQL
MySQL中主从复制的原理和配置命令
要原因包括提高性能、实现高可用性、数据备份和灾难恢复。了解两大线程( I/O 和 SQL)I/O线程:目的:I/O线程主要负责与MySQL服务器之外的其他MySQL服务器进行通信,以便复制(replication)数据。 功能: 当一个MySQL服务器作为主服务器(master)时,I/O线程会将变更日志(binary log)中的事件传输给从服务器(slave)。从服务器上的I/O线程负责接收主服务器的二进制日志,并将这些事件写入本地的中继日志(relay log)。 配置: 在MySQL配置文件中,你可以通过配置参数如和来启用二进制日志和指定服务器ID。log-bin server
158 1
MySQL中主从复制的原理和配置命令
|
10天前
|
SQL 存储 关系型数据库
MySQL主从复制 —— 作用、原理、数据一致性,异步复制、半同步复制、组复制
MySQL主从复制 作用、原理—主库线程、I/O线程、SQL线程;主从同步要求,主从延迟原因及解决方案;数据一致性,异步复制、半同步复制、组复制
|
4月前
|
存储 关系型数据库 MySQL
MySQL主从复制原理和使用
本文介绍了MySQL主从复制的基本概念、原理及其实现方法,详细讲解了一主两从的架构设计,以及三种常见的复制模式(全同步、异步、半同步)的特点与适用场景。此外,文章还提供了Spring Boot环境下配置主从复制的具体代码示例,包括数据源配置、上下文切换、路由实现及切面编程等内容,帮助读者理解如何在实际项目中实现数据库的读写分离。
270 1
MySQL主从复制原理和使用
|
4月前
|
SQL 关系型数据库 MySQL
Mysql中搭建主从复制原理和配置
主从复制在数据库管理中广泛应用,主要优点包括提高性能、实现高可用性、数据备份及灾难恢复。通过读写分离、从服务器接管、实时备份和地理分布等机制,有效增强系统的稳定性和数据安全性。主从复制涉及I/O线程和SQL线程,前者负责日志传输,后者负责日志应用,确保数据同步。配置过程中需开启二进制日志、设置唯一服务器ID,并创建复制用户,通过CHANGE MASTER TO命令配置从服务器连接主服务器,实现数据同步。实验部分展示了如何在两台CentOS 7服务器上配置MySQL 5.7主从复制,包括关闭防火墙、配置静态IP、设置域名解析、配置主从服务器、启动复制及验证同步效果。
197 0
Mysql中搭建主从复制原理和配置
|
6月前
|
SQL 关系型数据库 MySQL
说一下MySQL主从复制的原理?
【8月更文挑战第24天】说一下MySQL主从复制的原理?
78 0
|
6月前
|
SQL canal 关系型数据库
(二十四)全解MySQL之主从篇:死磕主从复制中数据同步原理与优化
兜兜转转,经过《全解MySQL专栏》前面二十多篇的内容讲解后,基本对MySQL单机模式下的各方面进阶知识做了详细阐述,同时在前面的《分库分表概念篇》、《分库分表隐患篇》两章中也首次提到了数据库的一些高可用方案,但前两章大多属于方法论,并未涵盖真正的实操过程。接下来的内容,会以目前这章作为分割点,开启MySQL高可用方案的落地实践分享的新章程!
2621 1
|
9月前
|
关系型数据库 MySQL 数据库
MySQL集群 双主架构(配置命令)
MySQL集群 双主架构(配置命令)
145 1
|
关系型数据库 MySQL 开发工具
MySQL双主复制
MySQL双主复制
221 0

推荐镜像

更多