Home Database Mysql Tutorial 线上mysql数据库不停机的环境下,如何添加新的从机

线上mysql数据库不停机的环境下,如何添加新的从机

Jun 07, 2016 pm 05:36 PM
linux mysql master-slave replication

更多博文请关注:没有伞的孩子必须努力奔跑(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号)


                   如果各位对此文章有异议或有更好的方式,欢迎反馈,大家共同进步!

本文出自 “没有伞的孩子必须努力奔跑” 博客,请务必保留此出处

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

MySQL: The Database, phpMyAdmin: The Management Interface MySQL: The Database, phpMyAdmin: The Management Interface Apr 29, 2025 am 12:44 AM

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

How to understand DMA operations in C? How to understand DMA operations in C? Apr 28, 2025 pm 10:09 PM

DMA in C refers to DirectMemoryAccess, a direct memory access technology, allowing hardware devices to directly transmit data to memory without CPU intervention. 1) DMA operation is highly dependent on hardware devices and drivers, and the implementation method varies from system to system. 2) Direct access to memory may bring security risks, and the correctness and security of the code must be ensured. 3) DMA can improve performance, but improper use may lead to degradation of system performance. Through practice and learning, we can master the skills of using DMA and maximize its effectiveness in scenarios such as high-speed data transmission and real-time signal processing.

How to handle high DPI display in C? How to handle high DPI display in C? Apr 28, 2025 pm 09:57 PM

Handling high DPI display in C can be achieved through the following steps: 1) Understand DPI and scaling, use the operating system API to obtain DPI information and adjust the graphics output; 2) Handle cross-platform compatibility, use cross-platform graphics libraries such as SDL or Qt; 3) Perform performance optimization, improve performance through cache, hardware acceleration, and dynamic adjustment of the details level; 4) Solve common problems, such as blurred text and interface elements are too small, and solve by correctly applying DPI scaling.

macOS vs. Linux: Exploring the Differences and Similarities macOS vs. Linux: Exploring the Differences and Similarities Apr 25, 2025 am 12:03 AM

macOSandLinuxbothofferuniquestrengths:macOSprovidesauser-friendlyexperiencewithexcellenthardwareintegration,whileLinuxexcelsinflexibilityandcommunitysupport.macOS,developedbyApple,isknownforitssleekinterfaceandecosystemintegration,whereasLinux,beingo

Steps to add and delete fields to MySQL tables Steps to add and delete fields to MySQL tables Apr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

See all articles