MySQL database log management, backup and recovery

One, MySQL log management

MySQL's default log storage location is /usr/local/mysql/data

There are two ways to open the log:

  • Through the configuration file or through the command
  • The log opened by command modification is temporary and will be closed after closing or restarting the service
(1) MySQL common log types and opening

Error log
Used to record error messages that occur when MySQL is started, stopped, or running. It is enabled by default

指定日志的保存位置和文件名
log-error=/usr/local/mysql/data/mysql_error.log	

General query log
Used to record all connections and statements of MySQL, it is closed by default

general_log=ON
general_log_file=/usr/local/mysql/data/mysql_general.log

Binary log (binlog)
Used to record all error messages sent when MySQL is started, stopped or running, it is closed by default

log-bin=mysql-bin / log_bin=mysql-bin

Slow query log
It is used to record all statements whose execution time exceeds long_query_time seconds. You can find out which query statements have a long execution time for optimization. The default is off

slow_query_log=ON
slow_query_log_file=/usr/local/mysql/data/mysql_slow_query.log
long_query_time=5	   		

systemctl restart mysqld    

Two, log status

Check whether the general query log is enabled

show variables like 'general%';

Insert picture description here
Check whether the binary log is turned on

show variables like 'log_bin%';

Insert picture description here
Check whether the slow query day function is enabled

show variables like '%slow%';	

Insert picture description here
View slow query time settings

show variables like 'long_query_time';

Insert picture description here
Set the method to start slow query in the database

set global slow_query_log=ON;
该方法重启服务失效

Three, MySQL backup and recovery

(1) The importance of data backup
  • The main purpose of backup is disaster recovery
  • In a production environment, data security is critical
  • Any loss of data may have serious consequences

Reasons for data loss:

  • Program error
  • Human error
  • Operation error
  • Disk failure
  • Disaster (fire, earthquake, theft, etc.)
(2) Classification of database backup

Physical backup: backup of physical files (such as data files, log files, etc.) of the database operating system

Physical backup method:
cold backup (offline backup): is performed when the database is closed.
Hot backup (online backup): the database is running and depends on the database log file.
Warm backup: database lock table (cannot be written but can be Backup operation in the state of reading)

Logical backup: backup of database logical components (such as tables and other database objects)

From the perspective of database backup strategy, backup can be divided into:
Full backup: make a complete backup of the database every time

A full backup is a backup of the entire database, database structure and file structure.
What is saved is the database at the time the backup was completed. It is the basis of differential backup and incremental backup. Equivalent to the cornerstone.

Differential backup: Back up files that have been modified since the last full backup

Incremental backup: only the files modified after the last full backup or incremental backup will be backed up

Three, common backup methods

Physical cold standby

The database is closed during the backup, and the database file is directly packaged. The backup speed is fast. It is also the easiest way to close the MySQL database. Use the tar command to directly package the database folder and
directly replace the existing MySQL directory.

Backup tool mydump or mysqlhotcopy

mysqldump is a commonly used logical backup tool. The backup tool that comes with MySQL can realize the backup of MySQL. You can export the specified library and table as SQL script.
Use the command mysql to import the backup data

mysqlhotcopy only has backup myisam and archive tables

Start the binary log for incremental backup

For incremental backup, you need to refresh the binary log

Third-party tool backup

Free MySQL hot backup software Percona XtraBackup

Four, MySQL complete backup and recovery

mysql -u root -p
create database wahaha;
use wahaha;
create table if not exists wa1 (
id int(4) not null auto_increment,
name varchar(10) not null,
sex char(10) not null,
hobby varchar(20),
primary key (id));

insert into wa1 values(1,'u1','male','run');
insert into wa1 values(2,'u2','female','sing');

Insert picture description here

(1) MySQL full backup

The database of the InnoDB storage engine is stored on the disk as three files: db.opt (table attribute file), table name .frm (table structure file), table name .ibd (table data file).

Physical cold backup and recovery

systemctl stop mysqld
yum -y install xz

Compressed backup

tar Jcvf /opt/mysql_all_$(date +%F).tar.xz /usr/local/mysql/data/

Unzip and restore

tar Jxvf /opt/mysql_all_2021-1-30.tar.xz -C /usr/local/mysql/data

systemctl restart mysql
(2) mysqldump backup and recovery

Full backup of one or more complete libraries (including all tables)

mysqldump -u root -p[password] --databases library name 1 [library name 2]…> /backup path/backup file name.sql #exported
is the database script file

mysqldump -uroot -p123123 --databases wahaha > /opt/wahaha.sql
mysqldump -uroot -p123123 --databases mysql wahaha > /opt/mysql-wahaha.sql

Q==,size_16,color_FFFFFF,t_70)

