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

# 重启从库节点

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

相关推荐
i道i5 小时前
MySQL win安装 和 pymysql使用示例
数据库·mysql
Oak Zhang6 小时前
sharding-jdbc自定义分片算法,表对应关系存储在mysql中,缓存到redis或者本地
redis·mysql·缓存
久醉不在酒8 小时前
MySQL数据库运维及集群搭建
运维·数据库·mysql
WindFutrue8 小时前
使用Mybatis向Mysql中的插入Point类型的数据全方位解析
数据库·mysql·mybatis
一只爱撸猫的程序猿9 小时前
一个简单的Linux 服务器性能优化案例
linux·mysql·nginx
计算机毕设源码qq-38365310419 小时前
(附项目源码)Java开发语言,215 springboot 大学生爱心互助代购网站,计算机毕设程序开发+文案(LW+PPT)
java·开发语言·spring boot·mysql·课程设计
袁庭新9 小时前
Cannal实现MySQL主从同步环境搭建
java·数据库·mysql·计算机·java程序员·袁庭新
爱学习的白杨树9 小时前
MySQL中有哪几种锁?
数据库·mysql
Stara051112 小时前
Git推送+拉去+uwsgi+Nginx服务器部署项目
git·python·mysql·nginx·gitee·github·uwsgi
不爱学习的啊Biao13 小时前
初识mysql数据库
数据库·mysql·oracle