数据库的备份和恢复

mysql备份和恢复
备份单个数据库
mysqldump是mysql自带的一个很好用的命令,基本语法
mysqldump -uroot -padmin 数据库名>备份的文件名
mysqldump -uroot -p -S /data/3308/mysql.sock tt >test01.sql
[root@localhost  ~]# egrep -v "#|\*|--|^$" ~/test01.sql
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `id` int(4) NOT NULL,
  `name` char(40) NOT NULL,
  `age` tinyint(2) NOT NULL DEFAULT '0',
  `dept` varchar(16) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
LOCK TABLES `student` WRITE;
INSERT INTO `student` VALUES (1,'hello',0,'EC'),(1,'hello',0,'??'),(1,'hello',0,'????¥?'),(1,'hello',0,'????¥?'),(1,'hello',0,'????¥?');
UNLOCK TABLES;

mysql>  show variables like 'character_set%';
+--------------------------+-------------------------------------------+
| Variable_name            | Value                                     |
+--------------------------+-------------------------------------------+
| character_set_client     | utf8                                      |
| character_set_connection | utf8                                      |
| character_set_database   | latin1                                    |
| character_set_filesystem | binary                                    |
| character_set_results    | utf8                                      |
| character_set_server     | latin1                                    |
| character_set_system     | utf8                                      |
| character_sets_dir       | /application/mysql-5.5.32/share/charsets/ |
+--------------------------+-------------------------------------------+
[root@localhost  ~]# mysqldump -uroot -p -S /data/3308/mysql.sock --default-character-set='utf8' tt >test02.sql
Enter password:
[root@localhost  ~]# ls
anaconda-ks.cfg  install.log         test01.sql  公共的  图片  音乐
bak.sql          install.log.syslog  test02.sql  模板    文档  桌面
Desktop          soft                test.sql    视频    下载

mysql -uroot -p -S /data/3308/mysql.sock -e "use tt;drop table student;"
[root@localhost  ~]#  mysql -uroot -p -S /data/3308/mysql.sock tt<~/test01.sql
[root@localhost  ~]#  mysql -uroot -p -S /data/3308/mysql.sock -B tt<~/test01.sql#加B作用是增加创建数据库和连接的命令
Enter password:
mysql> use tt
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> select * from student;
+----+-------+-----+--------------+
| id | name  | age | dept         |
+----+-------+-----+--------------+
|  1 | hello |   0 | EC           |
|  1 | hello |   0 | ??           |
|  1 | hello |   0 | ????¥?       |
|  1 | hello |   0 | ????¥?       |
|  1 | hello |   0 | ????¥?       |
+----+-------+-----+--------------+
5 rows in set (0.00 sec)
mysql -uroot -p -S /data/3308/mysql.sock -compact -B tt>~/test03.sql测试的用
使用压缩命令压缩数据
[root@localhost ~]# mysqldump -uroot -p -S /data/3308/mysql.sock -B tt |gzip >test02.sql.gz
Enter password:
[root@localhost ~]# ls
anaconda-ks.cfg  install.log.syslog  test02.sql.gz  模板  下载
bak.sql          soft                test03.sql     视频  音乐
Desktop          test01.sql          test.sql       图片  桌面
install.log      test02.sql          公共的         文档
利用mysqldump命令备份数据的过程,实际上就是把数据库里的逻辑sql语句直接输出或者生成备份文件的过程
如何做分库备份
分库备份实际上就是执行一个备份语句备份一个库,如果数据库里有多个库,就执行多条相同的备份单个库的
备份语句就可以备份多个库了,注意每个库都可以用对应备份的库作为库名,结尾加sql,备份多个库的命令如下
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| tt                 |
+--------------------+
5 rows in set (0.04 sec)

 mysqldump -uroot -p -S /data/3308/mysql.sock -e "use tt;show databases;"|grep -Evi "database|infor|perfor"去掉这几个数据库
 mysqldump -uroot -p -S /data/3308/mysql.sock -e "use tt;show databases;"|grep -Evi "database|infor|perfor" |sed 's#^#mysqldump -uroot -p'' -B #g'
 法一用脚本
 法二用for循环
 mysqldump -uroot -p -S /data/3308/mysql.sock tt student;
 分表备份的缺点:文件多,碎;
 1,备一个完整全备,再做一个分库分表的备份
 2,脚本批量批量服务多个sql文件
 备份数据库表结构
 利用mysqldump -d 参数只备份表结构 列如:备份oldboy 库的所有表的结构
 [root@localhost ~]# mysqldump -uroot -p -S /data/3308/mysql.sock -B -d tt >~/tett.sql;

