线上mysql数据库不停机的环境下,如何添加新的从机
更多博文请关注:没有伞的孩子必须努力奔跑(www.xuchanggang.cn)在工作中,主从环境搭的多了,但是,基本上都是在DBSERVER停机(游戏公司)的情况下搭建的,今天突
在工作中,主从环境搭的多了,但是,基本上都是在DB SERVER停机(游戏公司)的情况下搭建的,今天突然被一技术官问,如何在线添加主从,回答的大概思路,但是没有实践过,下面,我就测试一下。各位也可以先想想自己的思路:mysql 5.1版本,二进制日志文件(时间长了,有些二进制日志定期清除了),pos号
注:在mysql 5.6版本中,已经有基于GTID的主从复制(即:不需要知道日志文件和position号),只需还原最新的备份就可实现,,这里只讨论mysql 5.1
一.目前的基本环境:
主DB:192.168.1.101
从DB1:192.168.1.102
从DB2:192.168.1.103(准备在线添加做为从机的mysql数据库)
数据库版本:mysql 5.1.61(select version())
服务器版本:red hat linux 6
注:目前主从正常(show slave status\G),同步的数据库为 login
3台DB server的备份软件(xtrbackup)都已经安装完成
同步的表为innodb引擎的表,不然用xtrabackup还是会锁表
假设环境为:现在login库下的表a,备份的时候插入数据,最终行为5418272
注:思路是备份,可是用mysqldump备份会锁表,如果数据量很大的话,线上环境肯定不允许使用mysqldump备份
所以,我们采用的xtrabackup来进行在线热备,至于,它的原理,请各位自行google
二.部署实例:
1.主DB,线上数据正常写入的同时我实施备份
mysql> insert into a select * from a; [root@client101 backup]# innobackupex --user=root --password=kongzhong --defaults-file=/etc/my.cnf --port=3306 --database=login /tmp/backup/ >/tmp/backup/innoback.log 2>&1 # 备份完成后,可以查看备份文件目录下, xtrabackup_binlog_info 此文件会记录主上的日志和position号 [root@client101 2014-02-22_07-43-36]# cat xtrabackup_binlog_info
2.将备份文件压缩,并传送到从DB2(192.168.1.103)上,用于在上面进行还原
[root@client101 backup]# tar zcvf login.tar 2014-02-22_07-43-36 [root@client101 backup]# scp login.tar 192.168.1.103:/tmp/
3.从DB2上,解压刚才的备份,并用于恢复
[root@client103 tmp]# tar -xf login.tar # 应用日志,保证数据一致 [root@client103 tmp]# innobackupex --apply-log /tmp/2014-02-22_07-43-36/ # 停mysql [root@client103 mysql]# /etc/init.d/mysqld stop # mysql数据目录下的所有文件移到/tmp/mysqlbackup/下,不然,恢复有问题 # [这里建议只复制数据文件,不建议复制(日志,索引,innodb共享表空间,innodb日志文件,慢日志等)] [root@client103 mysql]# mv /var/lib/mysql/* /tmp/mysqlback # 恢复数据 [root@client103 mysql]# innobackupex --copy-back /tmp/2014-02-22_07-43-36/ # 将原数据库的内容复制mysql数据目录(这里就是我认为这个软件不好的地方,大家也可以给点建议) [root@client103 mysqlback]# mv /tmp/mysqlbac k/* /var/lib/mysql/ # 将数据目录的权限修改为mysql权限 [root@client103 mysql]# chown mysql. /var/lib/mysql/ # 启动数据库 [root@client103 mysql]# /etc/init.d/mysqld start # 验证数据的一致性:5418272(主上的行) mysql> select count(*) from a; +----------+ | count(*) | +----------+ | 5418272 | +----------+
4.将从DB2的主机指向主DB1
# 刚才回复后,会有个文件:xtrabackup_binlog_pos_innodb --> 此文件记录了备份时日志文件和pos号 [root@client103 mysql]# cat xtrabackup_binlog_pos_innodb /var/lib/mysql/mysql-101-bin.000008 2423 # 建立主从关系 mysql> change master to master_host='192.168.1.101', -> master_user='slave', -> master_password='slave', -> master_log_file='mysql-101-bin.000008', -> master_log_pos=2423; # 开启同步 mysql> start slave; # 查看同步状态 mysql> show slave status\G;
5.此时如果都为yes,则主从则建立完毕!大家可自行测试
注:
还有一种方式在线添加主从,即原本的从机只做冗余,可以在从机暂时停止主从,
然后,在从机上执行备份,看当前从机上,日志执行到哪个文件,哪个位置(master的日志文件和pos号)
如果各位对此文章有异议或有更好的方式,欢迎反馈,大家共同进步!
本文出自 “没有伞的孩子必须努力奔跑” 博客,请务必保留此出处

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

BITGet is a cryptocurrency exchange that provides a variety of trading services including spot trading, contract trading and derivatives. Founded in 2018, the exchange is headquartered in Singapore and is committed to providing users with a safe and reliable trading platform. BITGet offers a variety of trading pairs, including BTC/USDT, ETH/USDT and XRP/USDT. Additionally, the exchange has a reputation for security and liquidity and offers a variety of features such as premium order types, leveraged trading and 24/7 customer support.

Ouyi OKX, the world's leading digital asset exchange, has now launched an official installation package to provide a safe and convenient trading experience. The OKX installation package of Ouyi does not need to be accessed through a browser. It can directly install independent applications on the device, creating a stable and efficient trading platform for users. The installation process is simple and easy to understand. Users only need to download the latest version of the installation package and follow the prompts to complete the installation step by step.

Gate.io is a popular cryptocurrency exchange that users can use by downloading its installation package and installing it on their devices. The steps to obtain the installation package are as follows: Visit the official website of Gate.io, click "Download", select the corresponding operating system (Windows, Mac or Linux), and download the installation package to your computer. It is recommended to temporarily disable antivirus software or firewall during installation to ensure smooth installation. After completion, the user needs to create a Gate.io account to start using it.

Ouyi, also known as OKX, is a world-leading cryptocurrency trading platform. The article provides a download portal for Ouyi's official installation package, which facilitates users to install Ouyi client on different devices. This installation package supports Windows, Mac, Android and iOS systems. Users can choose the corresponding version to download according to their device type. After the installation is completed, users can register or log in to the Ouyi account, start trading cryptocurrencies and enjoy other services provided by the platform.

Gate.io is a highly acclaimed cryptocurrency trading platform known for its extensive token selection, low transaction fees and a user-friendly interface. With its advanced security features and excellent customer service, Gate.io provides traders with a reliable and convenient cryptocurrency trading environment. If you want to join Gate.io, please click the link provided to download the official registration installation package to start your cryptocurrency trading journey.
