Mysql数据备份与恢复
备份/恢复策略: 1. 要定期做 mysql备份,并考虑系统可以承受的恢复时间。 2. 确保 mysql 打开log-bin,有了 binarylog,mysql才可以在必要的时候做完整恢复,或基于时间点的恢复,或基于位置的恢复。 3. 要经常做备份恢复测试,确保备份是有效的,并且是可以
备份/恢复策略:1. 要定期做 mysql备份,并考虑系统可以承受的恢复时间。
2. 确保 mysql 打开log-bin,有了 binarylog,mysql才可以在必要的时候做完整恢复,或基于时间点的恢复,或基于位置的恢复。
3. 要经常做备份恢复测试,确保备份是有效的,并且是可以恢复的。
冷备份
备份:
1. 停掉 mysql服务,在操作系统级别备份 mysql的数据文件。
2. 重启 mysql服务,备份重启以后生成的 binlog。
恢复:
1. 停掉 mysql服务,在操作系统级别恢复 mysql的数据文件。
2. 重启 mysql服务,使用 mysqlbinlog恢复自备份以来的 binlog。
逻辑备份
备份:
1. 选择在系统空闲时,比如在夜间,使用 mysqldump –F(flush-logs)备份数据
库。
mysqldump –u root –p*** pointcard –F > pointcard.sql
2. 并备份 mysqldump开始以后生成的 binlog。
恢复:
1. 停掉应用,执行 mysql 导入备份文件.
mysql –u root –p*** pointcard <p>2. 使用 mysqlbinlog 恢复自mysqldump 备份以来的binlog。<br> </p> <p> </p><p></p><pre class="brush:php;toolbar:false">mysqlbinlog $HOME/data/mysql-bin.123456 | mysql -u root –p***
单个表的备份:
备份:
1. 方法 1:
mysql > select * into outfile ‘/tmp/order_tab’ fields-terminated-by=’,’ from order_tab;
2. 方法 2:
mysqldump –u root –p*** –T /tmp pointcard order_tab --fields-terminated-by=’,’;
1. 方法 1:
mysql > load data [local] infile ‘/tmp/order_tab’ into table order_tab fields-terminated-by=’,’;
2. 方法 2:
mysqlimport –u root –p*** [--local] pointcatd order_tab.txt --fields-terminated-by=’,’;
注意:如果导入和导出是跨平台操作的(windows和 linux),那么要注意设置参数line- terminated-by, windows上设置为 line-terminated-by=’\r\n’,linux上设置为 line-terminated-by=’\n’.
使用备份工具ibbackup
ibbackup是 innodb公司(www.innodb.com)的一个热备份工具,专门对 innodb存储引擎进行物理热备份,此工具是收费的,不能免费使用。现在innodb 公司已经被oracle收购
使用方法:
编辑用于启动的配置文件my.cnf和用于备份的配置文件my2.cnf
my.cnf的例子如下:
[mysqld] datadir = /home/heikki/data innodb_data_home_dir = /home/heikki/data innodb_data_file_path = ibdata1:10M:autoextend innodb_log_group_home_dir = /home/heikki/data set-variable = innodb_log_files_in_group=2 set-variable = innodb_log_file_size=20M
如果想备份到/home/heikki/backup,则my2.cnf的例子如下:
[mysqld]datadir = /home/heikki/backupinnodb_data_home_dir = /home/heikki/backupinnodb_data_file_path = ibdata1:10M:autoextendinnodb_log_group_home_dir = /home/heikki/backupset-variable = innodb_log_files_in_group=2 set-variable = innodb_log_file_size=20M
开始备份ibbackup my.cnf my2.cnf如果需要恢复,则进行日志重做ibbackup --apply-log my2.cnf恢复后重启数据库服务./bin/mysqld_saft --defaults-file=my2.cnf &
时间点恢复:
1. 如果上午 10点发生了误操作,可以用以下语句用备份和 binglog将数据恢复到故
障前:
mysqlbinlog --stop-date="2005-04-20 9:59:59" /var/log/mysql/bin.123456 | mysql -u root –pmypwd
2. 跳过故障时的时间点,继续执行后面的 binlog,完成恢复
mysqlbinlog --start-date="2005-04-20 10:01:00" /var/log/mysql/bin.123456| mysql -u root -pmypwd \
位置恢复:
和时间点恢复类似,但是更精确,步骤如下:
mysqlbinlog --start-date="2005-04-20 9:55:00" --stop-date="2005-04-20 10:05:00" /var/log/mysql/bin.123456 > /tmp/mysql_restore.sql
该命令将在/tmp目录创建小的文本文件,编辑此文件,找到出错语句前后的位置号 ,例如前后位置号分别是368312 和368315。恢复了以前的备份文件后,你应从命令行输入下面内容:
mysqlbinlog --stop-position="368312" /var/log/mysql/bin.123456 \ | mysql -u root -pmypwd mysqlbinlog --start-position="368315" /var/log/mysql/bin.123456 \ | mysql -u root -pmypwd \
上面的第1 行将恢复到停止位置为止的所有事务。下一行将恢复从给定的起始位置
直到二进制日志结束的所有事务。因为mysqlbinlog 的输出包括每个SQL 语句记录
之前的SET TIMESTAMP 语句,恢复的数据和相关MySQL 日志将反应事务执行的原时
间。
MyISAM表修复:
一张损坏的表的症状通常是查询意外中断并且能看到下述错误:
? “tbl_name.frm”被锁定不能更改。
? 不能找到文件“tbl_name.MYI”(Errcode:nnn)。
? 文件意外结束。
? 记录文件被毁坏。
? 从表处理器得到错误nnn
解决方法如下:
方法一:
myisamchk -r tablename
myisamchk -o tablename
1) CHECK TABLE tbl_name [, tbl_name] ... [option] ... option = {QUICK | FAST | MEDIUM | EXTENDED | CHANGED} 2) REPAIR [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name] ... [QUICK] [EXTENDED] [USE_FRM]

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.

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.

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.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

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.

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.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.
