MySQL master master backup


1. Configuration

A centos database
Port: 3306
Address: 106.11.111.11

A docker database
Port: 3307
Address: 106.11.111.12

Because of limited resources, we had to set up two databases for testing. Under normal circumstances, at least two servers are used for operation.

The steps of the operation are the same, don't care that I am using a container. In the master-slave database, I will first build two identical databases

The centos database has been installed

To run the container database, you can refer to:
https://blog.csdn.net/qq_38637558/article/details/101231395

# 首先检查主从两台Mysql是否作为其他master的slave,并停止复制,以免受到影响。
show slave status\G
stop slave;   

2. Configure the main library: 106.11.111.11

Modify my.cnf and
my location is: /usr/local/mysql

vim my.cnf

[mysqld]
server-id=1                       #指定master主机的id,不可为0,否则拒绝所有slave连接。
log-bin=mysql_bin                  #指定bin-log文件前缀名称,开启binlog日志
binlog_do_db=nestcloud        #指定binlog日志是记录的是哪个库 (nestcloud是我的数据库名)
replicate-do-db=nestcloud       #指定复制哪一个库(nestcloud是我的数据库名)
auto-increment-increment = 2   #每次增长2
auto-increment-offset = 1  #设置自动增长的字段的偏移量,即初始值为1
log_bin_trust_function_creators=1           #下面进行解释
expire_logs_days = 10            #保留10天的bin_log日志,防止日志太多占用磁盘空间
max_binlog_size = 100M              #限制每个bin_log日志大小最大为100M。
#log-slave-updates=1        #slave执行master的sql后,将sql记录在binlog日志中(默认是不记录的)——实际生产我没开启这条
max_connections=600             #指定最大连接数
wait_timeout=5                       #等待超时


log_bin_trust_function_creators: 
当二进制日志启用后,这个变量就会启用。它控制是否可以信任存储函数创建者,不会创建写入二进制日志引起不安全事件的存储函数。
如果设置为0(默认值),用户不得创建或修改存储函数,除非它们具有除CREATE ROUTINE或ALTER ROUTINE特权之外的SUPER权限。
如果变量设置为1,MySQL不会对创建存储函数实施这些限制。 此变量也适用于触发器的创建。

Insert picture description here

# 重启MySQL
service mysqld restart

Create an account

# 进入MySQL
mysql -h127.0.0.1 -uroot -p
#创建用户 106.11.111.12  (从服务器的IP)
CREATE USER 'sla'@'106.11.111.12' IDENTIFIED BY 'password123';
#分配权限
GRANT REPLICATION SLAVE ON *.* TO 'sla'@'106.11.111.12';
 #刷新权限
flush privileges;  

Check the master status and record the binary file name (mysql_bin.000001) and location (120):

show master status;

Insert picture description here

3. Configure the main library: 106.11.111.12

In fact, we configure this main library, and configure the above main library is the same operation. Basically there is no difference, but the my.cnf file is different.
Note: server-id and auto-increment-offset are the two values

vim my.cnf

[mysqld]
server-id=2                       #该值不能不可为0,并且所操作数据库的该值也不能一样
log-bin=mysql_bin                  #指定bin-log文件前缀名称,开启binlog日志
binlog_do_db=nestcloud        #指定binlog日志是记录的是哪个库 (nestcloud是我的数据库名)
replicate-do-db=nestcloud       #指定复制哪一个库(nestcloud是我的数据库名)
auto-increment-increment = 2   #每次增长2
auto-increment-offset = 2  #该值应设为操作的数据库的总数,本案例用到两台服务器,所以值设为2。
log_bin_trust_function_creators=1           #下面进行解释
expire_logs_days = 10            #保留10天的bin_log日志,防止日志太多占用磁盘空间
max_binlog_size = 100M              #限制每个bin_log日志大小最大为100M。
#log-slave-updates=1        #slave执行master的sql后,将sql记录在binlog日志中(默认是不记录的)——实际生产我没开启这条
max_connections=600             #指定最大连接数
wait_timeout=5                       #等待超时


log_bin_trust_function_creators: 
当二进制日志启用后,这个变量就会启用。它控制是否可以信任存储函数创建者,不会创建写入二进制日志引起不安全事件的存储函数。
如果设置为0(默认值),用户不得创建或修改存储函数,除非它们具有除CREATE ROUTINE或ALTER ROUTINE特权之外的SUPER权限。
如果变量设置为1,MySQL不会对创建存储函数实施这些限制。 此变量也适用于触发器的创建。

