mysql主从同步

1 mysql主从

1.1 主节点

1.1.1 配置

bash 复制代码
[client]
port = 3306
socket = /data/mysql_3306/tmp/mysql.sock
#default-character-set = utf8
default-character-set = utf8mb4

[mysqld]
port            = 3306
socket          = /data/mysql_3306/tmp/mysql.sock
#log            = /data/mysql_3306/logs/mysql.log

slow_query_log = 1
slow_query_log_file             = /data/mysql_3306/logs/sql_query_slow.log


basedir=/data/mysql_3306
datadir=/data/mysqldata_3306 
skip-external-locking
key_buffer_size = 256M
table_open_cache = 4096 
sort_buffer_size = 2M
read_buffer_size = 1M
read_rnd_buffer_size = 64M
net_buffer_length = 2K
thread_stack = 256K
query_cache_type=2
query_cache_size = 1024M
query_cache_limit=2M
lower_case_table_names=1
#character_set_server =  utf8
log_timestamps = SYSTEM
log_bin_trust_function_creators=1  
############################################
character-set-client-handshake = FALSE 
character-set-server = utf8mb4 
collation-server = utf8mb4_bin
init_connect='SET NAMES utf8mb4'
############################################
max_prepared_stmt_count=100000
max_connections=10000
bulk_insert_buffer_size = 128M
binlog_cache_size = 32M
tmp_table_size = 256M
max_heap_table_size =256M
thread_cache_size = 32
# 每个服务器必须有一个唯一的ID
server-id = 1
# 开启二进制日志,指定日志文件名前缀
log-bin=mysql-bin
# 设置binlog格式为行级别
binlog_format = ROW
binlog_rows_query_log_events = on
binlog_row_image = full
innodb_support_xa=1
sync_binlog=100
slow_query_log
long_query_time = 1
#binlog-ignore-db = cattle
default-storage-engine=InnoDB
innodb_buffer_pool_size = 24096M 
innodb_log_file_size = 1024M
innodb_log_buffer_size = 4M
innodb_flush_log_at_trx_commit = 2
innodb_lock_wait_timeout = 50
innodb_buffer_pool_instances=23
skip-name-resolve
skip-host-cache
max_allowed_packet = 134217728
innodb_log_files_in_group = 3
innodb_thread_concurrency=16
expire_logs_days = 30  
innodb_file_per_table=1   
wait_timeout=7200
join_buffer_size=4M
######### 
innodb_print_all_deadlocks = 1
# by 5.7 slave
#slave_parallel_type=LOGICAL_CLOCK
#slave_parallel_workers=16 
#master_info_repository=TABLE
#relay_log_info_repository=TABLE
#relay_log_recovery=ON
#slave_preserve_commit_order=1
#sql_mode=ONLY_FULL_GROUP_BY,
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ALLOW_INVALID_DATES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
######################################################

[mysqldump]
quick
max_allowed_packet = 2048M

[mysql]
#######################################
default-character-set = utf8mb4
#######################################
no-auto-rehash

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

1.1.2 设置开机启动

bash 复制代码
cat > /usr/lib/systemd/system/mysql_3306.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql
ExecStart=/data/mysql_3306/bin/mysqld --defaults-file=/etc/my_3306.cnf
#LimitNOFILE = 5000
LimitNOFILE=65535
Restart=always
#Restart=on-failure
#RestartPreventExitStatus=1
#PrivateTmp=false
EOF

因为我之前一台机器上可以装多个mysql,所以指定了不同端口

bash 复制代码
systemctl start firewalld
systemctl enable firewalld
systemctl status firewalld
# 开放端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --zone=public --add-port=3308/tcp --permanent
# 生效
firewall-cmd --reload
firewall-cmd --list-ports

useradd mysql
chown -R mysql:mysql mysqldata_3306
chown -R mysql:mysql mysqldata_3308

cd /data/mysql_3306
ln -s /etc/my_3306.cnf my.cnf
mkdir logs
mkdir tmp
mkdir pids
chown -R mysql:mysql tmp
chown -R mysql:mysql pids
chown -R mysql:mysql logs

systemctl daemon-reload
systemctl enable mysql_3306
systemctl start mysql_3306

1.1.3 设置复制用户

bash 复制代码
GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;

1.1.4 查看master状态

执行show master status;

1.2 从节点

1.2.1 配置

bash 复制代码
[client]
port = 3306
socket = /data/mysql_3306/tmp/mysql.sock
#default-character-set = utf8
default-character-set = utf8mb4

[mysqld]
port            = 3306
socket          = /data/mysql_3306/tmp/mysql.sock
#log            = /data/mysql_3306/logs/mysql.log

