Master-slave replication based on docker, mysql, mycat2, read-write separation

1. Understand the principle of MySQL master-slave replication.

MySQL's master-slave replication refers to the process of copying data changes on one MySQL database instance (i.e., the master database) to another MySQL database instance (i.e., the slave database) through binary logs to achieve data synchronization.

Specifically, in master-slave replication, when the master database performs a data update operation, it will record the content of the update operation in the binary log (binlog) and notify the slave database of this operation. After receiving the notification from the main library, the slave library will start an I/O thread, read events from the binary log of the main library, and write these events into its own relay log. Subsequently, the slave library will start a SQL thread and execute the events in the relay log to update its own data.

The benefit of master-slave replication is that it can increase the availability and scalability of the system, and it can also improve the performance of the system. But there are also some shortcomings, such as data delays from the database.

2. Complete MySQL master-slave replication (one master and two slaves).

1. Enable three docker containers

docker run --name master -p 3311:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --lower_case_table_names=1
docker run --name slave1 -p 3312:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --lower_case_table_names=1
docker run --name slave2 -p 3313:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --lower_case_table_names=1  

2: Start writing the configuration file

Method 1: Write directly into the container

1.docker exec -it master bash
2. cd /etc/mysql/conf.d/
3. vi docker.cnf
This method has many problems and is not recommended.

Method 2: Copy the configuration file, modify it, and then copy it to the container

1. 复制出来
	 docker cp master:/etc/mysql/conf.d/docker.cnf master.cnf
	docker cp slave1:/etc/mysql/conf.d/docker.cnf slave1.cnf
  	docker cp slave2:/etc/mysql/conf.d/docker.cnf slave2.cnf
  	如果提示找不到文件则自己创建相关文件再将其写入对应内容后cp到容器
  	
 2:修改完后写回去
 	 docker cp master.cnf master:/etc/mysql/conf.d/docker.cnf 
	docker cp slave1.cnf slave1:/etc/mysql/conf.d/docker.cnf 
  	docker cp slave2.cnf slave2:/etc/mysql/conf.d/docker.cnf 

The following is what is written

主机:
skip-host-cache
skip-name-resolve
server-id = 1 
#服务器的唯一标识
log_bin = master.bin 
# 开启日志功能
从机:
 server-id = 2 #服务器的唯一标识

3. Restart the container and complete related configurations

Enter the host:

create user 'master'@'%' identified by '123456'; 
grant replication slave on *.* to 'master'@'%';
flush privileges;
show master status;
//记住file名和position,从服务器配置要用
//如在下图中的 master.000001 和 154

Insert image description here

Slave machine:

change master to
master_host="192.168.128.132",master_port=3307,
// host 是主机的ip地址,port是映射的端口号,从服务器的配置相同
master_user="master",
master_password="123456",
master_log_file="master.000001",
master_log_pos=154;

4: Start successfully

Insert image description here

3. Complete the MySQL read-write separation configuration based on MySQL one master and two slave configurations.

doing

Error collection

1: show master status; the display is empty

Reason: The configuration file is wrong and the binary log is not configured properly.
Solution: Rewrite the configuration file.

2. MySQL encountered Found option without preceding group in config file

Reason: There is a syntax error in the MySQL configuration file, which is caused by the lack of a valid group declaration somewhere in the configuration file. Each configuration file must contain at least one valid group declaration, such as [mysqld], that specifies which settings apply.

Error demonstration

skip-host-cache
skip-name-resolve
server-id = 1 
log_bin = master.bin 

Correct way to write:

[mysqld]
skip-host-cache
skip-name-resolve
server-id = 1 
log_bin = master.bin 

3:docker msyql Exited (1)

As shown in the picture:
Insert image description here
Reason: There is a missing letter when entering MYSQL_ROOT_PASSWORD.
Insert image description here

4: There is no docker.cnf under /etc/mysql/con.f/ of docker

Reason: The docker.cnf file has been removed from the MySQL official Docker image and replaced by the .cnf configuration file in the /etc/mysql/conf.d directory.
Solution: Just create one yourself

5:you need (at least one of) the SUPER, REPLICATION CLIENT privilege(s) for this operation

Reason: The current user permissions are insufficient and SUPER, REPLICATION permissions are required.
SUPER: Allows the user to perform operations that require super user privileges.
REPLICATION: Allows the user to view information about replication status.
Solution:
Method 1: Switch root user
Method 2: Give the current user permissions, set the current user’s name to: libai (I haven’t tried it, but it is theoretically possible)

GRANT SUPER ON *.* TO 'libai'@'host';
GRANT REPLICATION CLIENT ON *.* TO 'libai'@'host';

6: Mycat cannot start

Insert image description here
Reason: JDK did not follow the

Guess you like

Origin blog.csdn.net/m0_51828898/article/details/129838043