如果只想导出数据-t
mysqldump -uroot -padmin -A -B --events|gzip >~/ttt01.sql
mysqldump -uroot -padmin -A -B -F --events|gzip >~/ttt01.sql -F 切割binlog
-A
mysqldump -uroot -padmin -A -B -F --master-data=1 |gzip >~/ttt01.sql自动找到文件的位置
mysqldump -uroot -padmin -A -B -F --master-data=2 |gzip >~/ttt01.sql加注释
mysqldump --help关键参数说明
-B     指定多个库,增加建库语句和use语句
--compact 去掉注释,适合调试输出,生产不用
-A 备份所有库
-F 刷新binlog日志
-x --locak-all-tables
--master-data 增加binlog日志文件名及其对应的位置点
-l只读锁表
-t只备份数据
-d只备份表结构
--single-transaction 适合innodb事务数据库备份:实质是讲隔离级别设置为repeatable read
以确保本次回话dump时,不会看到其他回话已经提交了的数据,保证备份的一致性
myisam或者混合引擎
mysqldump -uroot -p -A -B --master-data=2  -x --events |gzip >生产环境
innodb:推荐使用的
mysqldump -uroot -p -A -B --master-data=2 --events --single-transaction|gzip
利用source恢复(需要登录到里面,或者mysql -e)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| tt                 |
+--------------------+
5 rows in set (0.00 sec)

mysql> drop database tt;
Query OK, 1 row affected (0.18 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
mysql> system ls ~
anaconda-ks.cfg     soft       test.sql  图片
bak.sql            test01.sql       tett.sql  文档
Desktop            test02.sql       公共的    下载
install.log        test02.sql.gz  模板      音乐
install.log.syslog  test03.sql       视频      桌面
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database tt;
Query OK, 1 row affected (0.00 sec)

mysql> use tt;
Database changed
mysql> source /root/test01.sql
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+--------------+
| Tables_in_tt |
+--------------+
| student      |
+--------------+
1 row in set (0.00 sec)

mysql>
恢复方式二
mysql -uroot -padmin tt <~/test01.sql
mysql -uroot -padmin tt <'gzip -d *.gz'
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
| 12 | root | localhost | tt   | Query   |    0 | NULL  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)
mysql> show full processlist;
+----+------+-----------+------+---------+------+-------+-----------------------+
| Id | User | Host      | db   | Command | Time | State | Info                  |
+----+------+-----------+------+---------+------+-------+-----------------------+
| 12 | root | localhost | tt   | Query   |    0 | NULL  | show full processlist |
+----+------+-----------+------+---------+------+-------+-----------------------+
1 row in set (0.00 sec)
mysql> show variables like 'key_buffer%'
    -> ;
+-----------------+---------+
| Variable_name   | Value   |
+-----------------+---------+
| key_buffer_size | 8388608 |
+-----------------+---------+
1 row in set (0.00 sec)

mysql> set global key_buffer_size=1024*1024*32
    -> ;
Query OK, 0 rows affected (0.01 sec)
show status 查看当前的状态
除了logbin等开关参数
mysqlbinlog-->解析binlog日志
mysqlbinlog.index记录了所有的文件
mysql binlog作用是
用来记录mysql内部增删改对mysql数据库有更新内容的操作
[root@localhost data]# mysqlbinlog mysql-bin.000001>all.sql
[root@localhost data]# ls
all.sql      ib_logfile2       mysql-bin.index
ibdata1      mysql             performance_schema
ib_logfile0  mysql-bin.000001  test
ib_logfile1  mysql-bin.000002  tt
[root@localhost data]# mysqlbinlog -d tt mysql-bin.000001>all.sql拆库
   thread_id=4     exec_time=0     error_code=0
SET TIMESTAMP=1451294515/*!*/;
REVOKE INSERT ON test.* FROM 'test02'@'localhost'
/*!*/;
# at 1173
#151228  1:51:31 server id 4  end_log_pos 1368  Query
   thread_id=4     exec_time=1     error_code=0
SET TIMESTAMP=1451296291/*!*/;
create table student(
id int(4) not null,
name char(40) not null,
age tinyint(2) not null default '0',
dept varchar(16) default null
)
/*!*/;
# at 1368
#151228  2:21:11 server id 4  end_log_pos 1475  Query
   thread_id=4     exec_time=0     error_code=0
SET TIMESTAMP=1451298071/*!*/;
DROP TABLE `student` /* generated by server */
/*!*/;
# at 1475
指定位置点恢复
mysqlbinlog mysql-bin.000001 --start-position=1173 --stop-postion=1475 -r all.sql
mysqlbinlog --help
可以指定时间点恢复
mysqlbinlog mysql-bin.000001 --start-datetime='2014-10-10 17:14:18'--stop-datetime='2014-10-11 17:14:18' -r all.sql
-d 截取指定库的binlog
binlog功能的打开
log-bin=
mysqldump --master-data=2备份的点给找到 logfile{0020恢复}master_log_pos=1191恢复1191之前的



你可能感兴趣的:(数据库的备份和恢复)