Without further ado, let's go directly to the order!
The problem to be solved: One day, an idle person deletes your mysql container directly, and your tables and data can still be found in the secondary database, and can be reused.
1. First go to the docker hub to pull a mysql: 5.7 image (I am used to version 5.7, so I can pull the latest version)
docker pull mysql:5.7
2, first create the mysql-master main library
log: log data: data conf: configuration for data volume monitoring
docker run -d -p 3307:3306 --name mysql-master \
> -v /mydata/mysql-master/log:/var/log/mysql \
> -v /mydata/mysql-master/data:/var/lib/mysql \
> -v /mydata/mysql-master/conf:/etc/mysql \
> -e MYSQL_ROOT_PASSWORD=root \
> -d mysql:5.7
3, and then configure the main library, first enter
cd /mydata/mysql-master/conf
4, create a my.cnf file under /mydata/mysql-master/conf
vim my.cnf
5, edit my.cnf (just paste the configuration file directly)
[mysqld]
## Set server_id, unique
server_id is required in the same LAN=101
## Specify the database name that does not need to be synchronized
binlog_ignore-db=mysql
## Enable binary log function
log-bin=mall-mysql-bin
## Set binary log Use memory size (transaction)
binlog_cache_size=1M
## Set the binary log format used (mixed, statement, row)
binlog_format=mixed
## Binary log expiration cleanup time. The default value is 0, which means no automatic cleanup.
expire_logs_days=7
## Skip all errors or specified types of errors encountered in master-slave replication to avoid interruption of slave-side replication.
## Such as: 1062 error means that some primary keys are repeated, 1032 error is because the master-slave database data is inconsistent
slave_skip_errors=1062
6, then restart mysql-master
docker restart mysql-master
7, enter the main library
docker exec -it mysql-master /bin/bash
8 , enter the command
mysql -uroot -p
9 , enter password
root (you can't see it when you enter it, don't make a mistake)
This has entered the main library, we put it first, and then create a slave library
Before creating a slave library, you must authorize the slave library first, and outsiders cannot access my main library casually!
CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
CRANT REPLICATION SLAVE , REPLICATION CLIENT ON *.* TO 'slave'@'%';
After entering these two commands, the authorization is complete, so that you can access the main library.
Enter show master status; it will be displayed as follows: the information here is useful when binding the relationship below
10, start to create slave library
docker run -p 3308:3306 --name mysql-slave \
> -v /mydata/mysql-slave/log:/var/log/mysql \
> -v /mydata/mysql-slave/data:/var/lib/mysql \
> -v /mydata/mysql-slave/conf:/etc/mysql \
> -e MYSQL_ROOT_PASSWORD=root \
> -d mysql:5.7
11, enter into /mydata/mysql-slave/conf
cd /mydata/mysql-slave/conf
12, create the configuration file my.cnf from the library
vim my.cnf
13, edit the configuration file (you can also paste it directly)
[mysqld]
server_id=102
binlog-ignore-db=mysql
log-bin=mall-mysql-slave1-bin
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062
relay_log=mall-mysql-relay-bin
log_slave_updates=1
read_only=1
14, restart the slave library
docker restart mysql-slave
15 , enter the slave library
docker exec -it mysql-slave /bin/bash
16 , enter the command
mysql -uroot -p
17 , enter password
root (you can't see it when you enter the password, don't make a mistake)
18, now that the two libraries have been created, you need to bind the relationship (just like who recognizes who is the big brother)
master_host: local ip
change master to master_host='192.168.234.132' , master_user='slave'
,master_password='123456',master_port=3307,master_log_file='mall-mysql-bin.000001'
,master_log_pos=617,master_connect_retry=30;
19. After the execution is completed, enter, enter show slave status \G; it will appear as follows: the original No changes to Yes to indicate that the binding is complete.
20, operate tables in the main library, create a database
create database db01;
21, create a table
create table t1(id int ,name varchar(20));
22 , then execute
use db01;
23 , and then add a piece of data to the table
insert into t1 values(1,'zs');
24 , and then execute from the library
use db01;
25, and then check the data in the slave library to find the tables and data in the main library
select * from t1;