Fully backup all the libraries in the MySQL server

mysqldump -u root -p[password] --all-databases> /backup path/backup file name.sql

mysqldump -u root -p123123 --all-databases > /opt/all.sql

Insert picture description here
Full backup of some tables in the specified library

mysqldump -u root -p[password] library name [table name 1] [table name 2]…> /backup path/backup file name.sql

mysqldump -uroot -p123123 wahaha wa1 > 	/opt/wahaha_wa1.sql

#使用“-d”选项,说明只保存数据库的表结构
#不使用“-d”选项,说明表数据也进行备份

Insert picture description here
View backup files

cat wahaha_wa1.sql | grep -v "^--" /opt/wahaha_wa1.sql | grep -v "^/" | grep -v "^$"

Insert picture description here

(3) Complete backup and recovery

Restore database

mysql -uroot -p123123 -e 'drop database wahaha;'

#“-e”选项,用于指定连接 MySQL 后执行的命令,命令执行完后自动退出

mysql -uroot -p123123 -e 'SHOW DATABASES;'

mysql -uroot -p123123 < /opt/wahaha.sql

mysql -uroot -p123123 -e 'SHOW DATABASES;'

Insert picture description here
Restore data table

mysql -uroot -p123123 -e 'drop table wahaha.wa1;'

mysql -uroot -p123123 -e 'show tables from wahaha;'

mysql -uroot -p123123 wahaha < /opt/wahaha_wa1.sql

mysql -uroot -p123123 -e 'show tables from wahaha;'

Insert picture description here

Five, MySQL incremental backup and recovery

(1) MySQL incremental backup

Turn on the binary log function

vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
binlog_format = MIXED				
server-id = 1

Insert picture description here
Full backup of database or table can be performed weekly

In the case of weekly backups, a scheduled task crontab -e is required to create a plan to complete

mysqldump -uroot -p123123 wahaha wa1 > /opt/wahaha_wa1_$(date +%F).sql

mysqldump -uroot -p123123 --all-databases wahaha > /opt/wahaha_$(date +%F).sql

Incremental backup operations can be performed every day to generate new binary log files

mysqladmin -uroot -p123123 flush-logs

Insert picture description here
Insert new data to simulate the increase or change of data

mysql -uroot -p123123
use wahaha;
insert into wa1 values(3,'u3','male','game');
insert into wa1 values(4,'u4','female','reading');

Insert picture description here
Generate a new binary log file again

The newly generated file after refresh is used now, and the previous record is saved in the previous file

mysqladmin -uroot -p123123 flush-logs

Insert picture description here
View the contents of the binary log file

cp /usr/local/mysql/data/mysql-bin.000002 /opt/
mysqlbinlog --no-defaults --base64-output=decode-rows -v /opt/mysql-bin.000002

#- -base64-output=decode-rows:使用64位编码机制去解码并按行读取
#-v:显示详细内容

Insert picture description here

(2) MySQL incremental backup and recovery

General recovery

#模拟丢失更改的数据的恢复步骤
mysql -uroot -p123123
use wahaha;
delete from wa1 where id=3;
delete from wa1 where id=4;
select * from wa1;
quit

mysqlbinlog --no-defaults /opt/mysql-bin.000002 | mysql -uroot -p123123
mysql -uroot -p123123 -e "select * from wahaha.wa1;"

Insert picture description here
Location based recovery

mysqlbinlog --no-defaults --stop-position='position number' /opt/mysql-bin.000002 | mysql -uroot -p

mysqlbinlog --no-defaults --start-position='position number' /opt/mysql-bin.000002 | mysql -uroot -p

mysqlbinlog --no-defaults --start-position='position number' --stop-position='position number' /opt/mysql-bin.000002 | mysql -uroot -p

mysql -uroot -p123123 -e "truncate table wahaha.wa1;"

mysql -uroot -p123123 -e "select * from wahaha.wa1;"

mysqlbinlog --no-defaults --stop-position='609' /opt/mysql-bin.000002 | mysql -uroot -p

mysql -uroot -p123123 -e "select * from SCHOOL.CLASS1;"

Insert picture description here
Point-in-time recovery

Only restore the data before 22:03:09, that is, do not restore the data of "user4"

Example: First clear the table wa1 to facilitate the experiment

mysql -uroot -p123123 -e "truncate table wahaha.wa1;"
mysql -uroot -p123123 -e "select * from wahaha.wa1;"

mysqlbinlog --no-defaults --stop-datetime='2021-01-30 22:03:09' /opt/mysql-bin.000002 |mysql -uroot -p123123
mysql -uroot -p123123 -e "select * from wahaha.wa1;"

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51468875/article/details/113431603