mariadb master

Server environment

ip1:10.10.0.11 ip2:10.10.0.12

1. Import configuration files separately

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. Start a mariadb with docker respectively

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. Create duplicate accounts and authorize them separately

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

4. Features of master-master replication

Both nodes can update data and serve as master-slave to each other. It is easy to cause data inconsistency problems, so it should be used with caution in production.

Configuration steps for master-master replication:
(1) Each node uses its own unique server_id
(2) Start binary log and relay log
(3) Create a user account with replication permissions
(4) Define the value range of the automatically growing id field to be odd and even auto_increment_offset=1 auto_increment_increment=2
(5) Both masters must designate each other as the master node and start the replication thread.

5. Configure the master node 10.10.0.11

5.1# Add four lines in the [mysqld] section to define the node ID, start binary, starting point and growth rate
vim /data/mariadb/conf/my.cnf

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

5.2# View and record the binary log information. The two values ​​73e4df274138-bin.000001 and 335 will be used when reconfiguring the database master node 10.10.012.
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. Configure the master node 10.10.0.12

6.1# Add four lines in the [mysqld] section to define the node ID, start binary, starting point and growth rate
vim /data/mariadb/conf/my.cnf

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

6.2# Configure database replication

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# View and record the binary log information. The two values ​​​​179d604d3e6f-bin.000001 and 538 will be used when configuring the database master node 10.10.0.11 again.

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

6.4# Check IP11 to IP12 replication status

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

7. Then configure the replication of the database master node 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# Check IP12 to IP11 replication status

show slave status\G 

At this point, the replication (master-master replication) configuration of both servers as the primary MariaDB database is completed.

8. Verify database master-master replication

Create a test1 database on the main database node 1

create database test1;
Create table
use database test1;
create table t1(id int auto_increment primary key,name char(10));
Insert a pair of key values
​​insert t1 (name) values('user1');

Then switch to the main database node 2 and insert a pair of key values ​​into the t1 table of the shonedb1 database.

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

Then through the SSH terminal software, the following commands were sent to the database master node 1 and database master node 2 at the same time.

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

View the results
select * from t1;

9. Create keepalived to achieve high availability

yum install keepalived -y
9.1 uses non-preemption mode and edits keepalived.conf of node one.

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 Use non-preemption mode and edit keepalived.conf of node 2

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 Use the monitoring mariadb script
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. Test
ip addr to see which server the virtual ip is on.
End mariadb to see if the script takes effect and whether the ip is elegant.
Docker stop mariadb

Guess you like

Origin blog.csdn.net/weixin_42516922/article/details/134014505