逻辑备份mysqldump
- MySQL 自带的逻辑备份工具。可以保证数据的一致性和服务的可用性
- 原理是通过协议连接到 MySQL 数据库,将需要备份的数据查询出来,将查询出的数据转换成对应的 insert 语句,当我们需要还原这些数据时,只要执行这些 insert 语句,即可将对应的数据还原。
备份命令
mysqldump [选项] 数据库名 [表名] > 脚本名
mysqldump [选项] --数据库名 [选项 表名] > 脚本名
mysqldump [选项] --all-databases [选项] > 脚本名
--备份所有数据库
mysqldump -uroot -p --all-databases > /backup/mysqldump/all.db
--备份指定数据库
mysqldump -uroot -p test > /backup/mysqldump/test.db
--备份指定数据库的数据表
mysqldump -uroot -p mysql db event > /backup/mysqldump/2table.db
--备份指定数据库 排除某些表
mysqldump -uroot -p test --ignore-table=test.t1 --ignore-table=test.t2 > /backup/mysqldump/test2.db
还原命令
--在导入备份数据库前,db_name如果没有,是需要创建的; 而且与db_name.db中数据库名是一样的才可以导入。
mysqladmin -uroot -p create db_name
mysql -uroot -p db_name < /backup/mysqldump/db_name.db
--source方法
mysql > use db_name
mysql > source /backup/mysqldump/db_name.db
MySQL环境
系统版本 | mysql版本 | 安装方式 |
---|---|---|
centos7 | 5.7.42 | yum |
完整备份与恢复
-
修改配置文件,开启二进制日志
[root@mysql ~]# cat /etc/my.cnf | grep "mysql\|server-id" # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [mysqld] basedir=/server/mysql datadir=/server/mysql/data socket=/var/lib/mysql/mysql.sock server-id = 8 log-bin=/var/log/mysql/bin-log log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
-
创建所需目录,并授权
[root@mysql ~]# mkdir /server/mysql/data -p [root@mysql ~]# chown -R mysql:mysql /server/mysql/* [root@mysql ~]# mkdir -p /var/log/mysql [root@mysql ~]# chown -R mysql:mysql /var/log/mysql [root@mysql ~]# mkdir -p /backup/mysql -p [root@mysql ~]# chown -R mysql:mysql /backup/mysql/ #重启mysql [root@mysql ~]# systemctl restart mysqld
-
创建数据库
--创建数据库mydb create database mydb; mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mydb | | mysql | | performance_schema | | sys | +--------------------+
-
进行全量备份
mysqldump -uroot -hlocalhost -p'Rkun18.mysql' -P3306 --all-databases --triggers --routines --events --single-transaction --master-data=1 --flush-logs --set-gtid-purged=OFF > /backup/mysql/$(date +%F%H)-mysql-all.sql [root@mysql ~]# ll /backup/mysql 总用量 868 -rw-r--r--. 1 root root 888191 6月 12 17:15 2023-06-1217-mysql-all.sql
-
模拟文件被清除
[root@mysql ~]# systemctl stop mysqld [root@mysql ~]# rm -rf /server/mysql/data/* [root@mysql ~]# systemctl restart mysqld #mydb数据库消失 [root@mysql ~]# ls /server/mysql/data/ auto.cnf client-cert.pem ibdata1 ibtmp1 private_key.pem server-key.pem ca-key.pem client-key.pem ib_logfile0 mysql public_key.pem sys ca.pem ib_buffer_pool ib_logfile1 performance_schema server-cert.pem #重新启动后可能密码无法登录 需要查找初始化密码后自己修改 [root@mysql ~]# cat /var/log/mysqld.log | grep 'password' 2023-06-12T15:11:08.867826Z 1 [Note] A temporary password is generated for root@localhost: 8s*e9<_d=SI& 2023-06-12T15:36:16.343471Z 5 [Note] Access denied for user '-p'@'localhost' (using password: NO) 2023-06-12T20:15:48.106914Z 0 [Note] Shutting down plugin 'validate_password' 2023-06-12T20:15:49.937428Z 0 [Note] Shutting down plugin 'sha256_password' 2023-06-12T20:15:49.937433Z 0 [Note] Shutting down plugin 'mysql_native_password' 2023-06-12T20:49:02.437269Z 0 [Note] Shutting down plugin 'sha256_password' 2023-06-12T20:49:02.437272Z 0 [Note] Shutting down plugin 'mysql_native_password' 2023-06-12T20:49:03.808190Z 1 [Note] A temporary password is generated for root@localhost: aw,MeyEg-26E 2023-06-12T21:34:41.847131Z 0 [Note] Shutting down plugin 'sha256_password' 2023-06-12T21:34:41.847136Z 0 [Note] Shutting down plugin 'mysql_native_password' 2023-06-12T21:38:57.499224Z 1 [Note] A temporary password is generated for root@localhost: >erR;TW#s1k1
#追加不记录二进制日志 [root@mysql ~]# sed -i '23a SET sql_log_bin=0;' /backup/mysql/2023-06-1217-mysql-all.sql
-
恢复数据(数据库内部操作)
set sql_log_bin=0; mysql> source /backup/mysql/2023-06-1217-mysql-all.sql; set sql_log_bin=1; --导入后密码并不改变 当更新权限后 密码又恢复到备份时密码 flush privileges ; mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mydb | | mysql | | performance_schema | | sys | +--------------------+
增量备份与恢复
- 备份环境与恢复
- 数据库完整备份+数据库增量备份
- 新建数据表,进行全量备份,随时间推移,数据库突然崩溃
/etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=/var/lib/mysql/bin-log
server-id=2
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
备份前
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
create database mydb;
create table mydb.t1(id int,name varchar(20));
mysql> insert into mydb.t1 values(1,"test1");
Query OK, 1 row affected (0.01 sec)
mysql> insert into mydb.t1 values(2,"test2");
Query OK, 1 row affected (0.01 sec)
mysql> select * from mydb.t1;
+------+-------+
| id | name |
+------+-------+
| 1 | test1 |
| 2 | test2 |
+------+-------+
基于当前状态做一次全备
mysqldump -uroot -hlocalhost -p'Rkun18.mysql' -P3306 --all-databases --triggers --routines --events --single-transaction --master-data=1 --flush-logs --set-gtid-purged=OFF > /backup/mysql/$(date +%F%H)-mysql-all.sql
进入数据库再插入数据
mysql> insert into mydb.t1 values(3,"test3");
Query OK, 1 row affected (0.00 sec)
mysql> insert into mydb.t1 values(4,"test4");
Query OK, 1 row affected (0.00 sec)
mysql> select * from mydb.t1;
+------+-------+
| id | name |
+------+-------+
| 1 | test1 |
| 2 | test2 |
| 3 | test3 |
| 4 | test4 |
+------+-------+
模拟数据库崩溃
-
重启初始化,启动数据库,更改默认密码
[root@mysql ~]# systemctl stop mysqld [root@mysql ~]# rm -rf /var/lib/mysql/* [root@mysql ~]# systemctl start mysqld #使用最新密码 [root@mysql ~]# grep 'A temporary password' /var/log/mysqld.log 2023-06-12T15:11:08.867826Z 1 [Note] A temporary password is generated for root@localhost: 8s*e9<_d=SI& 2023-06-12T20:25:48.353946Z 1 [Note] A temporary password is generated for root@localhost: /u,8HyZnHgKn [root@mysql ~]# mysql -uroot -p'/u,8HyZnHgKn'
mysql> alter user 'root'@'localhost' identified by 'Rkun18.mysql' -> ; Query OK, 0 rows affected (0.00 sec) mysql> \q Bye
[root@mysql ~]# mysql -uroot -p'Rkun18.mysql'
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+
-
恢复全量数据
[root@mysql ~]# sed -i '23a SET sql_log_bin=0;' /backup/mysql/2023-06-1216-mysql-all.sql [root@mysql ~]# mysql -uroot -p'Rkun18.mysql' < /backup/mysql/2023-06-1216-mysql-all.sql mysql: [Warning] Using a password on the command line interface can be insecure. [root@mysql ~]# mysql -uroot -p'Rkun18.mysql' -e "select * from mydb.t1" mysql: [Warning] Using a password on the command line interface can be insecure. +------+-------+ | id | name | +------+-------+ | 1 | test1 | | 2 | test2 | +------+-------+
-
恢复增量备份
[root@mysql ~]# sed -n '22p' /backup/mysql/2023-06-1216-mysql-all.sql #全量备份到154位置 CHANGE MASTER TO MASTER_LOG_FILE='bin-log.000002', MASTER_LOG_POS=154;
-
查看数据库中二进制文件
mysql> show binary logs; +----------------+-----------+ | Log_name | File_size | +----------------+-----------+ | bin-log.000001 | 177 | | bin-log.000002 | 398 | +----------------+-----------+
-
根据MASTER_LOG_POS恢复增量数据
#这里只有两个bin-log 从2开始就到最后了 mysqlbinlog --start-position=154 bin-log.000002 | mysql -uroot -pRkun18.mysql;