Mysql的备份(上)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: Mysql的备份(上)

一、Mysql备份


(1)binlog二进制日志概述


  • binlog二进制日志是mysql常用的备份数据库的方式
  • mysql数据库中的增、删、改的操作都会记录到binlog日志中,相当于二进制日志记录着一条条SQL语句,开启了binlog二进制日志后,备份数据库其实就是备份二进制日志,
  • 通过二进制日志去恢复数据,日志中的每条操作都有相应的时间和位置号,可以指定事件或者位置号去恢复数据库数据


(2)开启binlog二进制日志


[root@rzy ~]# vim /etc/my.cnf #修改mysql主配置文件  
  1 [mysqld]
  2 basedir = /usr/local/mysql
  3 datadir = /usr/local/mysql/data  #这个路径就是二进制日志存放的路径
  4 port = 3306
  5 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  6 character_set_server=utf8
  7 init_connect='SET NAMES utf8'
  8 log-error=/usr/local/mysql/logs/mysqld.log
  9 pid-file=/usr/local/mysql/logs/mysqld.pid
 10 skip-name-resolve
 11 server-id = 1  #添加,开启二进制时,指定的id为1
 12 log-bin=mysql-bin #添加,开启二进制日志
#保存退出
[root@rzy ~]# systemctl restart mysqld #重启mysql服务
[root@rzy ~]# cd /usr/local/mysql/data/ #进入存放二进制日志的目录
[root@rzy data]# ls 
aaa       ib_buffer_pool  ib_logfile0  ibtmp1  mysql-bin.000001  mysqld_safe.pid     sys
auto.cnf  ibdata1         ib_logfile1  mysql   mysql-bin.index   performance_schema
————————————————————————————————————————————————————————————
#mysql-bin.000001这个就是二进制日志
#mysql-bin.index  这个可以查看mysql所有记录的二进制文件
————————————————————————————————————————————————————————————
[root@rzy data]# cat mysql-bin.index  #查看当前mysql操作记录的二进制文件是那个
./mysql-bin.000001   #可以看到当前mysql记录的二进制日志有000001
[root@rzy data]# mysqlbinlog mysql-bin.000001  #查看二进制日志需要使用mysqlbinlog命令查看,不然是乱码
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#210519 23:13:55 server id 1  end_log_pos 123 CRC32 0x203bbf20  Start: binlog v 4, server v 5.7.12-log created 210519 23:13:55 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
MyulYA8BAAAAdwAAAHsAAAABAAQANS43LjEyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAzK6VgEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
ASC/OyA=
'/*!*/;
# at 123
#210519 23:13:55 server id 1  end_log_pos 154 CRC32 0x92e7fa2e  Previous-GTIDs
# [empty]
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*/;

binlog日志内容作用:


  • 记录了mysql数据的增、删、改的操作
  • MySQL的主从复制就是基于二进制日志来进行同步的,slave服务器通过复制master服务器的二进制日志完成主从复制,在执行之前保存于中继日志(relay log)中
  • slave从服务器通常可以关闭二进制日志从而提高服务器性能,因为从服务器主要是读主服务器发过来的二进制日志,它本身是不需要记录二进制日志的,所有可以关闭二进制日志功能从而提高性能



有两种情况,二进制日志会重新生成:


  1. 重启mysql服务
  2. 在MySQL服务器中使用flush logs;重置日志


注意!!!!:


在删除二进制日志时,只删除二进制日志是不行的,而且会导致mysql管理混乱,还会使mysql无法重新启动,故而在删除二进制日志时,要记得把mysql-bin.index中的内容也删除


(3)在mysql中查看二进制日志

mysql> show master status;  #查看当前的记录的二进制日志
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> flush logs;  #重置日志
Query OK, 0 rows affected (0.01 sec)
mysql> show master status; #再次查看发现变成了02号日志
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> exit
Bye
[root@rzy ~]# cat /usr/local/mysql/data/mysql-bin.index  #查看记录的日志,发现多了一个
./mysql-bin.000001
./mysql-bin.000002


(4)mysql二进制文件读取工具mysqlbinlog


命令格式:mysqlbinlog 选项 二进制日志路径


