数据库MySQL(五)数据库备份mysqldump+binlog方法

目录

一、mysqldump+binlog

1、优势

2、语法

二、备份与恢复

1、备份流程

2、恢复流程

三、记录的导入和导出

1、记录导出

2、记录导入


一、mysqldump+binlog

1、优势

自动记录日志position位置

可用性,一致性

2、语法

mysqldump -h 服务器ip  -u用户名 -p'密码'  数据库名  >  备份文件.sql

mysqldump  -u用户名   -p'密码'  数据库名  >  备份文件.sql

二、备份与恢复

1、备份流程

  • 准备库
//登录数据库,在数据库里面添加点数据
[root@localhost ~]# mysql -uroot -p'Qian@123'
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hhh                |
| mysql              |
| performance_schema |
| sys                |
| test1              |
| testdb             |
+--------------------+
7 rows in set (0.00 sec)

mysql> select * from testdb.test2;
+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
+------+
3 rows in set (0.07 sec)
  • 执行备份
[root@localhost ~]# mkdir /backup
[root@localhost ~]# date +%F-%H    //一种时间输出格式
2022-10-13-04

//这里有个错误提醒,二进制日志没开启
[root@localhost ~]# mysqldump -uroot -p'Qian@123' --all-databases --single-transaction --master-data=2 --flush-logs >  /backup/`date +%F-%H`mysql-all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump: Error: Binlogging on server not active

//开启二进制日志
[root@localhost ~]# vim /etc/my.cnf    //在里面添加下面的内容
[mysqld]
log_bin=mysql_bin
server_id=1

[root@localhost ~]# systemctl restart mysqld    //重启数据库
[root@localhost ~]# mysql -uroot -p'Qian@123'    //登录数据库查看一下二进制日志状态,ON就是开启成功
mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | ON    |
+---------------+-------+
1 row in set (0.00 sec)

//把二进制日志没开启之前的备份删除掉,再重新执行一下备份操作
//master-data=2注释掉日志记录,flush-logs刷新日志
[root@localhost ~]# mysqldump -uroot -p'Qian@123' --all-databases --single-transaction --master-data=2 --flush-logs >  /backup/`date +%F-%H`mysql-all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# ls /backup/
2022-10-13-04mysql-all.sql
  • 观察备份细节
//在前面备份时如果写的是master-data=1,那么在CHANGE MASTER TO MASTER_LOG_FILE='mysql_bin.000002', MASTER_LOG_POS=154的前面就不会有注释了
[root@localhost ~]# vim /backup/2022-10-13-04mysql-all.sql 
//第22行可以看到mysql_bin.0000002,在/etc/lib/mysql里面有对应的文件
22 -- CHANGE MASTER TO MASTER_LOG_FILE='mysql_bin.000002', MASTER_LOG_POS=154;

LOCK TABLES `columns_priv` WRITE;    //观察各种锁机制,用来保证数据的一致性
/*!40000 ALTER TABLE `columns_priv` DISABLE KEYS */;
/*!40000 ALTER TABLE `columns_priv` ENABLE KEYS */;
UNLOCK TABLES;

2、恢复流程

  • 备份二进制日志文件
[root@localhost ~]# cp /var/lib/mysql/*bin* .
[root@localhost ~]# ls
anaconda-ks.cfg    mysql_bin.000001  mysql_bin.index  mysql_bin.000002
  • 停止数据库
[root@localhost ~]# systemctl stop mysqld
  • 清理环境
//模拟,模拟,模拟
[root@localhost ~]# rm -rf /var/lib/mysql/*
  • 启动数据库
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# grep password /var/log/mysqld.log 
2022-10-13T09:19:23.742142Z 1 [Note] A temporary password is generated for root@localhost: re%f<7H1bi7s
//这里更改一下数据库的密码可以更直观的感受到数据库恢复数据的过程
[root@localhost ~]# mysqladmin -uroot -p're%f<7H1bi7s' password 'Qian@1234'
[root@localhost ~]# mysql -uroot -p'Qian@1234'
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)
  • MySQL恢复数据
//恢复数据
[root@localhost ~]# mysql -uroot -p'Qian@1234' < /backup/2022-10-13-04mysql-all.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.
//刷新一下
[root@localhost ~]# mysql -uroot -p'Qian@1234' -e 'flush privileges'
mysql: [Warning] Using a password on the command line interface can be insecure.

//用新密码登录数据库发现报错,登不进去了,这时候要用旧密码登录了,千万要注意这里,
如果没改密码就无所谓了,一旦改了密码刷新之后新密码是登不进去的
[root@localhost ~]# mysql -uroot -p'Qian@1234'
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@localhost ~]# mysql -uroot -p'Qian@123'
mysql> show databases;    //可以看到数据库已经恢复了
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hhh                |
| mysql              |
| performance_schema |
| sys                |
| test1              |
| testdb             |
+--------------------+
7 rows in set (0.00 sec)
  • 二进制日志恢复

        如果在备份之后又操作了数据库,那么这些数据是没有备份的,想要恢复可以通过恢复二进制日志来恢复备份之后的一些操作

[root@localhost ~]# vim /backup/2022-10-13-04mysql-all.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql_bin.000002', MASTER_LOG_POS=154;

//mysql_bin.000001就不用写了,这里的数据要看2022-10-13-04mysql-all.sql
//在/etc/lib/mysql,里面有多少个二进制日志文件就要在mysql_bin.000002后面跟着增加多少个二进制文件
[root@localhost ~]# mysqlbinlog mysql_bin.000002 --start-position154 | mysql -uroot -p'Qian@123'
//假如说在/etc/lib/mysql里面有mysql_bin.000001  mysql_bin.000002  mysql_bin.000003那么就要这样写
[root@localhost ~]# mysqlbinlog mysql_bin.000002  mysql_bin.000003 --start-position154 | mysql -uroot -p'Qian@123'

三、记录的导入和导出

1、记录导出

[root@localhost ~]# vim /etc/my.cnf    //在里面写入下面的操作
[mysqld]
secure-file-priv=/backup
[root@localhost ~]# chown mysql.mysql /backup/
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# mysql -p'Qian@123'
//SELECT... INTO OUTFILE导出文本文件
mysql> select * from test into outfile '/backup/testdb.test.txt';


[root@localhost ~]# mysql -uroot -p'Qian@123' -e 'select * from testdb1.t1' > /backup/testdb1.t2.txt
[root@localhost ~]# mysql -uroot -p'Qian@123' --xml -e 'select * from testdb1.t1' > /backup/testdb1.t3.txt
[root@localhost ~]# mysql -uroot -p'Qian@123' --html -e 'select * from testdb1.t1' > /backup/index.html

2、记录导入

//删除表的内容
//表的导出和导入只备份表记录,不会备份表结构,因此需要通过mysqldump备份表结构,恢复时先恢复表结构,再导入数据
mysql> delete from testdb.test;
mysql> select * from testdb.test;
mysql> load data infile '/backup/testdb.test.txt' into table testdb.test;

你可能感兴趣的:(数据库,mysql,sql)