一.日志
1.MySQL 的日志默认保存位置为
/usr/local/mysql/data
2.修改日志配置文件
vim /etc/my.cnf
[mysqld]
##错误日志,用来记录当MySQL启动、停止或运行时发生的错误信息,默认已开启
log-error=/usr/local/mysql/data/mysql_error.log #指定日志的保存位置和文件名
##通用查询日志,用来记录MySQL的所有连接和语句,默认是关闭的
general_log=ON
general_log_file=/usr/local/mysql/data/mysql_general.log
##二进制日志(binlog),用来记录所有更新了数据或者已经潜在更新了数据的语句,记录了数据的更改,可用于数据恢复,默认已开启
log-bin=mysql-bin #也可以 log_bin=mysql-bin
##慢查询日志,用来记录所有执行时间超过long_query_time秒的语句,可以找到哪些查询语句执行时间长,以便于优化,默认是关闭的
slow_query_log=ON
slow_query_log_file=/usr/local/mysql/data/mysql_slow_query.log
long_query_time=5 #设置超过5秒执行的语句被记录,缺省时为10秒
3.查看通用查询日志是否开启
systemctl restart mysqld
mysql -u root -p
show variables like 'general%';
例:[root@www data]# systemctl restart mysqld
[root@www data]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show variables like 'general%';
+------------------+-----------------------------------------+
| Variable_name | Value |
+------------------+-----------------------------------------+
| general_log | ON |
| general_log_file | /usr/local/mysql/data/mysql_general.log |
+------------------+-----------------------------------------+
2 rows in set (0.00 sec)
4.查看二进制日志是否开启
show variables like 'log_bin%';
例:mysql> show variables like 'log_bin%';
+---------------------------------+---------------------------------------+
| Variable_name | Value |
+---------------------------------+---------------------------------------+
| log_bin | ON |
| log_bin_basename | /usr/local/mysql/data/mysql-bin |
| log_bin_index | /usr/local/mysql/data/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
+---------------------------------+---------------------------------------+
5 rows in set (0.01 sec)
5.查看慢查询日功能是否开启
show variables like '%slow%';
例:mysql> show variables like '%slow%';
+---------------------------+--------------------------------------------+
| Variable_name | Value |
+---------------------------+--------------------------------------------+
| log_slow_admin_statements | OFF |
| log_slow_slave_statements | OFF |
| slow_launch_time | 2 |
| slow_query_log | ON |
| slow_query_log_file | /usr/local/mysql/data/mysql_slow_query.log |
+---------------------------+--------------------------------------------+
5 rows in set (0.00 sec)
6.查看慢查询时间设置
show variables like 'long_query_time';
例:mysql> show variables like 'long_query_time';
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| long_query_time | 5.000000 |
+-----------------+----------+
1 row in set (0.00 sec)
7.在数据库中设置开启慢查询的方法
set global slow_query_log=ON;
例:mysql> set global slow_query_log=ON;
Query OK, 0 rows affected (0.00 sec)
二.日志类型与作用
1.redo 重做日志
- 达到事务一致性(每次重启会重做)
- 确保日志的持久性,防止在发生故障,脏页未写入磁盘。重启数据库会进行redo log执行重做,达到事务一致性
2.undo 回滚日志
- 保证数据的原子性,记录事务发生之前的一个版本,用于回滚,innodb事务可重复读和读取已提交 隔离级别就是通过mvcc+undo实现
3.errorlog 错误日志
- Mysql本身启动,停止,运行期间发生的错误信息
4.slow query log 慢查询日志
- 记录执行时间过长的sql,时间阈值(10s)可以配置,只记录执行成功 另一个作用:在于提醒优化
5.bin log 二进制日志
- 用于主从复制,实现主从同步 记录的内容是:数据库中执行的sql语句
6.relay log 中继日志
- 用于数据库主从同步,将主库发来的bin log保存在本地,然后从库进行回放
7.general log 普通日志
记录数据库的操作明细,默认关闭,开启后会降低数据库性能
三.数据备份的重要性
1.备份的主要目的是灾难恢复
2.在生产环境中,数据的安全性至关重要任何数据的丢失都可能产生严重的后果
3.造成数据丢失的原因
- 程序错误
- 人为操作错误
- 运算错误
- 磁盘故障
- 灾难(如火灾、地震)和盗窃
四.Mysql数据库完全备份
1.从物理与逻辑的角度
- 热备份(联机备份)︰数据库处于运行状态,依赖于数据库的日志文件
- 冷备份:数据库锁定表格(不可写入但可读)的状态下进行备份操作
- 逻辑备份:对数据库逻辑组件(如:表等数据库对象)的备份
2.数据库备份的分类
- 完全备份:每次对数据库进行完整的备份
- 差异备份:备份自从上次完全备份之后被修改过的文件
- 增量备份:只有在上次完全备份或者增量备份后被修改的文件才会被备份
3.常见的备份方法
(1)物理冷备
- 在库中新建表并添加数据
use tour;
create table if not exists info1 (
id int(4) not null auto_increment,
name varchar(10) not null,
sex char(10) not null,
hobby varchar(50),
primary key (id));
insert into info1 values(1,'user1','male','running');
insert into info1 values(2,'user2','female','singing');
例:mysql> use tour;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> create table if not exists info1 (
-> id int(4) not null auto_increment,
-> name varchar(10) not null,
-> sex char(10) not null,
-> hobby varchar(50),
-> primary key (id));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into info1 values(1,'user1','male','running');
Query OK, 1 row affected (0.01 sec)
mysql> insert into info1 values(2,'user2','female','singing');
Query OK, 1 row affected (0.00 sec)
- 备份时数据库处于关闭状态,直接打包数据库文件备份速度快,恢复时也是最简单的
systemctl stop mysqld
yum -y install xz
tar Jcvf /opt/mysql_all_$(date +%F).tar.xz /usr/local/mysql/data/
mv /usr/local/mysql/data/ ~
tar Jxvf /opt/mysql_all_2020-11-22.tar.xz
mv /opt/usr/local/mysql/data/ /usr/local/mysql/
例:[root@www data]# systemctl stop mysqld
[root@www data]# yum -y install xz
已加载插件:fastestmirror, langpacks
file:///mnt/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /mnt/repodata/repomd.xml"
正在尝试其它镜像。
Loading mirror speeds from cached hostfile
软件包 xz-5.2.2-1.el7.x86_64 已安装并且是最新版本
无须任何处理
[root@www data]# tar Jcvf /opt/mysql_all_$(date +%F).tar.xz /usr/local/mysql/data/
[root@www data]# mv /usr/local/mysql/data/ ~
[root@www opt]# tar Jxvf /opt/mysql_all_2023-06-19.tar.xz
[root@www opt]# mv /opt/usr/local/mysql/data/ /usr/local/mysql/
(2)mysqldump专用备份工具
- 完全备份一个或多个完整的库(包括其中所有的表)
mysqldump -u root -p[密码] --databases 库名1 [库名2] … > /备份路径/备份文件名.sql #导出的就是数据库脚本文件
例:[root@www opt]# systemctl restart mysqld.service
[root@www opt]# mysqldump -u root -p --databases tour > /opt/tour.sql
Enter password:
[root@www opt]# mysqldump -u root -p --databases mysql tour > /opt/mysql-tour.sql
Enter password:
- 完全备份 MySQL 服务器中所有的库
mysqldump -u root -p[密码] --all-databases > /备份路径/备份文件名.sql
例:[root@www opt]# mysqldump -u root -p --all-databases > /opt/all.sql
Enter password:
- 完全备份指定库中的部分表
mysqldump -u root -p[密码] 库名 [表名1] [表名2] … > /备份路径/备份文件名.sql
例:[root@www opt]# mysqldump -u root -p -d tour info1 sunset > /opt/tour_info1.sql
Enter password:
- 查看备份文件
grep -v "^--" /opt/kgc_info1.sql | grep -v "^/" | grep -v "^$"
例:[root@www opt]# grep -v "^--" /opt/tour_info1.sql | grep -v "^/" | grep -v "^$"
DROP TABLE IF EXISTS `info1`;
CREATE TABLE `info1` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`name` varchar(10) NOT NULL,
`sex` char(10) NOT NULL,
`hobby` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `sunset`;
CREATE TABLE `sunset` (
`id` int(11) DEFAULT NULL,
`name` char(4) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`sex` char(2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
(3)启用二进制日志进行增量备份
- 进行增量备份,需要刷新二进制日志
(4)第三方工具备份
- 免费的MySQL热备份软件Percona XtraBackup
4.MySQL完全备份
- 是对整个数据库、数据库结构和文件结构的备份
- 保存的是备份完成时刻的数据库
- 是差异备份与增量备份的基础
5.MySQL完全备份优点
- 备份与恢复操作简单方便
6.MySQL完全备份缺点
- 数据存在大量的重复
- 占用大量的备份空间
- 备份与恢复时间长
7.MySQL完全恢复
(1)恢复数据库
systemctl start mysqld
mysql -u root -p -e 'drop database tour;'
mysql -u root -p -e 'SHOW DATABASES;'
mysql -u root -p < /opt/tour.sql
mysql -u root -p -e 'SHOW DATABASES;'
例:[root@www opt]# systemctl start mysqld
[root@www opt]# mysql -u root -p -e 'drop database tour;'
Enter password:
[root@www opt]# mysql -u root -p -e 'SHOW DATABASES;'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbs |
| mysql |
| performance_schema |
| sun |
| sys |
+--------------------+
[root@www opt]# mysql -u root -p < /opt/tour.sql
Enter password:
[root@www opt]# mysql -u root -p -e 'SHOW DATABASES;'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbs |
| mysql |
| performance_schema |
| sun |
| sys |
| tour |
+--------------------+
(2)恢复数据表
当备份文件中只包含表的备份,而不包含创建的库的语句时,执行导入操作时必须指定库名,且目标库必须存在。
mysqldump -u root -p tour info1 > /opt/tour_info1.sql
mysql -u root -p -e 'drop table tour.info1;'
mysql -u root -p -e 'show tables from tour;'
mysql -u root -p tour < /opt/tour_info1.sql
mysql -u root -p -e 'show tables from tour;'
例:[root@www opt]# mysqldump -u root -p tour info1 > /opt/tour_info1.sql
Enter password:
[root@www opt]# mysql -u root -pabc123 -e 'drop table tour.info1;'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@www opt]# mysql -u root -pabc123 -e 'show tables from tour;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------------+
| Tables_in_tour |
+----------------+
| account |
| kiki |
| moon |
| stars |
| sunset |
| t1 |
+----------------+
[root@www opt]# mysql -u root -pabc123 tour < /opt/tour_info1.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@www opt]# mysql -u root -pabc123 -e 'show tables from tour;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------------+
| Tables_in_tour |
+----------------+
| account |
| info1 |
| kiki |
| moon |
| stars |
| sunset |
| t1 |
+----------------+
五.Mysql 增量备份
1.开启二进制日志功能
vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
binlog_format = MIXED #默认格式:STATEMENT(基于SQL语句),ROW(基于行)、MIXED(混合模式)
server-id = 1
systemctl start mysqld
ls -l /usr/local/mysql/data/mysql-bin.*
例:[root@www opt]# systemctl start mysqld
[root@www opt]# ls -l /usr/local/mysql/data/mysql-bin.*
-rw-r----- 1 mysql mysql 1042 6月 19 15:11 /usr/local/mysql/data/mysql-bin.000001
-rw-r----- 1 mysql mysql 177 6月 19 15:52 /usr/local/mysql/data/mysql-bin.000002
-rw-r----- 1 mysql mysql 10154 6月 19 16:07 /usr/local/mysql/data/mysql-bin.000003
-rw-r----- 1 mysql mysql 57 6月 19 15:52 /usr/local/mysql/data/mysql-bin.index
2.可每周对数据库或表进行完全备份
mysqldump -u root -p kgc info1 > /opt/kgc_info1_$(date +%F).sql
mysqldump -u root -p --databases kgc > /opt/kgc_$(date +%F).sql
例:[root@www opt]# mysqldump -u root -p tour info1 > /opt/tour_info1_$(date +%F).sql
Enter password:
[root@www opt]# mysqldump -u root -pabc123 --databases tour > /opt/tour_$(date +%F).sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
3.可每天进行增量备份操作,生成新的二进制日志文件(例如 mysql-bin.000002)
mysqladmin -u root -p flush-logs
例:[root@www opt]# mysqladmin -u root -pabc123 flush-logs
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
4.插入新数据,以模拟数据的增加或变更
use kgc;
insert into info1 values(3,'user3','male','game');
insert into info1 values(4,'user4','female','reading');
例:[root@www opt]# mysql -troot -pabc123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use tour;
Database changed
mysql> insert into info1 values(3,'user3','male','game');
Ignoring query to other database
mysql> insert into info1 values(4,'user4','female','reading');
Ignoring query to other database
5.再次生成新的二进制日志文件(例如 mysql-bin.000003)
mysqladmin -u root -p flush-logs
例:[root@www opt]# mysqladmin -u root -pabc123 flush-logs
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
6.查看二进制日志文件的内容
cp /usr/local/mysql/data/mysql-bin.000002 /opt/
mysqlbinlog --no-defaults --base64-output=decode-rows -v /opt/mysql-bin.000002
例:[root@www opt]# cp /usr/local/mysql/data/mysql-bin.000002 /opt/
[root@www opt]# mysqlbinlog --no-defaults --base64-output=decode-rows -v /opt/mysql-bin.000002
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#230619 15:31:31 server id 1 end_log_pos 123 CRC32 0xcf970784 Start: binlog v 4, server v 5.7.41-log created 230619 15:31:31 at startup
ROLLBACK/*!*/;
# at 123
#230619 15:31:31 server id 1 end_log_pos 154 CRC32 0x519bcca2 Previous-GTIDs
# [empty]
# at 154
#230619 15:52:29 server id 1 end_log_pos 177 CRC32 0xf8dd4e78 Stop
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
六.Mysql 增量恢复
1.一般恢复文章来源:https://www.toymoban.com/news/detail-491945.html
- 模拟丢失更改的数据的恢复步骤
use kgc;
delete from info1 where id=3;
delete from info1 where id=4;
mysqlbinlog --no-defaults /opt/mysql-bin.000002 | mysql -u root -p
例:mysql> use tour;
Database changed
mysql> delete from info1 where id=3;
Ignoring query to other database
mysql> delete from info1 where id=4;
Ignoring query to other database
mysql> quit
Bye
[root@www opt]# mysqlbinlog --no-defaults /opt/mysql-bin.000002 | mysql -u root -p
Enter password:
- 模拟丢失所有数据的恢复步骤
use kgc;
drop table info1;
mysql -u root -p kgc < /opt/kgc_info1_2020-11-22.sql
mysqlbinlog --no-defaults /opt/mysql-bin.000002 | mysql -u root -p
例:mysql> use tour;
Database changed
mysql> drop table info1;
Ignoring query to other database
mysql> quit
Bye
[root@www opt]# mysql -u root -p tour < /opt/tour_info1_2023-06-19.sql
Enter password:
[root@www opt]# mysqlbinlog --no-defaults /opt/mysql-bin.000002 | mysql -u root -p
Enter password:
2.断点恢复文章来源地址https://www.toymoban.com/news/detail-491945.html
mysqlbinlog --no-defaults --base64-output=decode-rows -v /opt/mysql-bin.000002
例:[root@www opt]# mysqlbinlog --no-defaults --base64-output=decode-rows -v /opt/mysql-bin.000002
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#230619 15:31:31 server id 1 end_log_pos 123 CRC32 0xcf970784 Start: binlog v 4, server v 5.7.41-log created 230619 15:31:31 at startup
ROLLBACK/*!*/;
# at 123
#230619 15:31:31 server id 1 end_log_pos 154 CRC32 0x519bcca2 Previous-GTIDs
# [empty]
# at 154
#230619 15:52:29 server id 1 end_log_pos 177 CRC32 0xf8dd4e78 Stop
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
- 基于位置恢复
mysqlbinlog --no-defaults --stop-position='623' /opt/mysql-bin.000002 | mysql -uroot -p
mysqlbinlog --no-defaults --start-position='623' /opt/mysql-bin.000002 | mysql -uroot -p
例:[root@www opt]# mysqlbinlog --no-defaults --stop-position='623' /opt/mysql-bin.000002 | mysql -uroot -p
Enter password:
[root@www opt]# mysqlbinlog --no-defaults --start-position='623' /opt/mysql-bin.000002 | mysql -uroot -p
Enter password:
- 基于时间点恢复
mysqlbinlog --no-defaults --stop-datetime='2020-11-22 16:41:24' /opt/mysql-bin.000002 |mysql -uroot -p
mysqlbinlog --no-defaults --start-datetime='2020-11-22 16:41:24' /opt/mysql-bin.000002 |mysql -uroot -p
例:[root@www opt]# mysqlbinlog --no-defaults --stop-datetime='2023-06-19 16:41:24' /opt/mysql-bin.000002 |mysql -uroot -p
Enter password:
[root@www opt]# mysqlbinlog --no-defaults --start-datetime='2023-06-19 16:41:24' /opt/mysql-bin.000002 |mysql -uroot -p
Enter password:
- 如果恢复某条SQL语句之前的所有数据,就stop在这个语句的位置节点或者时间点
- 如果恢复某条SQL语句以及之后的所有数据,就从这个语句的位置节点或者时间点start
到了这里,关于Mysql之数据备份的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!