mariadb主主

服务器环境

ip1:10.10.0.11 ip2:10.10.0.12

1.分别导入配置文件

mkdir -p /data/mariadb/conf/
vim my.cnf

[client]
default-character-set = utf8mb4

[mysql]
default-character-set = utf8mb4

[mysqld]
lower_case_table_names=1
wait_timeout=1800
max_allowed_packet = 512M
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
skip-host-cache

2.分别docker启动一个mariadb

docker run -d --restart=always -p3306:3306 --name mariadb -v /etc/localtime:/etc/localtime  -v /data/mariadb/conf/my.cnf:/etc/mysql/my.cnf -v /data/mariadb/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456  mariadb:10.6

3.分别创建复制账号并授权

grant replication slave on *.* to repluser@'10.10.0.%' identified by 'zhuzhu';

4.主主复制特点

两个节点,都可以更新数据,并且互为主从。容易产生数据不一致的问题,生产中要慎用。

主主复制的配置步骤:
(1) 各节点使用各自唯一server_id
(2) 都启动binary log和relay log
(3) 创建拥有复制权限的用户账号
(4) 定义自动增长id字段的数值范围各为奇偶 auto_increment_offset=1 auto_increment_increment=2
(5) 双主均要把对方指定为主节点,并启动复制线程

5.配置主节点10.10.0.11

5.1# 在[mysqld]段内增加四行,定义节点ID、开启二进制、开始点和增长幅度
vim /data/mariadb/conf/my.cnf

[mysqld]
server-id=11
log-bin
auto_increment_offset=1
auto_increment_increment=2

5.2# 查看并记录二进制日志信息,将在数据库主节点10.10.012再次配置需要用到73e4df274138-bin.000001和335这两个值
docker exec -it mariadb bash
mysql -uroot -p123456

MariaDB [(none)]> show master logs;
+-------------------------+-----------+
| Log_name                | File_size |
+-------------------------+-----------+
| 73e4df274138-bin.000001 |       335 |
+-------------------------+-----------+
1 row in set (0.000 sec)

6.配置主节点10.10.0.12

6.1# 在[mysqld]段内增加四行,定义节点ID、开启二进制、开始点和增长幅度
vim /data/mariadb/conf/my.cnf

[mysqld]
server-id=12
log-bin
auto_increment_offset=2
auto_increment_increment=2

6.2# 配置数据库复制

stop slave; 

CHANGE MASTER TO 
MASTER_HOST='10.10.0.11',
MASTER_USER='repluser', 
MASTER_PASSWORD='zhuzhu',
MASTER_PORT=3306,
MASTER_LOG_FILE='73e4df274138-bin.000001', 
MASTER_LOG_POS=1629;

start slave; 

6.3# 查看并记录二进制日志信息,将在数据库主节点10.10.0.11再次配置需要用到179d604d3e6f-bin.000001和538这两个值

MariaDB [(none)]> show master logs;       
+-------------------------+-----------+
| Log_name                | File_size |
+-------------------------+-----------+
| 179d604d3e6f-bin.000001 |       538 |
+-------------------------+-----------+
1 row in set (0.000 sec)

6.4# 查看IP11到IP12复制状态

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

7.再配置数据库主节点10.10.0.11复制。

stop slave;

CHANGE MASTER TO 
MASTER_HOST='10.10.0.12',
MASTER_USER='repluser', 
MASTER_PASSWORD='zhuzhu',
MASTER_PORT=3306,
MASTER_LOG_FILE='179d604d3e6f-bin.000001', 
MASTER_LOG_POS=1382;

start slave;

7.1# 查看IP12到IP11复制状态

show slave status\G 

至此,两台都为主MariaDB数据库的复制(主主复制)配置全部完成

8.验证数据库主主复制

在数据库主节点1创建一个test1数据库

create database test1;
创建表
use database test1;
create table t1(id int auto_increment primary key,name char(10));
插入一对键值
insert t1 (name) values(‘user1’);

随即切换到数据库主节点2上,也在shonedb1数据库的t1表内插入一对键值

use database test1;
insert t1 (name) values(‘user2’);

再通过SSH终端软件,同时向数据库主节点1和数据库主节点2,同时发送了下面命令

insert t1 (name) values(‘userX’);

查看结果
select * from t1;

9.创建keepalived实现高可用

yum install keepalived -y
9.1使用非抢占模式,编辑节点一的keepalived.conf

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
   }
   notification_email_from [email protected]
   router_id LVS_DEVEL

  # vrrp_strict

}
vrrp_script chk_mariadb {
script "/root/check_mariadb.sh" #最后手动执行下此脚本,以确保此脚本能够正常执行       
interval 2 #(检测脚本执行的间隔,单位是秒)
weight 2
}

vrrp_instance VI_1 {
    state BACKUP #两台都需要设置为backup
    interface eth0
    virtual_router_id 51
    priority 99
    nopreempt #非抢占模式
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 8888
    }
    virtual_ipaddress {
        10.10.0.150/24
    }
   track_script {
   chk_mariadb #调用检测脚本
}
}

9.2使用非抢占模式,编辑节点二的keepalived.conf

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
   }
   notification_email_from [email protected]
   router_id LVS_DEVEL
}
vrrp_script chk_mariadb {
script "/root/check_mariadb.sh" #最后手动执行下此脚本,以确保此脚本能够正常执行
interval 2 #(检测脚本执行的间隔,单位是秒)
weight 2
}

vrrp_instance VI_1 {
    state BACKUP #两台都需要设置为backup
    interface eth0
    virtual_router_id 51
    priority 100
    nopreempt #非抢占模式
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 8888
    }
    virtual_ipaddress {
        10.10.0.150/24
    }
    track_script {
    chk_mariadb #调用检测脚本
}
}

9.3使用监测mariadb脚本
vim /root/check_mariadb.sh

#!/bin/bash
A=`ps -C mariadbd --no-header |wc -l`
if [ $A -eq 0 ]
then
echo 'mariadb server is died'
systemctl stop keepalived
fi

chmod +x /root/check_mariadb.sh

10.测试
ip addr 查看虚拟ip在哪台服务器上
结束mariadb,看脚本是否生效,ip是否飘逸
docker stop mariadb

你可能感兴趣的:(1024程序员节)