首页 > 数据库 >MySQL备份与日志

MySQL备份与日志

时间:2022-09-07 21:34:44浏览次数:100  
标签:binlog log hellodb ## 备份 MySQL mysql 日志

MySQL日志管理

事务日志innodb_log

##事务日志执行逻辑:
1.想要修改表,update操作,磁盘内先加载到内存中要执行
2.在内存中执行完update
3.结果先写入到事务日志,这一条操作确认是已经完成的
4.如无网络断电原因,在从内存中写入到磁盘,数据落盘

##事务日志文件是否会写满?
不会,达到一定的大小后会覆盖写,有两个事务日志文件,一个50M,写满一个会继续写另一个
image-20220828152754887

事务日志分类:

redo log:记录某数据块被修改后的值,DML语句,可以用于导入会系统,恢复未写入的数据

undo log:记录某数据块被修改前的值,用于rollback操作

查看事务日志,最大值:50M

[hellodb]>show variables like '%innodb_log%';
+------------------------------------+----------+
| Variable_name                      | Value    |
+------------------------------------+----------+
| innodb_log_buffer_size             | 16777216 |
| innodb_log_checksums               | ON       |
| innodb_log_compressed_pages        | ON       |
| innodb_log_file_size               | 50331648 |
| innodb_log_files_in_group          | 2        |
| innodb_log_group_home_dir          | ./       |
| innodb_log_spin_cpu_abs_lwm        | 80       |
| innodb_log_spin_cpu_pct_hwm        | 50       |
| innodb_log_wait_for_flush_spin_hwm | 400      |
| innodb_log_write_ahead_size        | 8192     |
| innodb_log_writer_threads          | ON       |
+------------------------------------+----------+
11 rows in set (0.00 sec)

ls /data/mysql
[root@rocky mysql]#ll | grep innodb
drwxr-x---. 2 mysql mysql     4096 8月  28 15:13 #innodb_redo
drwxr-x---. 2 mysql mysql      187 8月  28 15:13 #innodb_temp

事务日志性能优化

执行DML语句时写入性能修改

默认的事务日志性能级别1,遵从ACID,但是并发的性能差

1.为默认值,日志缓冲区写入文件,事务提交后落盘
0.性能好
2.性能比0差一点,但是安全性高,建议高并发业务选择2

[hellodb]>select @@innodb_flush_log_at_trx_commit;
+----------------------------------+
| @@innodb_flush_log_at_trx_commit |
+----------------------------------+
|                                1 |
+----------------------------------+
1 row in set (0.00 sec)
image-20220828154502076

MySQL错误日志log_error

查看MySQL中的错误日志,记录比较重大的事件,或者是系统错误日志,日志路径可以自由更改

[hellodb]>show global variables like 'log_error';
+---------------+-----------------------+
| Variable_name | Value                 |
+---------------+-----------------------+
| log_error     | /data/mysql/mysql.log |
+---------------+-----------------------+
1 row in set (0.00 sec)

[hellodb]>select @@log_error;
+-----------------------+
| @@log_error           |
+-----------------------+
| /data/mysql/mysql.log |
+-----------------------+
1 row in set (0.00 sec)


##可以自定义错误日志路径,目录内基本都是mysql.mysql用户,可以使用chown -R mysql.mysql /data/mysql
cat /etc/my.cnf | grep error
log-error=/data/mysql/mysql.log

cat /data/mysql/mysql.log
2022-08-28T13:19:35.607894Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2022-08-28T13:19:35.607925Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2022-08-28T13:19:35.631715Z 0 [System] [MY-010931] [Server] /usr/local/mysql/bin/mysqld: ready for connections. Version: '8.0.30'  socket: '/data/mysql/mysql.sock'  port: 3306  MySQL Community Server - GPL.
2022-08-28T13:19:35.631769Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /tmp/mysqlx.sock

image-20220828212504218

MySQL通用日志general_log

通用日志:用户记录在MySQL库内的一切操作,包括正确操作,错误操作都会记录下来

开启MySQL的通用日志,变量:@@general_log

##默认是不开的,可以开启
[(none)]>select @@general_log;
+---------------+
| @@general_log |
+---------------+
|             0 |
+---------------+