Insert picture description here

# 重启MySQL
service mysqld restart

Create an account

# 进入MySQL
mysql -h127.0.0.1 -uroot -p
#创建用户 106.11.111.12  (从服务器的IP)
CREATE USER 'sla2'@'106.11.111.12' IDENTIFIED BY 'password123';
#分配权限
GRANT REPLICATION SLAVE ON *.* TO 'sla2'@'106.11.111.12';
 #刷新权限
flush privileges;  

Check the master status and record the binary file name (mysql_bin.000001) and location (786):

show master status;

Insert picture description here

4. Start to configure the master

I am now in the database: 106.11.111.12

Enter MySQL

Execute synchronous SQL statement:

mysql> CHANGE MASTER TO
    -> MASTER_HOST='106.11.111.11',
    -> MASTER_USER='sla',
    -> MASTER_PASSWORD='password123',
    -> MASTER_PORT=3306,
    -> MASTER_LOG_FILE='mysql_bin.000001',
    -> MASTER_LOG_POS=120;

MASTER_HOST:106.11.111.11 数据库地址
MASTER_USER:106.11.111.11 用户名
MASTER_PASSWORD:106.11.111.11 密码
MASTER_PORT:106.11.111.11 端口,如果是3306可以不写
MASTER_LOG_FILE:106.11.111.11 指定开始复制的binlog二进制文件。
MASTER_LOG_POS:106.11.111.11 指定(在master二进制文件中)要开始复制的位置

# 整合成一段
CHANGE MASTER TO MASTER_HOST='106.11.111.11', MASTER_USER='sla', MASTER_PASSWORD='password123', MASTER_PORT=3306, MASTER_LOG_FILE='mysql_bin.000001', MASTER_LOG_POS=120;

Insert picture description here

启动slave同步进程:
start slave;

查看slave状态:
show slave status\G

Insert picture description here
When these two are yes, it proves that one is successful
-------------------------------------- -------------------------------...------------------ ----------------------

Now configure another one, I am now in the database: 106.11.111.11

The steps are the same as above, except that the user name, password, port, etc. can be modified.

Enter MySQL to
execute synchronous SQL statements:

CHANGE MASTER TO MASTER_HOST='106.11.111.12', MASTER_USER='sla2', MASTER_PASSWORD='password123', MASTER_PORT=3307, MASTER_LOG_FILE='mysql_bin.000001', MASTER_LOG_POS=786;


MASTER_HOST:106.11.111.12的 数据库地址
MASTER_USER:106.11.111.12的 用户名
MASTER_PASSWORD:106.11.111.12的 密码
MASTER_PORT:106.11.111.12的 端口,如果是3306可以不写
MASTER_LOG_FILE:106.11.111.12的 指定开始复制的binlog二进制文件。
MASTER_LOG_POS:106.11.111.12的 指定(在master二进制文件中)要开始复制的位置
启动slave同步进程:
start slave;

查看slave状态:
show slave status\G

Insert picture description here
When these two are yes, it proves that we have succeeded in another one

-------------------------------------------------- -------------------...------------------------------ ----------At
this point our master has succeeded

5. Start verification

In the database: 106.11.111.11, perform CURD operations on the data, and the database 106.11.111.12 will be updated together.
Also in the database: 106.11.111.12, perform CURD operations on the data, and the database 106.11.111.11 will also be updated together.
I will not take screenshots here.

6, miscellaneous talk

In fact, the principle of master-master backup is the same as master-slave backup. It is basically the same. After meeting the master-slave, the master-slave will naturally become the master-slave.
The main master is actually configuring one side, and the other side is basically repeated operations. It is nothing more than modifying some key data.



Then pay attention to the user name, password, binary file name, and the location to start copying. If you don’t fill in these errors, there will be no problem



. The problem needs special attention. When we turn on the main backup, we need to pay attention to the function call of the database. There may be problems when calling the function. For solutions, please refer to:
https://blog.csdn.net/lost_in_the_woods/article/details/81589294 https://blog.csdn.net/lost_in_the_woods/article/details/81589294 https://blog.csdn.net/lost_in_the_woods/article/details/81589294 https://blog.csdn.net/lost_in_the_woods/article/details/81589294
https://blog.csdn.net/lost_in_the_woods/ ://blog.csdn.net/topasstem8/article/details/8216740






Okay, so be it, goodbye!
Previous: Master-Slave Backup

Next: Clear backup files regularly

Provide a group: 807770565, welcome everyone to come in and chat
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_38637558/article/details/101321337