mysql/mairadb双主复制

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:
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,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
7月前
|
SQL 存储 关系型数据库
MySQL主从复制之原理&一主一从部署流程—2023.04
MySQL主从复制之原理&一主一从部署流程—2023.04
229 0
|
9月前
|
关系型数据库 MySQL 开发工具
MySQL双主复制
MySQL双主复制
163 0
|
5月前
|
SQL 容灾 关系型数据库
MySQL 主从复制原理
MySQL 主从复制原理
47 1
MySQL 主从复制原理
|
6月前
|
负载均衡 网络协议 关系型数据库
rhel 8.7 部署 keepalived+haproxy 实现 mysql 双主高可用场景 2
rhel 8.7 部署 keepalived+haproxy 实现 mysql 双主高可用场景
97 2
|
6月前
|
关系型数据库 MySQL 网络安全
rhel 8.7 部署 keepalived+haproxy 实现 mysql 双主高可用场景 1
rhel 8.7 部署 keepalived+haproxy 实现 mysql 双主高可用场景
77 0
|
7月前
|
SQL 关系型数据库 MySQL
MySql主从复制原理及其搭建
MySql主从复制原理及其搭建
|
9月前
|
SQL 负载均衡 关系型数据库
MySQL主从复制的原理与实操+mycat2读写分离
MySQL主从复制的原理与实操+mycat2读写分离
139 0
|
11月前
|
SQL 存储 缓存
MySQL-双主高可用
MySQL-双主高可用
172 0
|
SQL 缓存 算法
【MySQL】主从复制(重点:主从复制原理)
本文重点介绍MySQL的主从复制概述,作用,原理,同步数据一致性问题。
121 0
|
SQL 关系型数据库 MySQL
面试官问:了解Mysql主从复制原理么?我呵呵一笑
搭建Mysql主从同步之前,我们先来说他们之间同步的过程与原理: 同步复制过程 献上一张图,这张图诠释了整个同步过程