MySQL8.0.45主从搭建
安装忽略
主库配置
# cat /etc/my.cnf
[mysqld]
basedir=/data/mysql-8.0.45
datadir=/data/mysql-8.0.45/data
port=23306
socket=/data/mysql-8.0.45/mysql.sock
log-error=/data/mysql-8.0.45/data/error_mysqld.log
pid-file=/data/mysql-8.0.45/data/mysqld.pid
default-time-zone = +08:00
lc-messages-dir = /data/mysql-8.0.45/share
lc-messages = en_US
server_id=208
log-bin=mysql-bin
binlog_expire_logs_seconds = 604800 #为7天
max_binlog_size = 512M
innodb_buffer_pool_size = 16G
innodb_buffer_pool_instances = 8 # 多实例提高并发
innodb_file_per_table = ON
innodb_max_dirty_pages_pct = 75 # 减少突发刷盘
innodb_log_buffer_size = 64M # 日志缓冲区大小
innodb_redo_log_capacity = 1G
lower_case_table_names=1
max_connections=1000
wait_timeout=1800
interactive_timeout=1800 #单位s
skip-name-resolve = ON
#主从配置
gtid-mode=ON
enforce-gtid-consistency
log-replica-updates=ON
# 密码复杂度
#validate_password.policy = 1
#validate_password.length = 10
#validate_password.number_count = 1
#validate_password.mixed_case_count = 1
#validate_password.special_char_count = 1
#validate_password.check_user_name = ON
# 加载连接控制插件
#plugin-load-add=connection_control.so
# 强制永久启用(无法卸载,重启丢失)
#connection-control=FORCE_PLUS_PERMANENT
#connection-control-failed-login-attempts=FORCE_PLUS_PERMANENT
# 安全策略(等保推荐)
#connection_control_failed_connections_threshold=5 # 失败5次触发延迟
#connection_control_min_connection_delay=1000 # 最小延迟1秒
#connection_control_max_connection_delay=60000 # 最大延迟60秒
# 锁相关优化
innodb_lock_wait_timeout = 50 # 锁等待超时时间
innodb_deadlock_detect = ON # 死锁检测
innodb_print_all_deadlocks = ON # 记录所有死锁信息
# 慢查询日志
slow_query_log = ON
slow_query_log_file =/data/mysql/data/slow.log
long_query_time = 2
# 临时表存储在内存(避免磁盘临时表)
tmp_table_size = 64M
max_heap_table_size = 64M
innodb_temp_data_file_path=ibtmp1:12M:autoextend:max:12G
[mysql]
socket=/data/mysql/mysql.sock
[client]
socket=/data/mysql/mysql.sock
从库配置
# cat /etc/my.cnf
[mysqld]
basedir=/data/mysql-8.0.45
datadir=/data/mysql-8.0.45/data
port=23306
socket=/data/mysql-8.0.45/mysql.sock
log-error=/data/mysql-8.0.45/data/error_mysqld.log
pid-file=/data/mysql-8.0.45/data/mysqld.pid
default-time-zone = +08:00
lc-messages-dir = /data/mysql-8.0.45/share
lc-messages = en_US
server_id=82
log-bin=mysql-bin
binlog_expire_logs_seconds = 604800 #为7天
max_binlog_size = 512M
innodb_buffer_pool_size = 16G
innodb_buffer_pool_instances = 8 # 多实例提高并发
innodb_file_per_table = ON
innodb_max_dirty_pages_pct = 75 # 减少突发刷盘
innodb_log_buffer_size = 64M # 日志缓冲区大小
innodb_redo_log_capacity = 1G
lower_case_table_names=1
max_connections=1000
wait_timeout=1800
interactive_timeout=1800 #单位s
skip-name-resolve = ON
##主从配置
gtid_mode=ON
enforce-gtid-consistency=ON
relay-log=relay-bin
log-replica-updates=ON
replicate_wild_ignore_table = mysql.%
replicate_wild_ignore_table = sys.%
replicate_wild_ignore_table = information_schema.%
replicate_wild_ignore_table = performance_schema.%
##从库设置只读
#read_only = 1
#super_read_only = 1
# 密码复杂度
#validate_password.policy = 1
#validate_password.length = 10
#validate_password.number_count = 1
#validate_password.mixed_case_count = 1
#validate_password.special_char_count = 1
#validate_password.check_user_name = ON
# 加载连接控制插件
#plugin-load-add=connection_control.so
# 强制永久启用(无法卸载,重启丢失)
#connection-control=FORCE_PLUS_PERMANENT
#connection-control-failed-login-attempts=FORCE_PLUS_PERMANENT
# 安全策略(等保推荐)
#connection_control_failed_connections_threshold=5 # 失败5次触发延迟
#connection_control_min_connection_delay=1000 # 最小延迟1秒
#connection_control_max_connection_delay=60000 # 最大延迟60秒
# 锁相关优化
innodb_lock_wait_timeout = 50 # 锁等待超时时间
innodb_deadlock_detect = ON # 死锁检测
innodb_print_all_deadlocks = ON # 记录所有死锁信息
# 慢查询日志
slow_query_log = ON
slow_query_log_file =/data/mysql-8.0.45/data/slow.log
long_query_time = 2
# 临时表存储在内存(避免磁盘临时表)
tmp_table_size = 64M
max_heap_table_size = 64M
innodb_temp_data_file_path=ibtmp1:12M:autoextend:max:12G
[mysql]
socket=/data/mysql-8.0.45/mysql.sock
[client]
socket=/data/mysql-8.0.45/mysql.sock
主从库创建同步用户
SQL> create user repl@'%' identified with 'mysql_native_password' by 'repl@123';
SQL> grant replication slave on *.* to 'repl'@'%';
SQL> exit;
从库执行
CHANGE REPLICATION SOURCE TO
SOURCE_HOST='10.10.1.1',
SOURCE_PORT=23306,
SOURCE_USER='repl',
SOURCE_PASSWORD='repl@123',
SOURCE_AUTO_POSITION = 1,
GET_SOURCE_PUBLIC_KEY=1;
start replica;
show replica status\G