MySql master-slave replication (master: CentOS7_docker_mySql.8, slave: windows11_mySql.8)

I stepped on a lot of pitfalls when configuring mysql master-slave replication, so I made a note and I hope I can help you.

Master configuration (based on docker):

1.docker install mysql:

It is relatively simple to install mysql with docker, but pay attention to preparing the mount point directory in advance! Let's talk about the steps briefly.

# 1.下载mySql,默认最新

docker pull mysql

# 2.创建挂载点目录

mkdir -p /usr/local/mysql/log
mkdir -p /usr/local/mysql/data
mkdir -p /usr/local/mysql/config

# 3.运行创建容器

docker run -d \
-p 3306:3306 \
--privileged=true \
-v /usr/local/mysql/log:/var/log/mysql \
-v /usr/local/mysql/data:/var/lib/mysql \
-v /usr/local/mysql/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=设置你自己的密码 \
--name mysql mysql:latest

2. Configure the character set to prevent Chinese errors:

Note: For mysql installed by docker, there is no my.cnf configuration file under /etc, you need to create it yourself! Perform the following operations:

# 进入配置目录

cd /usr/local/mysql/conf

# 修改/新建配置文件,my.cnf应该为空

vim my.cnf

# 复制以下配置并保存退出

[client]
default_character_set=utf8
[mysqld]
collation_server=utf8_general_ci
character_set_server=utf8

# 重启mysql,使配置生效

docker restart mysql

Now that the preparatory work has been completed, the formal configuration of master-slave replication will begin.

3. Configure the master:

Continue to modify the my.cnf file, copy and save the following content:

# 进入/usr/local/mysql/conf目录

cd /usr/local/mysql/conf

# 修改my.cnf文件

vim my.cnf

# 复制并保存以下内容

[mysqld]
log-bin=mysql-bin # 启用二进制日志
server-id=100 # 服务器唯一id,也可以不设置为100

#重启mysql,使配置生效

docker restart mysql

The binary log and the unique id of the server must be configured, and there are some other configurations, which can be checked online if needed.

4. Enter the master container instance to log in to the database

# 进入数据库容器实例

docker exec -it 自己的容器名称(mysql) /bin/bash

# 登录数据库

mysql -uroot -p

5. Create a data synchronization user in the master container instance mysql

# 一条一条执行以下命令:
# 可以将'xiaoming'修改为其他名称,'123456'也可以修改为其他密码

CREATE USER 'xiaoming'@'%' IDENTIFIED BY '123456'; 

# 下面命令中的'xiaoming'需要与你自己设置的一致

GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'xiaoming'@'%';

6. Check the master status and verify whether the configuration is successful

# 查看master状态

show master status;

The following form appears, proving that the configuration is successful:

 Slave configuration (in Windows environment)

There are many pits here, and it took a long time to configure. Please follow the steps below to complete:

1. Find the configuration file:

The configuration file of mysql under windows is: C:\ProgramData\MySQL\my.ini.

Note: C:\ProgramData is a hidden directory, and it needs to be set to display the hidden directory. Click View -> Click Display -> Check the hidden directory.

 2. Modify the available permissions of the my.ini file:

The default permissions available to ordinary users of the my.ini file should be read-only. At this time, we cannot modify the file, so we need to modify the permissions first:

Right-click the my.ini file -> click "Properties" -> click "Security" -> click "Users" -> click "Edit" -> click "Users" -> check "Full Control" -> click "Apply" -> Click OK -> Click OK.

It is recommended to modify the permissions after this use is over.

 3. Edit the my.ini configuration file

Open the "my.ini" file with Notepad, add the following configuration at the end of the file, and the id can be changed to other unique ids:

[mysqld]
server-id=101

 The unique id must be configured, and other non-essential configurations need to be queried online.

4. Navicat connects to the database or directly starts mysql in the black window of mysql and executes the following SQL in sequence

# 连接master与slave

change master to
master host = '192.168.116.130',
master_user='xiaoming',
master_password='zb185462',
master_log_file='mysql-bin.000001',
master_log_pos=717;


# 查看slave状态

show slave status;

At this time, a screen that looks like garbled characters but is actually a table will appear in the black window:

 It can be seen clearly in navicat:

 Congratulations on the successful configuration.

Guess you like

Origin blog.csdn.net/m0_56680022/article/details/130301701