Table of Contents
Preface
Data recovery
1. Turn on the binlog log
2. Check the binlog log
3. Restore data
每日备份
1.检查是否安装定时任务,安装crontab
2.设置定时任务
3.创建一个shell脚本
4.修改文件权限
5.打开定时任务文件
Home Database Mysql Tutorial Sad story, when the online database was accidentally deleted

Sad story, when the online database was accidentally deleted

Sep 12, 2020 pm 05:59 PM
database

Sad story, when the online database was accidentally deleted

Related learning recommendations: mysql tutorial

Preface

Due to the recent school season, our company I also do business related to colleges and universities, so I am quite busy and don’t have much time to write articles. Once people are too busy, they are prone to making mistakes while busy. As for me, just a few days ago, no, a friend of mine almost performed a deletion of the database and ran away when he was operating an online database a few days ago.

Sad story, when the online database was accidentally deleted

Note in advance: He is really my friend, not me. It's really my friend, not me. It's really my friend, not me. However, for the convenience of narrative, "my friend" will be referred to as "I" below.

The thing is like this. When I compared some table structures, I found that the table structures of this library and other libraries were quite different, so I thought that this environment was not in use, so I directly compared these tables. An overwriting operation was performed, and these tables happened to be related to the student wallets. Then at around 10 o'clock in the evening, the company's front-line staff reported in the group why the money in the wallet became 0.0. As soon as I saw this I was so frightened that I peeed and wondered if I should run away.

Sad story, when the online database was accidentally deleted

Fortunately, I have seen some database recovery plans and I still know a little bit about them.

Data recovery

1. Turn on the binlog log

I use the binlog log to recover data. To use binlog, you must first ensure that the binlog log is turned on. You can use command to view.

show variables like 'log_%';复制代码
Copy after login
Sad story, when the online database was accidentally deleted

You can see that it is in the ON state, indicating that it is turned on. If it is in the OFF state, in my.cnf [mysqld]After adding the configuration, restart the mysql service to enable it.

# my.cnf文件
[mysqld]
log-bin=mysql-bin
server-id=1复制代码
Copy after login

2. Check the binlog log

Make sure that after opening the binlog day, you can check the binlog log status through the command.

# 查看binlog日志的目录show master status;复制代码
Copy after login
Sad story, when the online database was accidentally deleted
# 查看binlog日志内容show binlog events IN 'mysql-bin.000002';复制代码
Copy after login
Sad story, when the online database was accidentally deleted
  • Log_name represents the binlog log name
  • Pos represents the pos starting point
  • Event_type represents the type of this operation
  • Server_id represents the machine id, which is specified by the configuration in my.cnf
  • End_log_pos represents the pos end point
  • Info represents the specific statement
Sad story, when the online database was accidentally deleted
# 进入存储binlog日志的文件木了,可以通过这条命令查看详情
 mysqlbinlog --base64-output=decode-rows -v mysql-bin.000002;复制代码
Copy after login
Sad story, when the online database was accidentally deleted
# 将binlog日志转为txt导出
mysqlbinlog --base64-output=decode-rows -v mysql-bin.000002 > sql.txt复制代码
Copy after login

3. Restore data

If you want to restore data from binlog log, command line recovery is commonly used, or The method is to copy the binlog log, use a binlog reading tool to convert it into a .sql file, and then copy all the SQL that needs to be used and run it again. The main focus here is the command line recovery method.

Command line recovery is mainly done by checking the binlog log, confirming the starting point and end point to be restored, and then entering the command to restore. Or estimate the time when you made a mistake and recover the data within a period of time.

# 通过起始点和结束点恢复    
mysqlbinlog --start-position="582" --stop-position="9414" mysql-bin.000002 | mysql -uroot -proot;复制代码
Copy after login
# 通过起始时间和结束时间来恢复,传入的时间可以是一个yyyy-MM-dd HH:mm:ss 的时间格式,也可以是一个时间戳
mysqlbinlog --start-datetime="2020-9-1 8:25:04" --stop-datetime="2020-9-1 20:00:00" mysql-bin.000002 | mysql -uroot -proot复制代码
Copy after login
Sad story, when the online database was accidentally deleted
Sad story, when the online database was accidentally deleted
##As you can see, after entering the recovery command, I cleared Database data is back.

4. Problem

Although the binlog log can recover data, there are also problems. When the binlog log continues to increase, it will be automatically deleted, which will result in the deleted part of the log. The data cannot be recovered.

However, the size and storage time of the binlog log can be set in mysql. However, it is still recommended to make daily backups to ensure nothing goes wrong.

# 设置文件大小,单位是字节,下面换算是100Mset global max_binlog_size=104857600;

# 设置文件保存天数,下面是保存7天,默认值为0,表示"没有自动删除"set global expire_logs_days = 7;复制代码
Copy after login

每日备份

对于线上环境来说,做好每日备份和binlog一起用才是王道。线上一般都是部署在linux上的,所以这里就简单列一下linux的定时备份方法。

1.检查是否安装定时任务,安装crontab

yum install crontabs复制代码
Copy after login

2.设置定时任务

/var/spool/cron/root  此文件为crontab定时任务,可通过crontab -e或者直接修改此文件修改.

crontab -l查看定时任务.

3.创建一个shell脚本

touch xxx.sh

mysqldump -uroot -p"密码" 数据库名 > /mysql/person_`date +%Y%m%d`.sql复制代码
Copy after login

4.修改文件权限

chmod 777 xxx.sh

5.打开定时任务文件

crontab -e

每天凌晨2点执行
00 2 * * * /xxx.sh复制代码
Copy after login

完结!

想了解更多编程学习,敬请关注php培训栏目!

The above is the detailed content of Sad story, when the online database was accidentally deleted. For more information, please follow other related articles on the PHP Chinese website!

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 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
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

How to connect to remote database using Golang? How to connect to remote database using Golang? Jun 01, 2024 pm 08:31 PM

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

How to handle database connections and operations using C++? How to handle database connections and operations using C++? Jun 01, 2024 pm 07:24 PM

Use the DataAccessObjects (DAO) library in C++ to connect and operate the database, including establishing database connections, executing SQL queries, inserting new records and updating existing records. The specific steps are: 1. Include necessary library statements; 2. Open the database file; 3. Create a Recordset object to execute SQL queries or manipulate data; 4. Traverse the results or update records according to specific needs.

See all articles