Install MySQL8 master-slave replication configuration based on centos7

1. Principle of master-slave replication

  1. Data changes are recorded in the Binary Log on the main library. These records are called Binary Log events.
  2. The slave library uses the IO thread to copy the log on the main library to its own relay log (Relay Log).
  3. Read the events in the relay log from the library through the SQL thread, and replay them on your own data.
    Insert picture description here

1.1. Environmental preparation

Centos7 install mysql8 database reference article address
Server environment: centos7 mysql version: 8.0.18
master server: 192.168.60.165
slave server: 192.168.60.206
other articles are part of the operation commands are operated on the server mysql terminal, as follows I will use Navicat connects to the database to execute commands.

2. Primary server node

2.1, modify the my.cnf file

[root@localhost ~]# vi /etc/my.cnf
#服务器节点id,一般为服务器ip方便区分
server-id=230
#开启日志文件
log-bin=mysql-bin
#指定要同步的数据库,多个用逗号隔开(可以不用配置)
binlog_do_db=user_db

2.1.1, verify that the configuration is successful

#重启mysql服务
systemctl restart mysqld
#查看mysql服务状态
systemctl status mysqld

2.1.2, verify that the query server_id exists

## 验证查询server_id是否存在 
 show variables like '%server_id%';

2.1.3, query the number of synchronized files and lines

# 查询同步的文件和行数,如下查询的数据在从服务器上执行命令会用到。
show master status;  

3. Slave server node

3.1, modify the my.cnf file

[root@localhost ~]# vi /etc/my.cnf
 
#服务器节点id,一般为服务器ip方便区分
server-id=235
#开启日志文件
log-bin=mysql-bin
#指定要同步的数据库,多个用逗号隔开(可以不用配置)
binlog_do_db=gi_test

3.2, verify that the configuration is successful

#重启mysql服务
systemctl restart mysqld
#查看mysql服务状态
systemctl status mysqld

3.2.1, verify that the query server_id exists

## 验证查询server_id是否存在 
 show variables like '%server_id%';

3.2.2, synchronize the master server configuration from the server

#从服务器器同步主服务器配置
	## master_log_file='mysql-bin.000002' 表示要同步的日志文件File,
	##是主服务器上述命令‘show master status;'查询出来的值 
	## master_log_pos=155 表示要同步日志文件的位置(多少行),详见上图。
change master to master_host='192.168.60.165',master_user='admini',master_password='123456',master_log_file='mysql-bin.000002',master_log_pos=155;
#开启同步
start slave
#检查从服务器复制功能状态
show slave status;

Note: The two fields in the red box shown in the figure above are yes, which means that the master-slave replication has been successfully started.

4. Functional test

Create a new table tb_uesr in the main database 192.168.60.165

DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`user_id` bigint(20) NOT NULL COMMENT '用户id',
`fullname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户姓名',
`user_type` char(1) DEFAULT NULL COMMENT '用户类型',
PRIMARY KEY (`user_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

Then go to the database 192.168.60.206 to check whether tb_user exists in the table, if it exists, it means the configuration has been successful

Guess you like

Origin blog.csdn.net/zhouzhiwengang/article/details/112369713