Xtrabackup备份mysql实战(做从库全过程)
一、背景 线上的一个库一直都是RD他们自己维护的,由于业务的增长,现在数据已经500G+了,急需做一个从库,登陆机器一瞅,坑爹啊,还是mysql5.6(我们维护的都是5.5),mysql5.6还没有搞过呢,还有就是他这安装的都不是按我们运维的标准安装的,我的第一反应
一、背景
线上的一个库一直都是RD他们自己维护的,由于业务的增长,现在数据已经500G+了,急需做一个从库,登陆机器一瞅,坑爹啊,还是mysql5.6(我们维护的都是5.5),mysql5.6还没有搞过呢,还有就是他这安装的都不是按我们运维的标准安装的,我的第一反应就是这活没法干,这个库又不容许停库太长时间,于是就度娘、QQ群各种请教,最终确定用Xtrabackup试一把,一直在听大家说Xtrabackup这好那好的,却从来没有玩过,我觉得数据备份(用于做从库)还是冷备最好,速度快,还最能保证数据的一致性,但是今天这种情形貌似行不通了,Xtrabackup搞起!!!
二、Xtrabackup相关资料学习
Xtrabackup的有点:
1、备份完成快速、可靠 2、备份期间不间断的事务处理 3、节省磁盘空间和网络带宽 4、自动备份验证 5、提高正常运行时间由于更快的恢复时间
我个人觉的比较好的就是备份不需要锁表(innodb),备份MyISAM的表还是会锁的,还支持增量备份,总之这工具值得赞。
参考文档:
http://www.percona.com/doc/percona-xtrabackup/2.1/
http://www.baidu-ops.com/2013/05/26/xtrabackup/
三、实施
1、环境:
172.28.29.152 主库 172.28.26.138 从库(待做)
2、主库上安装Xtrabackup
yum install perl-Time-HiRes* yum install perl-DBD-MySQL -y wget http://www.percona.com/redir/downloads/XtraBackup/LATEST/RPM/rhel6/x86_64/percona-xtrabackup-2.1.9-744.rhel6.x86_64.rpm rpm -ivh percona-xtrabackup-2.1.9-744.rhel6.x86_64.rpm
3、备份主库
innobackupex --defaults-file=/usr/local/mysql/my.cnf --user root --socket=/data/log/mysql.sock --password 123456 /data/xtrabackup/ & ##备份 ps:备份完成了,会在/data/xtrabackup/目录下生成一个时间点目录,你也可以加一个--no-timestamp参数不生产这个目录,直接备份到 /data/xtrabackup/下 innobackupex --defaults-file=/usr/local/mysql/my.cnf --user root --socket=/data/log/mysql.sock --password 123456 --apply-log /data/xtrabackup/2014-06-04_10-20-55/ & ##preparing,undo撤销未提交的事务,重放redo log
ps:1、ll /data/xtrabackup/2014-06-04_10-20-55/查看,你发现xtrabackup会把你数据库datadir下的所有数据文件都复制过来还新增了5个xtrabackup_开头的文件,我们关心的是xtrabackup_binlog_info这个文件,因为这个文件里面记录了你做从库需要change到主库的binlog的位置和pos点
2、如果只想备份一个库,加“--include=navy”指定库名即可, “--databases=navy”不好使,被坑过。
4、从库安装mysql5.6
主库的mysql是RD编译安装的,路径/usr/local/mysql/,那么从库你还苦逼的再编译一次吗?不用了,直接把主库的安装目录scp过来就可以用,my.cnf也scp过来,日志和datadir目录的宿主一定要是mysql哦,之后就可以初始化启动库了。
scp -r /usr/local/mysql/ 172.28.26.138:/usr/local/ chown mysql:mysql /data/log/ -R ##日志和pid存放目录 chown mysql:mysql /data/mysql/ -R ##datadir /usr/local/mysql/my.cnf 中添加 server-id = 2 /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/ 初始化数据库 /usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/my.cnf --user=mysql & 启动数据库 /usr/local/mysql/bin/mysqladmin --sock=/data/log/mysql.sock -p123456 shutdown 停库,把datadir目录清空,执行下面步骤
PS:主库竟然没有设置server-id,这不是坑爹嘛,别急,这个参数是动态的,可以在线调整,set global server_id=1;就ok了
5、从库服务器把主库备份文件拉到data_dir下
rsync -av 172.28.29.152::xtrabackup/2014-06-04_10-20-55/ /data/mysql/ &
6、启动库,搭建主从,
/usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/my.cnf --user=mysql & cat /data/mysql/xtrabackup_binlog_info mysql-bin.008370 147669327 change master to master_host="172.28.29.152", master_port=3306, master_user='slave', master_password='123456', master_log_file='mysql-bin.008370', master_log_pos=147669327;
到此工作已经完成:

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

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

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

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.