选项:


选项 作用
–start-datetime 用来指定二进制日志的起始日期 日期需要写成yyyy-mm-dd 00:00:00格式的
–stop-datetime 用来指定二进制日志的结束日期
–start-position 用来指定二进制日志的起始位置
–stop-position 用来指定二进制日志的结束位置


利用这些选项可以完成mysql的增量备份,一般都是配合and加上起始和结束日期来备份,以天来备份的

[root@rzy ~]# mysql -u root -p123123 #进入mysql
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.12-log Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use aaa;
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> show tables;  #查看所有表
+---------------+
| Tables_in_aaa |
+---------------+
| aaa           |
| bbb           |
+---------------+
2 rows in set (0.01 sec)
mysql> drop table aaa; #删除两个表
Query OK, 0 rows affected (0.00 sec)
mysql> drop table bbb;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;  #再次查看确认删除
Empty set (0.00 sec)
mysql> exit
Bye
[root@rzy ~]# mysqlbinlog /usr/local/mysql/data/mysql-bin.000002  #使用mysqlbinlog命令查看
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#210519 23:48:49 server id 1  end_log_pos 123 CRC32 0x8d27bd79  Start: binlog v 4, server v 5.7.12-log created 210519 23:48:49
# Warning: this binlog is either in use or was not closed properly.
BINLOG '
YTOlYA8BAAAAdwAAAHsAAAABAAQANS43LjEyLWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA
AXm9J40=
'/*!*/;
# at 123
#210519 23:48:49 server id 1  end_log_pos 154 CRC32 0x4c53ba28  Previous-GTIDs
# [empty]
# at 154
#210520  0:19:23 server id 1  end_log_pos 219 CRC32 0x24558e28  Anonymous_GTID  last_committed=0  sequence_number=1
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 219
#210520  0:19:23 server id 1  end_log_pos 333 CRC32 0x3ce0596c  Query thread_id=3 exec_time=0 error_code=0
use `aaa`/*!*/;          
SET TIMESTAMP=1621441163/*!*/;
SET @@session.pseudo_thread_id=3/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1075838976/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
DROP TABLE `aaa` /* generated by server */  #这个就是删除aaa表的命令
/*!*/;
# at 333
#210520  0:19:25 server id 1  end_log_pos 398 CRC32 0x701d265b  Anonymous_GTID  last_committed=1  sequence_number=2
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 398
#210520  0:19:25 server id 1  end_log_pos 512 CRC32 0xd93b7e48  Query thread_id=3 exec_time=0 error_code=0
SET TIMESTAMP=1621441165/*!*/;
DROP TABLE `bbb` /* generated by server */  #这个就是刚才删除bbb表的命令
/*!*/;
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*/;
[root@rzy ~]# mysqlbinlog --start-position 333 --stop-position 398 /usr/local/mysql/data/mysql-bin.000002 > /root/mysql-bin.sql  #从位置333到398的内容输出到新的二进制日志中
[root@rzy ~]# mysqlbinlog --start-datetime 2021-05-20 00:19:23 --stop-datetime 2021-05-20 00:19:25 /usr/local/mysql/data/mysql-bin.000002 > /root/mysql-date-bin.sql #利用时间来输出二进制日志内容
[root@rzy ~]# mysqlbinlog mysql-bin.sql  #查看新建的二进制日志,确认输出内容
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
ERROR: File is not a binary log file.
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*/;
[root@rzy ~]# mysql -u root -p123123 < mysql-bin.sql  #恢复数据
mysql: [Warning] Using a password on the command line interface can be insecure. #这个是提示密码不安全


(5)二进制日志内容解释


[root@rzy ~]# mysqlbinlog /usr/local/mysql/data/mysql-bin.000002
# at 577
#210520  0:47:31 server id 1  end_log_pos 687 CRC32 0x1b302d07  QueryQuery:记录类型
#thread_id=6  exec_time=0 error_code=0
SET TIMESTAMP=1621442851/*!*/;
create table aaa(id int,name char(10))
_________________________________________________________
#210520 0:47:31:时间点
#server id 1:服务器id,就是配置文件里的那个id
#Query:记录类型
#thread_id=6:线程号
#exec_time=0:语句的时间戳和写入二进制日志文件的时间差
#at 577:事件位置
#error_code=0:错误代码
#create table aaa(id int,name char(10)):事件内容一般z