##开启general.log
vim /etc/my.cnf
[mysqld]
general_log
systemctl restart mysqld

[(none)]>set global general_log=1;
Query OK, 0 rows affected (0.02 sec)

[(none)]>select @@general_log;
+---------------+
| @@general_log |
+---------------+
|             1 |
+---------------+
1 row in set (0.00 sec)

##查看general_log的存放目录
[hellodb]>select @@general_log_file;
+-----------------------+
| @@general_log_file    |
+-----------------------+
| /data/mysql/rocky.log |
+-----------------------+
1 row in set (0.00 sec)

##修改general_log的存放目录
[hellodb]>set global general_log_file='/data/mysql/gerneral.log';
Query OK, 0 rows affected (0.00 sec)


##磁盘文件位置,因为在MySQL表里,所以在MySQL的目录内,其实是一个CSV格式的文件
/data/mysql/mysql:general_log.CSV
cat general_log.CSV

##查看表内的信息
[hellodb]>select* from stu where age=22;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|     2 | Shi Potian    |  22 | M      |       1 |         7 |
|    21 | Huang Yueying |  22 | F      |       6 |      NULL |
+-------+---------------+-----+--------+---------+-----------+
2 rows in set (0.01 sec)

##查看日志的内容
cat /data/mysql/gerneral.log
[root@rocky mysql]#cat /data/mysql/gerneral.log
/usr/local/mysql/bin/mysqld, Version: 8.0.30 (MySQL Community Server - GPL). started with:
Tcp port: 3306  Unix socket: /data/mysql/mysql.sock
Time                 Id Command    Argument
2022-09-03T02:21:42.365212Z	    8 Query	select @@general_log_file
2022-09-03T02:24:20.347058Z	    8 Query	call sp_testlog

MySQL慢查询日志slow_query_log

专门记录MySQL库慢查询记录的日志,如果一条SQL执行超过10S,证明他是慢查询语句

##查询MySQL变量有两种方法:
1.show variables like '%slow%' --->模糊查询,较为常用,需要配置变量的值
2.select @@general_log --->精确查询,一般是确认变量的值
3.set global slow_query_log=ON / slow_query_log=1; --->设置变量,一般是全局变量:

##文件名
[hellodb]>show variables like '%slow%';
[hellodb]>show variables like '%slow_query%';
+---------------------+----------------------------+
| Variable_name       | Value                      |
+---------------------+----------------------------+
| slow_query_log      | ON                         |
| slow_query_log_file | /data/mysql/rocky-slow.log |
+---------------------+----------------------------+
2 rows in set (0.00 sec)

[mysqld]
slow_query_log

自定义慢查询SQL语句的标准,刚启用慢查询日志的时候,超过10S即为慢查询,需要优化

[hellodb]>select @@long_query_time;
+-------------------+
| @@long_query_time |
+-------------------+
|         10.000000 |
+-------------------+

##模拟慢查询场景,超过15S的SQL语句
[hellodb]>select sleep(15);
+-----------+
| sleep(15) |
+-----------+
|         0 |
+-----------+
1 row in set (15.00 sec)

##实时输出慢查询日志的内容,超过10S的所有SQL语句,不限制于DQL,DML,DDL等
tail -f /data/mysql/rocky-slow.log

/usr/local/mysql/bin/mysqld, Version: 8.0.30 (MySQL Community Server - GPL). started with:
Tcp port: 3306  Unix socket: /data/mysql/mysql.sock
Time                 Id Command    Argument
# Time: 2022-09-03T02:41:59.051808Z
# User@Host: root[root] @ localhost []  Id:     8
# Query_time: 15.001283  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 1
use hellodb;
SET timestamp=1662172904;
select sleep(15);

记录一下没利用索引的语句,不关超不超过10S的问题:log_queries_not_using_indexes

show variables like '%log_queries%';
[hellodb]>set global log_queries_not_using_indexes=1;
Query OK, 0 rows affected (0.00 sec)

[hellodb]>show variables like '%log_queries%';
+-------------------------------+-------+
| Variable_name                 | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | ON    |
+-------------------------------+-------+

##测试一条简单的select语句,没利用索引
[hellodb]>select * from stu;

