MySQL8.0.45主从搭建

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
相关推荐
行者-全栈开发6 天前
SpringBoot API 网关实战:Spring Cloud Gateway 路由/限流/熔断/鉴权一体化方案
spring boot·后端·adb
MSTcheng.7 天前
【MySQL】MySQL数据库——表的操作
数据库·mysql·adb
小Ti客栈11 天前
MySQL 常用函数速查(完整篇)
数据库·mysql·adb
2035去旅行14 天前
ADB的底层原理
linux·adb·嵌入式·arm·soc
小呆爱发呆16 天前
第三章 MySQL 库表操作
数据库·mysql·adb
Starry-sky(jing)18 天前
Vidda ADB 开启:R Pro 65 找不到开发者选项的处理与连接验收
android·adb·远程控制·电视·vidda
韩曙亮18 天前
【错误记录】安卓 adb 调试 5037 端口占用排查 ( adb 的三层结构 | 找出本机占用 5037 的进程 )
android·数据库·adb·安卓·错误记录
科小怪22 天前
ADB工具下载安装与常用命令:一条数据线连上电脑,就能给手机装应用、传文件、查日志
adb·智能手机·电脑
张小姐的猫24 天前
【MySQL】访问 & 数据流动原理
android·数据库·mysql·adb·1024程序员节