(6)二进制日志格式


  • 二进制有三种格式分别是statement、row、mixed,是由bin_log_format定义的,可以在mysql中临时定义,也可以在配置文件中永久定义,
  • mysql5.7版本之前默认格式为statement,mysql5.7版本及之后默认格式为row,


statement: 此格式是基于恢复语句的,使用这个格式二进制日志会记录在mysql中执行过的SQL语句。需要注意的是,如果执行的SQL语句使用了某种即时函数,例如current_date()这种表示当前时间的函数的话,在恢复数据时,会导致恢复数据的不一致


row: 此格式是基于恢复行数据的,顾名思义就是恢复数据库的行数据,但是缺点就是如果数据库数据量大的话,恢复数据的量会很大


mixed: 此格式是混合模式,使用这个模式会由mysql去自行定义恢复SQL语句还是行数据 (使用statement格式还是row格式)

mysql> show variables like "binlog_format";   #查看mysql当前二进制记录的格式
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+
1 row in set (0.04 sec)
mysql> set session binlog_format=statement;   #临时修改二进制记录的格式为statement
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@rzy ~]# vim /etc/my.cnf 
  1 [mysqld]
  2 basedir = /usr/local/mysql
  3 datadir = /usr/local/mysql/data
  4 port = 3306
  5 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  6 character_set_server=utf8
  7 init_connect='SET NAMES utf8'
  8 log-error=/usr/local/mysql/logs/mysqld.log
  9 pid-file=/usr/local/mysql/logs/mysqld.pid
 10 skip-name-resolve
 11 server-id = 1
 12 log-bin=mysql-bin
 13 binlog_format=ROW  #添加binlog_format=ROW永久指定mysql二进制日志格式为row
#保存退出
[root@rzy ~]# systemctl restart mysqld

建议:不要把二进制日志和数据文件放在同一个设备,可以将binlog二进制日志备份到其他设备上


(7)备份类型


在备份mysql数据时,根据多种情况可以分类成多种备份类型

  • 根据备份时,mysql服务是否可以正常进行读写


  • 冷备份(cold backup):备份时需要停止mysql服务,mysql的读写操作都不能进行
  • 温备份:备份时需要对全局(mysql的所有数据)施加共享锁,mysql只能进行读操作不能进行写操作
  • 热备份(hot backup):备份时不需要停止mysql服务,mysql的读写操作可以正常进行


  • 根据备份时备份的数据是整个数据还是变化数据(数据集)


  • 完全备份(full backup):备份整个mysql的数据,恢复时只需一次性恢复,备份和恢复简单,但是恢复时间长,备份时间也长,并且每一次进行完全备份会产生大量的重复数据,占用大量空间
  • 增量备份(partial backup):只备份完全备份后增加的数据,例如:第一天把数据库进行了完全备份,第二天增加了数据A,增量备份只会备份数据A而不会把全部的数据在备份一遍,恢复时需要诶个恢复,如果中间的某一次备份数据损坏,将导致数据丢失
  • 差异备份(differential backup):只备份完全备份之后修改过的所有文件,备份的时间起点是自完全备份开始,备份数据量会越来越大,恢复数据时,只需要恢复上一次的完全备份和最近一次的差异备份即可,例如:第一天把数据库进行了完全备份,第二天没有新数据,第三天增加了数据A,差异备份会只备份数据A,恢复时也是需要恢复完全备份和最后一次差异备份的数据A


  • 根据备份时的备份内容


  • 物理备份(physical backup):物理备份会直接复制数据文件(即mysql的数据存储路径),并且打包归档
  • 特点:物理备份不需要额外工具,直接使用归档命令既可以,但是跨平台能力比较差,如果数据量超过几十个G的话,则适用于物理备份


  • 逻辑备份(logical backup):逻辑备份其实就是备份SQL语句,mysqldump就是逻辑备份
  • 特点:可以使用文档编辑器进行编辑,导入数据库方便,直接读取SQL语句即可。逻辑备份恢复数据时间较慢,占用空间大,并且无法保证浮点数的精度,恢复完数据后还需重新建立索引


