MySQL5.7 master-slave synchronization environment

1. Prepare the environment: (one master and one slave)

database ip
Main library 192.168.87.133
From the library 192.168.87.134

 1.1 Both virtual machines are installed with MySQL5.7. (Mysqladmin --version)

reference:

  1. Linux Centos7.5 install MySQL5.7
  2. mysql 5.7 can not be connected: Host '192.168.87.1' is not allowed to connect to this MysQL server
  3. Mysql--Host 'xxx' is blocked because of many connection errors;unblock with 'mysqladmin flush-hosts'

  1.2 After successful installation, create two databases.

Database script:

(1)创建数据库
#创建数据库user_db
CREATE DATABASE `user_db` CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci';

(2)在user_db中创建t_user表
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;

2. Configuration

 2.1 Main library configuration:

vim  /etc/my.cnf

Note: (wrong configuration)

Many blogs on the Internet are configured as follows. I configured it the same way at the beginning. After the configuration is complete, restarting mysql will not start.

[mysqld]
#开启日志
log‐bin = mysql‐bin
#设置服务id,主从不能一致
server‐id = 1
#设置需要同步的数据库
binlog‐do‐db=user_db
#屏蔽系统库同步
binlog‐ignore‐db=mysql
binlog‐ignore‐db=information_schema
binlog‐ignore‐db=performance_schema

Prompt to use systemctl status mysqld.service and journalctl -xe to view the error message;

And use the command: cat /var/log/mysqld.log to view the mysql log, it will report similar errors as follows: 

2019-12-25T13:34:26.236167Z 0 [ERROR] unknown variable 'binlog‐do‐db=user_db'
2019-12-25T13:34:26.236192Z 0 [ERROR] Aborting

Correct configuration (complete configuration) : (Instead of using-, use _ instead)

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]

validate_password=off
max_connect_errors=1000
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

##################主从配置的新增部分
#开启日志文件
log_bin=mysql-bin
#设置服务id,主从不能一致
server_id=1
#设置需要同步的数据库
binlog_do_db=user_db
##屏蔽系统库同步
binlog_ignore_db=mysql
binlog_ignore_db=information_schema
binlog_ignore_db=performance_schema


# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0                                                      

After correct configuration, restart the mysql service; use the command: systemctl restart mysqld

 

2.2 Slave configuration: (full configuration)

[mysqld]

validate_password=off
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

#开启日志
log_bin=mysql‐bin
#设置服务id,主从不能一致
server_id=2
#设置需要同步的数据库
replicate_wild_do_table=user_db.%
#屏蔽系统库同步
replicate_wild_ignore_table=mysql.%
replicate_wild_ignore_table=information_schema.%
replicate_wild_ignore_table=performance_schema.%


# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

 After correct configuration, restart the mysql service; use the command: systemctl restart mysqld

2.3 Check whether log_bin is successfully opened (the master and slave libraries can be queried )

3. Create a user for replication ( master library )

#切换至主库bin目录,登录主库,输入密码
mysql ‐uroot ‐p 

#授权主备复制专用账号
grant replication slave on *.* to 'slave'@'%' identified by '123456';

#刷新权限
FLUSH PRIVILEGES;

#确认位点 记录下文件名以及位点
show master status;

Screenshot: (use the command show master status; find out what you need later from the library configuration )

4. Set the slave library to the master library to synchronize data and check the link

#切换至从库bin目录,登录从库(输入密码)
mysql ‐uroot ‐p

#先停止同步
STOP SLAVE;

#修改从库指向到主库,使用上一步记录的文件名以及位点
CHANGE MASTER TO
master_host = '192.168.87.133',
master_user = 'slave',
master_password = '123456',
master_log_file = 'mysql-bin.000009',
master_log_pos = 582;

#启动同步
START SLAVE;

#查看从库状态Slave_IO_Runing和Slave_SQL_Runing都为Yes说明同步成功,如果不为Yes,请检error_log,#然后排查相关异常。
show slave status\G

######(一般不需要执行)注意: 如果之前此备库已有主库指向 需要先执行以下命令清空#########
STOP SLAVE IO_THREAD FOR CHANNEL '';
reset slave all;

ps:

master_host master node: ip address
master_user master node: the account created by the master node for replication
master_password master master node: the password created by the master node for replication
master_log_file The master node: the value of the file field queried by the sql command show master status
master_log_pos  Master master node: the value of the Position field queried by the sql command show master status

Full screenshot:

ps: View the slave status Slave_IO_Runing and Slave_SQL_Runing are both Yes to indicate that the synchronization is successful. If it is not Yes, please check error_log and then troubleshoot related exceptions.

5. Test:

Insert a piece of data in the t_user table in the master library (192.168.87.133); then refresh the t_user table in the slave library to see if the data has been synchronized.

5.1 Main library : t_user

5.2 Slave: t_user  (refresh table)

 

 
Published 186 original articles · praised 146 · 490,000 views

Guess you like

Origin blog.csdn.net/qq_37495786/article/details/103731681