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
相关推荐
笑梦无境2 小时前
mysql的安装及配置(3)
数据库·mysql·adb
!chen7 小时前
数据库主从有延迟怎么处理
数据库·adb
2501_915106322 天前
安卓抓包软件2026,免证书抓包 应用层抓包 代理抓包全解析
网络协议·计算机网络·网络安全·ios·adb·https·udp
ymwlchina2 天前
华为手机P40 Plus如何通过adb无线连接
adb·华为·智能手机
00后程序员张3 天前
Android证书绑定抓包失败?Android SSL Pinning绕过实战指南
android·网络协议·计算机网络·网络安全·adb·https·ssl
九皇叔叔5 天前
MySQL ORDER BY 性能优化详解:Using filesort、联合索引、ASC/DESC 与覆盖索引
android·adb
firstacui6 天前
Mysql调优
android·mysql·adb
醉颜凉7 天前
MySQL 8 忘记 root 密码?这两种方法帮你轻松解决
数据库·mysql·adb
2601_962063237 天前
【Spring Boot】掌握 Spring 事务:隔离级别与传播机制解读与应用
spring boot·spring·adb