(8)备份的策略


在进行mysql的备份时,有以下因素需要考虑


  1. 备份方式
  2. 备份实践
  3. 备份成本
  4. 锁时间
  5. 使用时间
  6. 性能开销
  7. 恢复成本
  8. 恢复时间
  9. 可以容忍数据的最大丢失数量


(9)备份内容


在备份mysql时要备份的内容一般有:


  1. 数据库中的数据
  2. mysql的配置文件
  3. mysql中的代码:存储过程、存储函数、触发器
  4. OS相关的配置文件,crontab计划任务中的备份策略脚本
  5. 如果在主从复制的场景中。一般是备份跟复制相关的信息
  6. 二进制日志需要定期备份,一旦发现二进制日志出现问题,需要马上对数据进行完全备份


(10)Mysql最常用的三种备份工具


  1. mysqldump


通常为小数据情况下的备份,mysqldump是单线程备份,所以在恢复和备份时较慢,故通常只备份小数据

innoDB引擎支持:热备、温备

MyISAM、Aria引擎支持:温备


  1. Xtrabackup


Xtrabackup通常使用innobackupex工具,一般备份mysql大数据时使用,xtrabackup属于物理备份,备份和恢复速度快


innoDB引擎支持:热备、增量备份、完全备份

MyISAM引擎支持:温备、完全备份


  1. LVM-snapshot


这个备份工具在实际工作中几乎不使用,这个一个接近于热备的工具,利用LVM可以创建快照的特点备份数据,备份之前要先请求全局锁并且在创建完快照后释放全局锁,这个快照只能恢复一次,快照恢复后需要再次创建快照,使用cp、tar等工具进行物理备份,备份和恢复的速度较快


使用这个工具是很难实现增量备份的,并且请求全局锁需要等待一段时间,在实际工作的服务器上尤为如此,所以几乎不使用这个工具


因为LVM-snapshot几乎不使用,所以这里只做mysqldump和Xtrabackup的比较:


image.png


推荐使用Xtrabackup进行完全备份,使用Mysqldump进行增量备份


二、Mysqldump工具


  • mysqldump是基于mysql的二进制日志备份的,是单线程备份,所以在备份、恢复大数据时,比较耗费时间,一般都使用mysqldump备份小数据
  • 使用mysqldump工具之前要记得开启
  • 使用Mysqldump工具:


-备份所有数据库

mysql> show databases;  #先查看有几个数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aaa                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.07 sec)
mysql> use aaa;
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> show tables; #查看aaa库中的表
+---------------+
| Tables_in_aaa |
+---------------+
| aaa           |
| bbb           |
+---------------+
2 rows in set (0.00 sec)
mysql> exit
bye
[root@rzy ~]# mkdir /beifen #创建备份目录
[root@rzy ~]# mysqldump -u root -p123123 --opt --all-databases > /beifen/all.sql #备份所有数据库
mysqldump: [Warning] Using a password on the command line interface can be insecure.


-恢复数据

[root@rzy ~]# mysql -u root -p123123; #进入数据库
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.12-log Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases; #先查看所有数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aaa                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
mysql> drop database aaa; #删除aaa库
Query OK, 2 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit
Bye
[root@rzy ~]# mysql -u root -p123123 < /beifen/all.sql  #使用刚才的完全备份恢复mysql数据
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@rzy ~]# mysql -u root -p123123 -e "show databases;" #-e命令可以直接调用SQl语句,查看所以数据库发现aaa库又回来了
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aaa                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+


-备份单个库

[root@rzy ~]# mysqldump -u root -p123123 --databases aaa > /beifen/aaa-data.sql #备份aaa库
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@rzy ~]# mysql -u root -p123123 -e "drop database aaa;" #删除aaa库
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@rzy ~]# mysql -u root -p123123 -e "show databases;;" #查看是否删除
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
[root@rzy ~]# mysql -u root -p123123 < /beifen/aaa-data.sql  #恢复aaa库
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@rzy ~]# mysql -u root -p123123 -e "show databases;;" #再次查看所有库,发现aaa库回来了
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aaa                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

