MySQL5.7 配置主从同步
配置文件修改
主节点
my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
transaction-isolation = READ-COMMITTED
key_buffer_size = 32M
max_allowed_packet = 32M
thread_stack = 256k
thread_cache_size = 64
query_cache_limit = 8M
query_cache_size = 64M
query_cache_type = 1
max_connections = 550
server-id = 1
log_bin = /var/lib/mysql/mysql_master.log
binlog_format = mixed
read_buffer_size = 2M
read_rnd_buffer_size = 16M
sort_buffer_size = 8M
join_buffer_size = 8M
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 64M
innodb_buffer_pool_size = 4G
innodb_thread_concurrency = 8
innodb_flush_method = O_DIRECT
innodb_log_file_size = 512M
character_set_server = utf8
[client]
[mysqld_safe]
log-error=/var/log/mysql/mysql.log
pid-file=/var/run/mysql/mysql.pid
sql_mode=STRICT_ALL_TABLES
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
核心
server-id = 1
log_bin = /var/lib/mysql/mysql_master.log
备节点
my,cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
transaction-isolation = READ-COMMITTED
key_buffer_size = 32M
max_allowed_packet = 32M
thread_stack = 256k
thread_cache_size = 64
query_cache_limit = 8M
query_cache_size = 64M
query_cache_type = 1
max_connections = 550
server-id = 2
log_bin = /var/lib/mysql/mysql_slave.log
binlog_format = mixed
read_buffer_size = 2M
read_rnd_buffer_size = 16M
sort_buffer_size = 8M
join_buffer_size = 8M
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 64M
innodb_buffer_pool_size = 4G
innodb_thread_concurrency = 8
innodb_flush_method = O_DIRECT
innodb_log_file_size = 512M
character_set_server = utf8
[client]
[mysqld_safe]
log-error=/var/log/mysql/mysql.log
pid-file=/var/run/mysql/mysql.pid
sql_mode=STRICT_ALL_TABLES
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
核心
server-id = 2
log_bin = /var/lib/mysql/mysql_slave.log
重启主备节点mysqld服务
主节点创建于同步用户并展示同步信息
主节点执行命令
set global validate_password_policy=LOW;
grant replication slave on *.* to 'repl'@'%' identified by '123456';
flush privileges;
show master status;
备节点配置同步数据
备节点执行命令
change master to master_host='master_ip' ,master_user='repl', master_password='password',master_port=3306,master_log_file='mysql_master.000001',master_log_pos=437;
start slave;
show slave status\G;
注意
SQL_IO_RUNNING
Slave_SQL_Running_State


测试
主节点创建数据库、表
create database test;
use test;
CREATE TABLE persons
(
Id_P int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
备节点查看
show databases;
use test;
show tables;



浙公网安备 33010602011771号