image-20220903104902382

MySQL二进制日志log_bin(备份)

binlog:可以记录对DB的任何操作,SQL语句除了写进事务日志外,还需要完全写进binlog中;事务日志写完会覆盖,被覆盖了就丢失了,成为在线日志;

binlog则是属于备份日志,备份所有的操作语句,可以利用开启binlog功能实现备份--->无论在什么数据库中,binlog一定是开启的

.wal:归档日志,存放在etcd库中,备份/元数据等

默认binlog选项不开启,例子:update xxx set age=22--->记录修改了多少行

##二进制日志的三种记录类型
1.基于语句的格式:statement,常用于语句较少的场景;基于语句只会记录这一条update语句,不会记录哪些row被修改了,可能是100行被修改了--->记录语句
2.基于行的记录:row,记录数据的量更大,数据更加安全,mysql8.0默认的日志格式;会记录哪个记录被修改了--->非常重要,比如update xxx set age=22--->记录修改了多少行
3.MIXED:混合记录,由MySQL自身来决定

show variables like '%binlog_format%'; --->直接用row型
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+

mariadb:MIXED型,建议修改成row行型

##开启MySQL_binlog功能,下面两项都要开启功能
[hellodb]>show variables like '%sql_log_bin%'; --->这个要开启
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_log_bin   | ON    |
+---------------+-------+

##变量内有制定的文件存放目录
[hellodb]>show variables like '%log_bin%';
+---------------------------------+-----------------------------+
| Variable_name                   | Value                       |
+---------------------------------+-----------------------------+
| log_bin                         | ON                          |
| log_bin_basename                | /data/mysql/rocky-bin       |
| log_bin_index                   | /data/mysql/rocky-bin.index |
| log_bin_trust_function_creators | OFF                         |
| log_bin_use_v1_row_events       | OFF                         |
| sql_log_bin                     | ON                          |
+---------------------------------+-----------------------------+

根据一些情况:比如批量化的插入新的数据,或者执行存储过程批量化更新数据,可以临时关闭一下二进制日志,避免二进制日志中记录了成千上万行这样的操作记录,占用磁盘太大的空间

配置binlog日志的路径:需要加上指定的路径+binlog日志文件前缀 /etc/my.cnf log-bin=/data/mysql/binlog/rocky-bin

MySQL服务重启后,会重新生成一个新的binlog日志文件,按照原有的序号往后排序

[hellodb]>set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec)

[hellodb]>show variables like '%sql_log_bin%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_log_bin   | OFF   |
+---------------+-------+
1 row in set (0.00 sec)

##配置二进制日志路径,写在[mysqld]内表示开启binlog功能
vim /etc/my.cnf
[mysqld]
server-id=132
log-bin=/data/mysql/binlog/rocky-bin --->binlog日志的前缀,记得加上
systemctl restart mysqld

[root@rocky mysql]#ls binlog
rocky-bin.000001  rocky-bin.index

[hellodb]>select @@log_bin_basename;
+------------------------------+
| @@log_bin_basename           |
+------------------------------+
| /data/mysql/binlog/rocky-bin |
+------------------------------+
1 row in set (0.02 sec)

查看binlog日志中记录的操作(DML语句,增删改):show binlog events

log_name:日志名称

POS:大小,类似position位置

event:表示你这个DML的事务执行过程,比如对哪个表进行操作了,什么时候BEGIN,COMIIT等,从开始大小到结束大小都有记录到位的

show binlog events in 'rocky-bin.000001';

##查看binlog的记录
[hellodb]>show binlog events;
+---------------+-----+----------------+-----------+-------------+-----------------------------------+
| Log_name      | Pos | Event_type     | Server_id | End_log_pos | Info                              |
+---------------+-----+----------------+-----------+-------------+-----------------------------------+
| binlog.000001 |   4 | Format_desc    |       132 |         126 | Server ver: 8.0.30, Binlog ver: 4 |
| binlog.000001 | 126 | Previous_gtids |       132 |         157 |                                   |
+---------------+-----+----------------+-----------+-------------+-----------------------------------+

##执行修改的操作,在binlog中记录这个细节
[hellodb]>update stu set age=100 where stuid=1;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