-备份单个、多个表

[root@rzy ~]# mysqldump -u root -p123123 aaa bbb > /beifen/aaa-bbb.sql #备份aaa库的bbb表
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@rzy ~]# mysql -u root -p123123 -e "use aaa; drop table bbb;" #删除aaa库的bbb表
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@rzy ~]# mysql -u root -p123123 -e "use aaa; show tables;" #查看是否删除
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+
| Tables_in_aaa |
+---------------+
| aaa           |
+---------------+
[root@rzy ~]# mysql -u root -p123123 aaa < /beifen/aaa-bbb.sql  #恢复bbb表,要注意指定库
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@rzy ~]# mysql -u root -p123123 -e "use aaa; show tables;" #查看是否恢复成功
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+
| Tables_in_aaa |
+---------------+
| aaa           |
| bbb           |
+---------------+
[root@rzy ~]# mysqldump -u root -p123123 aaa aaa bbb > /beifen/aaa-table.sql   #备份aaa库的aaa和bbb表
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@rzy ~]# mysql -u root -p123123 -e "use aaa; drop table aaa ;"         #删除aaa表
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@rzy ~]# mysql -u root -p123123 -e "use aaa; drop table bbb ;"         #删除bbb表
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@rzy ~]# mysql -u root -p123123 -e "use aaa; show tables;"       #查看aaa库的所有表确认删除
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@rzy ~]# mysql -u root -p123123 aaa < /beifen/aaa-table.sql      #恢复两个表
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@rzy ~]# mysql -u root -p123123 -e "use aaa; show tables;"       #查看aaa库的所有表,发现成功恢复
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+
| Tables_in_aaa |
+---------------+
| aaa           |
| bbb           |
+---------------+


-导出表的结构

[root@rzy ~]# mysqldump  -u root -p123123 -d aaa > /beifen/aaa-jiegou.sql #导出aaa库下所有表的结构
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@rzy ~]# mysqldump  -u root -p123123 -d aaa aaa > /beifen/aaa-jiegou.sql #只导出aaa库下aaa表的结构
mysqldump: [Warning] Using a password on the command line interface can be insecure.

-导出表的数据

[root@rzy ~]# mysqldump -u root -p123123 -t aaa > /beifen/aaa-shuju.sql #导出aaa库下所有表的数据
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@rzy ~]# mysqldump -u root -p123123 -t aaa aaa > /beifen/aaa-shuju.sql #只导出aaa库下aaa表的数据
mysqldump: [Warning] Using a password on the command line interface can be insecure.


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
3月前
|
关系型数据库 MySQL 数据库
rds备份与恢复
rds备份与恢复
57 3
|
3月前
|
关系型数据库 MySQL 数据库
Python tk dos命令备份mysql数据库
Python tk dos命令备份mysql数据库
25 0
|
3月前
|
存储 关系型数据库 MySQL
mysql数据库如何做到定期备份
mysql数据库如何做到定期备份
293 2
|
4月前
|
存储 关系型数据库 MySQL
MySQL库的操作『增删改查 ‖ 编码问题 ‖ 备份与恢复』
MySQL库的操作『增删改查 ‖ 编码问题 ‖ 备份与恢复』
51 0
|
2月前
|
SQL 关系型数据库 MySQL
mysql怎么备份
mysql怎么备份
197 7
|
3月前
|
存储 关系型数据库 MySQL
利用Xtrabackup进行mysql增量备份和全量备份
利用Xtrabackup进行mysql增量备份和全量备份
199 0
|
18天前
|
SQL 存储 关系型数据库
mysql数据库备份与恢复
mysql数据库备份与恢复
|
2月前
|
关系型数据库 MySQL Linux
Linux环境下定时备份mysql数据库
Linux环境下定时备份mysql数据库
|
2月前
|
存储 关系型数据库 MySQL
mysql怎么备份
mysql怎么备份
22 7
|
2月前
|
监控 容灾 安全
规划阿里云RDS跨区迁移并构建容灾与备份策略
规划阿里云RDS(Relational Database Service)跨区迁移并构建容灾与备份策略
114 2