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.

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.

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_%';复制代码

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复制代码
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;复制代码

# 查看binlog日志内容show binlog events IN 'mysql-bin.000002';复制代码

- 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

# 进入存储binlog日志的文件木了,可以通过这条命令查看详情 mysqlbinlog --base64-output=decode-rows -v mysql-bin.000002;复制代码

# 将binlog日志转为txt导出 mysqlbinlog --base64-output=decode-rows -v mysql-bin.000002 > sql.txt复制代码
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;复制代码
# 通过起始时间和结束时间来恢复,传入的时间可以是一个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复制代码


# 设置文件大小,单位是字节,下面换算是100Mset global max_binlog_size=104857600; # 设置文件保存天数,下面是保存7天,默认值为0,表示"没有自动删除"set global expire_logs_days = 7;复制代码
每日备份
对于线上环境来说,做好每日备份和binlog一起用才是王道。线上一般都是部署在linux上的,所以这里就简单列一下linux的定时备份方法。
1.检查是否安装定时任务,安装crontab
yum install crontabs复制代码
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复制代码
4.修改文件权限
chmod 777 xxx.sh
5.打开定时任务文件
crontab -e
每天凌晨2点执行 00 2 * * * /xxx.sh复制代码
完结!
想了解更多编程学习,敬请关注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!

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











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

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.

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())

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.

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.

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.

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.

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.