[hellodb]>show binlog events in 'binlog.000001';
+---------------+-----+----------------+-----------+-------------+--------------------------------------+
| Log_name      | Pos | Event_type     | Server_id | End_log_pos | Info                                 |
+---------------+-----+----------------+-----------+-------------+--------------------------------------+
| binlog.000001 |   4 | Format_desc    |       132 |         126 | Server ver: 8.0.30, Binlog ver: 4    |
| binlog.000001 | 126 | Previous_gtids |       132 |         157 |                                      |
| binlog.000001 | 157 | Anonymous_Gtid |       132 |         236 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000001 | 236 | Query          |       132 |         323 | BEGIN                                |
| binlog.000001 | 323 | Table_map      |       132 |         387 | table_id: 96 (hellodb.stu)           |
| binlog.000001 | 387 | Update_rows    |       132 |         471 | table_id: 96 flags: STMT_END_F       |
| binlog.000001 | 471 | Xid            |       132 |         502 | COMMIT /* xid=68 */                  |
+---------------+-----+----------------+-----------+-------------+--------------------------------------+

查看MySQL当前使用的binlog日志序号:show master status/show master logs

会一直记录增删改查等操作,binlog文件会逐渐变大

##目前只存在一个binlog日志,大小为502B,和上面end_pos的大小一致,其实pos就是position
[hellodb]>show master logs;
show binary logs;
+---------------+-----------+-----------+
| Log_name      | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000001 |       502 | No        |
+---------------+-----------+-----------+
1 row in set (0.00 sec)

##ll 查看
##查看当前使用的Binlog日志文件,position的变化
[hellodb]>show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| rocky-bin.000001 |      157 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

[hellodb]>update stu set age=100 where stuid=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

[hellodb]>show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| rocky-bin.000001 |      500 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

##刷新一版binlog
flush logs;

MySQL客户端专门查看binlog日志的工具:mysqlbinlog

一般如果我们数据库挂掉了,需要查看/使用binlog日志进行恢复,可以使用这个命令进行查看

mysqlbinlog /data/mysql/binlog/rocky-bin.000001
##执行完update后,会很清晰的记录每一条语句的执行效果,目标表等

image-20220904000643213

binlog:被BASE64编码转换过了

image-20220904001000458

利用MySQL binlog恢复数据

1.先使用mysqlbinlog查看binlog文件

2.根据binlog的pos来选择那一段进行导出到本地的.sql文件,一般是通过一个事务的开始BEGIN到COMMIT左右,二进制日志的核心功能:恢复数据,恢复某个时间段执行的事务

##查看binlog日志的内容
mysqlbinlog /data/mysql/binlog/rocky-bin.000001

##可以查看某个位置的内容
mysqlbinlog /data/mysql/binlog/rocky-bin.000001 --start-position=666 --stop-position=841

##目前最大是841,执行了set age=100 where stuid=5
[hellodb]>show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| rocky-bin.000001 |      841 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

[hellodb]>update stu set age=100 where stuid=5;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

##查询一下,age为100,现在修改成50
select * from stu where stuid=5;
[hellodb]>select * from stu where stuid=5;
+-------+-----------+-----+--------+---------+-----------+
| StuID | Name      | Age | Gender | ClassID | TeacherID |
+-------+-----------+-----+--------+---------+-----------+
|     5 | Yu Yutong | 100 | M      |       3 |         1 |
+-------+-----------+-----+--------+---------+-----------+

[hellodb]>select * from stu where stuid=5;
+-------+-----------+-----+--------+---------+-----------+
| StuID | Name      | Age | Gender | ClassID | TeacherID |
+-------+-----------+-----+--------+---------+-----------+
|     5 | Yu Yutong |  50 | M      |       3 |         1 |
+-------+-----------+-----+--------+---------+-----------+

##执行恢复,证明是可以直接通过binlog内记录的SQL语句(比如DML update等),直接恢复数据
[root@rocky binlog]#mysql -uroot -p123 < recover.sql
[hellodb]>select * from stu where stuid=5;
+-------+-----------+-----+--------+---------+-----------+
| StuID | Name      | Age | Gender | ClassID | TeacherID |
+-------+-----------+-----+--------+---------+-----------+
|     5 | Yu Yutong | 100 | M      |       3 |         1 |
+-------+-----------+-----+--------+---------+-----------+