slow_query_log = 1
slow_query_log_file             = /data/mysql_3306/logs/sql_query_slow.log

basedir=/data/mysql_3306
datadir=/data/mysqldata_3306 
skip-external-locking
key_buffer_size = 256M
table_open_cache = 4096 
sort_buffer_size = 2M
read_buffer_size = 1M
read_rnd_buffer_size = 64M
net_buffer_length = 2K
thread_stack = 256K
query_cache_type=2
query_cache_size = 1024M
query_cache_limit=2M
lower_case_table_names=1
#character_set_server =  utf8
log_timestamps = SYSTEM
log_bin_trust_function_creators=1  
############################################
character-set-client-handshake = FALSE 
character-set-server = utf8mb4 
collation-server = utf8mb4_bin
init_connect='SET NAMES utf8mb4'
############################################
max_prepared_stmt_count=100000
max_connections=10000
bulk_insert_buffer_size = 128M
binlog_cache_size = 32M
tmp_table_size = 256M
max_heap_table_size =256M
thread_cache_size = 32
#  每个服务器必须有一个唯一的ID
server-id = 2
# 开启二进制日志,指定日志文件名前缀
log-bin=mysql-bin
# 设置relay log的名字
relay-log=mysql-relay
# 设置binlog格式为行级别
binlog_format = ROW
binlog_rows_query_log_events = on
binlog_row_image = full
innodb_support_xa=1
sync_binlog=100
slow_query_log
long_query_time = 1
#binlog-ignore-db = cattle
default-storage-engine=InnoDB
innodb_buffer_pool_size = 24096M 
innodb_log_file_size = 1024M
innodb_log_buffer_size = 4M
innodb_flush_log_at_trx_commit = 2
innodb_lock_wait_timeout = 50
innodb_buffer_pool_instances=23
skip-name-resolve
skip-host-cache
max_allowed_packet = 134217728
innodb_log_files_in_group = 3
innodb_thread_concurrency=16
expire_logs_days = 30  
innodb_file_per_table=1   
wait_timeout=7200
join_buffer_size=4M
######### 
innodb_print_all_deadlocks = 1
# by 5.7 slave
slave_parallel_type=LOGICAL_CLOCK
slave_parallel_workers=16 
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log_recovery=ON
slave_preserve_commit_order=1
log_slave_updates=1
sql_mode=ONLY_FULL_GROUP_BY
#sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ALLOW_INVALID_DATES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
######################################################

[mysqldump]
quick
max_allowed_packet = 2048M

[mysql]
#######################################
default-character-set = utf8mb4
#######################################
no-auto-rehash

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

1.2.2 启动从库

在从库的mysql中执行这些命令。

bash 复制代码
CHANGE MASTER TO
MASTER_HOST='10.101.13.55',
MASTER_USER='replication',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000103',  # 根据SHOW MASTER STATUS的结果填写
MASTER_LOG_POS=78177991; 

#重启从库
start slave;

show slave status;

如果是mysql是复制过来的,从库启动可能会提示下面的错误

bash 复制代码
Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.

修改auto.cnf中的uuid。然后重启从库即可。

bash 复制代码
# 产生新的uuid
new_uuid=$(uuidgen)
echo $new_uuid

# 在/data/mysqldata_3308/auto.cnf中配置
[mysqld]
server_uuid=de59364b-4212-4735-b160-2385e0aa73c5

# 重启从库节点

经过验证在主库中创建库,或者添加数据,数据自动从主库会同步到从库。

相关推荐
Ai 编码助手39 分钟前
MySQL中distinct与group by之间的性能进行比较
数据库·mysql
白云如幻1 小时前
MySQL排序查询
数据库·mysql
苹果醋31 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
stars_User2 小时前
MySQL数据库面试题(下)
数据库·mysql
Yaml42 小时前
智能化健身房管理:Spring Boot与Vue的创新解决方案
前端·spring boot·后端·mysql·vue·健身房管理
yanwushu3 小时前
Xserver v1.4.2发布,支持自动重载 nginx 配置
mysql·nginx·php·个人开发·composer
蓝眸少年CY3 小时前
MySQL 【流程控制】函数
mysql
掘金-我是哪吒4 小时前
微服务mysql,redis,elasticsearch, kibana,cassandra,mongodb, kafka
redis·mysql·mongodb·elasticsearch·微服务
zmgst5 小时前
canal1.1.7使用canal-adapter进行mysql同步数据
java·数据库·mysql
令狐少侠20115 小时前
explain执行计划分析 ref_
mysql