当前位置:网站首页>MySQL 的主从复制配置
MySQL 的主从复制配置
2022-04-23 11:36:00 【我和你】
摘要
大型项目对备份尤为关注,一般有双机备份,热备冷备,异地灾备等等… 今天来说一下两台服务器上的 MySQL 主从复制备份,需求比较简单:从要同步主的数据,但也不用太频繁,保持 15 分钟的数据差即可,意思就是每 15 分钟去同步一次修改过的数据。
正文
我们这里环境是两台 Linux 服务器,一台在公网,一台在内网,公网是主,内网是从,数据库都是 MySQL 5.7(使用 LNMP 安装)。
主库操作
首先我们先新建一个数据同步用户,让从库使用这个用户可以来同步数据
CREATE USER 'slave'@'%' IDENTIFIED BY '密码';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
百分号代表该用户可以在任意 IP 登录。
然后我们需要把主库备份下来,如果你的库此时并没有访问,那么可以不用锁表,但最好先锁表再备份
mysql> flush tables with read lock;
使用 mysqldump 备份需要同步的库,我这里是同步 mikublog 这个库
mysqldump -uroot -p --databases mikublog | gzip > mysql_bak.$(date + %F)sql.gz
备份好之后记录一下当前位置,记住 File 和 Position 的值
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000014 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
最后解锁表
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
从库操作
把刚刚备份的压缩包拷贝过来,并解压:gzip -d 压缩包名字,然后把 sql 刷到数据库里去。
修改 my.cnf,一般只需要改两个值,把 server-id 改为唯一值
[mysqld]
log-bin=mysql-slave-bin
server-id = 2
改好之后重启数据库
service mysql restart
设置主从配置
mysql> change master to master_host='192.168.100.110', master_user='slave', master_password='mypassword', master_port=3306, master_log_file='mysql-bin.000014', master_log_pos= 154, master_delay=900, master_connect_retry=30;
各参数含义:
master_host :主库的地址
master_port:主库的端口号
master_user:用于数据同步的用户
master_password:用于同步的用户的密码
master_log_file:指定 Slave 从哪个日志文件开始复制数据,即上文中提到的 File 字段的值
master_log_pos:从哪个 Position 开始读,即上文中提到的 Position 字段的值
master_delay:延迟复制配置,设置从数据库延时 N 秒后,再与主数据库进行数据同步复制
master_connect_retry:如果连接失败,重试的时间间隔,单位是秒,默认是 60 秒
开启主从复制
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
查看同步状态
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
...
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
...
1 row in set (0.00 sec)
SlaveIORunning 和 SlaveSQLRunning 都是 Yes 说明主从复制已经开启。
完结
以上就是MySQL 的主从复制配置的内容,欢迎小伙伴们交流讨论。
版权声明
本文为[我和你]所创,转载请带上原文链接,感谢
https://cloud.tencent.com/developer/article/1986093
边栏推荐
- redis优化系列(二)Redis主从原理、主从常用配置
- 简易投票系统数据库设计
- Compress the curl library into a sending string of utf8 and send it with curl library
- Yunna | how to manage the company's fixed assets and how to manage fixed assets
- 卷积层和池化层总结
- qt5. 8. You want to use SQLite in the 64 bit static library, but the static library has no method to compile the supporting library
- golang之筆試題&面試題01
- Pytorch neural network trainer
- 分享两个实用的shell脚本
- 少儿编程结构的改变之路
猜你喜欢
Summary of the relationship among GPU, CUDA and cudnn
赛微微电科创板上市破发:跌幅达26% 公司市值44亿
解析性能良好的机器人使用守则
解决由于找不到amd_ags_x64.dll,无法继续执行代码。重新安装程序可能会解决此问题,地平线(Forza Horizon 5)
Learn go language 0x04: Code of exercises sliced in go language journey
SOFA Weekly | 年度优秀 Committer 、本周 Contributor、本周 QA
分享两个实用的shell脚本
Yunna | how to manage the company's fixed assets and how to manage fixed assets
The way to change children's programming structure
Nacos Basics (5): getting started with Nacos configuration
随机推荐
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
Practical data Lake iceberg lesson 30 MySQL - > iceberg, time zone problems of different clients
redis优化系列(二)Redis主从原理、主从常用配置
Tensorflow使用keras创建神经网络的方法
Applet payment
nacos基础(8):登录管理
Nacos Foundation (6): Nacos configuration management model
MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题
Database design of forum system
PSCP 基本使用
Laravel adds custom helper functions
nacos基础(7):配置管理
MySQL interview questions explain how to set hash index
解决由于找不到amd_ags_x64.dll,无法继续执行代码。重新安装程序可能会解决此问题,地平线(Forza Horizon 5)
Redis learning 5 - high concurrency distributed lock practice
Significance of actively participating in middle school robot competition
5个免费音频素材网站,建议收藏
GPU, CUDA,cuDNN三者的關系總結
Understanding of MQ
Usage record of map < qstring, bool >