二进制文件日志时间格式

mysqlbinlog /data/mysql/binlog/rocky-bin.000001

at:position
日期date+发生时间
serverid
end_log_pos 结束事务的pos
/*!*/--->
# at 1350
#220904  0:09:26 server id 132  end_log_pos 1414 CRC32 0x14b2967b 	Table_map: `hellodb`.`stu` mapped to number 96
# at 1414
#220904  0:09:26 server id 132  end_log_pos 1494 CRC32 0x3823ee5b 	Update_rows: table id 96 flags: STMT_END_F

BINLOG '
NnwTYxOEAAAAQAAAAIYFAAAAAGAAAAAAAAEAB2hlbGxvZGIAA3N0dQAGAw8B/gEDBJYA9wEwAQHw
AgEhe5ayFA==
NnwTYx+EAAAAUAAAANYFAAAAAGAAAAAAAAEAAgAG//8ABQAAAAlZdSBZdXRvbmcyAgMBAAAAAAUA
AAAJWXUgWXV0b25nZAIDAQAAAFvuIzg=
'/*!*/;

image-20220904084947201

binlog远程实时同步到其他MySQL实例

从master主机库不中断复制到其他的MySQL实例,版本号需要一致,最好都是MySQL一个版本,使用同一个二进制安装脚本

##在master上创建一个同步用户
create user sync@'%' identified by '123';
grant all on *.* to sync@'%';
flush privileges;

##在slave上执行同步,查看当前在用的binlog日志序号
show master status;
show master logs;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| rocky-bin.000002 |      157 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

mysqlbinlog -R --host 10.0.0.128 --user=sync --password=123 --raw --stop-never rocky-bin.000002

##默认放在/root目录下,使用mysqlbinlog查看binlog文件,执行了一个update事务后,会实时更新binlog日志文件大小,一更改就同步
[root@master ~]#ll | grep rocky
-rw-r-----   1 root   root       824 Sep  4 10:15 rocky-bin.000002
[root@master ~]#mysqlbinlog rocky-bin.000002
-rw-r-----   1 root   root      1165 Sep  4 10:21 rocky-bin.000002

image-20220904101938190

image-20220904090609474

MySQL备份和还原

DB的备份机制:库级别,表级别

备份命名:最好是日期+备份方式+库等,基于不同环境,不同重要级别的库做定是备份job,配置备份策略,比如定时导出等

备份类型:

1.全备:完整备份

2.差异备份:基于上一次的全备时间点做备份,适中,累积备份的容量大,但是恢复可以基于全备+差异一次性还原了,可以定向恢复,其实差异是比较好的

3.增量备份:基于上一次的增量备份的时间点做备份,速度快,还原复杂,基于binlog备份,如果binlog没有记录操作DML,则没有数据

冷备:数据库停机,全备

温备:加读锁,不能写,但是可以读,做备份,电商站点--->不能下单购买商品,但是可以看商品,可以select,不能修改

热备:数据库在读写,做备份,基本都是热备,innodb,SQL server等

备份还原原则:如果是数据库宕机了

1.还原全备

2.还原差异/增量,增量的话得一个个还原,因为都是基于上一个增量的

备份的对象以及考虑的要素,注意要做还原测试

1.MySQL数据

2.binlog日志,记录还原到哪个时间点;innodb事务日志,提交事务后先写进事务日志中,在落盘+写进binlog日志,比较大的还是binlog,占用磁盘空间

show variables like '%innodb_log%';
show variables like '%log_bin%';

3.用户账号,权限,程序代码(存储过程--->批量DML、函数、触发器--->如果修改,则ID+1、视图--->保存select结果)

4.服务器的配置文件,例如/etc/my.cnf(Linux机器)

备份产生的性能影响、备份的时长(全备、增备)、备份的恢复时间、备份占用磁盘的大小、备份执行的时间

可以做了备份后,找测试DB进行还原测试,看看数据是否还存在,针对于库/表级别的还原测试,最好是写成还原/备份执行步骤runbook等

常用备份工具:

1.tar、cp等拷贝Linux的MySQL数据目录,一般都是冷备,因为数据库一直在写,数据一直在变化的

2.mysqldump:要求数据库正常运行,导出库的数据,支持热备

3.xtrabackup:支持对innodb做热备

MySQL冷备tar

ubuntu的MySQL服务不是mysqld服务

根本得原则在于:完全复制使用库的信息数据到目的库,很麻烦,还是还原数据舒服,还原.sql文件,还需要复制mysqld.service这个systemd服务来实现

##ubuntu的mysql
systemctl enable --now mysql
systemctl restart mysql

##服务配置文件,备份tar必须进到对应目录下,可以cd $path来实现
/etc/mysql/mysql.cnf

1.先停服务(冷备),备份原来的所有MySQL数据文件,在/data/mysql下面,打包当前目录下的所有文件到/opt
systemctl stop mysqld
cd /data/mysql
tar cf /opt/backup.tar .

备份binlog文件,如果在同一个目录就不用了,不同的话
cd /data/binlog
tar cf /opt/binlog.tar .

##解压到对应的目录,tar -C解压到对应目录
tar xf /opt/backup.tar -C /opt/mysql
ll /opt/mysql

2.拷贝到别的主机,解压,修改权限
scp /opt/backup.tar 10.0.0.128:/opt
mkdir -p /data/mysql
chown -R mysql.mysql /data/mysql

3.如果是MySQL7.0以上,复制mysqld.service到对端机器
scp /usr/lib/systemd/system/mysqld.service 10.0.0.128:/usr/lib/systemd/system/

4.尝试启动机器
service mysqld start
systemctl start mysqld

mysqldump备份工具

mysql最常用的备份工具,直接导出库/表出来作为备份,备份库表

这个只包含了表的备份,并不包括一些数据库属性的备份

表级别的备份

##基本格式,导出,恢复等
mysqldump -uroot -p123 db_name > file
mysqldump -uroot -p123 db_name table_name > file
mysql -uroot -p123 db_name < file

##备份,导出单个库的一个表
mkdir -p /data/mysql
mysqldump -uroot -p123 database_name > /data/mysql/backup.sql
mysqldump -uroot -p123 hellodb stu > /data/backup/stu.sql

##测试还原
[hellodb]>show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| emp               |
| scores            |
| stu               |
| stu_count         |
| student_info      |
| teachers          |
| testlog           |
| toc               |
| v_leader          |
+-------------------+
12 rows in set (0.00 sec)

drop table stu;
mysql -uroot -p123 hellodb < /data/backup/stu.sql
##直接source也可以

Query OK, 0 rows affected (0.00 sec)

##直接还原到库不行,还得手动创建个库
create database hellodb1;
[hellodb]>source /data/backup/hellodb.sql

库级别的备份

mysqldump有专门针对库级别的备份导出,--databases或者是-B,这是带有原来库的属性,包括设置的字符集、排列顺序等

mysqldump -uroot -p123 --databases / -B db_name > /data/backup/hellodb.sql
mysqldump -uroot -p123 --databases hellodb > /data/backup/hellodb.sql
mysqldump -uroot -p123 -B hellodb > /data/backup/hellodb.sql

##查看目录下的文件大小
[root@rocky backup]#ls -lh
总用量 11M
-rw-r--r--. 1 root root  11M 9月   7 21:16 hellodb.sql
-rw-r--r--. 1 root root 2.8K 9月   7 21:01 stu.sql
[root@rocky backup]#du -sh *
11M	hellodb.sql
4.0K	stu.sql

##尝试还原数据库,无法回滚这个事务的,直接source恢复
[hellodb]>drop database hellodb;
Query OK, 12 rows affected (0.11 sec)

[(none)]>source /data/backup/hellodb.sql
[hellodb]>show databases;
+--------------------+
| Database           |
+--------------------+
| catyer             |
| hellodb            |

[hellodb]>show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| emp               |

标签:binlog,log,hellodb,##,备份,MySQL,mysql,日志
From: https://www.cnblogs.com/catyer/p/16667333.html

相关文章