数据的完全备份
1.物理备份
[root@mrloam ~]# cp -r /data/mysql/ /root/mysql.bak
别分策略:
完全+差异
完全+增量
2.完全备份 mysqldump
mysqldump -uroot -p密码 库名 > 目录/文件名.sql
库名的表示方式:
备份1台服务器上的所有数据 --all-databases 或 -A
备份1个库里的所有表 库名
备份1张里的所有记录 库名 表名
只备份指定库的所有表 -B 库名1 库名2
[root@mrloam ~]# mkdir /mysqlbak
[root@mrloam ~]# mysqldump -uroot -p123 --all-databases > /mysqlbak/`date +%F`_fullbak.sql
#将db3库下的user表备份
[root@mrloam ~]# mysqldump -uroot -p123 db3 user > /mysqlbak/`date +%F`_db3_user.sql
#将d1 db3 sql_self_learning_skills 三个库备份
[root@mrloam ~]# mysqldump -uroot -p123 -B d1 db3 sql_self_learning_skills > /mysqlbak/`date +%F`_threedb.sql
3.数据恢复
]# mysql -uroot -p密码 库名 < 目录/文件名.sql
root@mysqldb 16:47: [(none)]> drop table db3.user;
Query OK, 1 row affected (0.00 sec)
root@mysqldb 16:50: [(none)]> use db3;
Database changed
root@mysqldb 16:50: [db3]> show tables;
Empty set (0.00 sec)
[root@mrloam ~]# mysql -uroot -p123 db3 < /mysqlbak/2023-09-12_db3_user.sql
[root@mrloam ~]# mysql -uroot -p123 -e 'use db3 ; select * from user'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+-----------------+-----+----------+------+------+---------+--------------------+----------------+
| id | name | age | password | uid | gid | comment | homedir | shell |
+----+-----------------+-----+----------+------+------+---------+--------------------+----------------+
| 1 | root | 19 | x | 0 | 0 | root | /root | /bin/bash |
| 2 | bin | 19 | A | 1 | 1 | student | /bin | /sbin/nologin |
| 3 | daemon | 19 | A | 2 | 2 | student | /sbin | /sbin/nologin |
| 4 | adm | 19 | A | 3 | 4 | student | /var/adm | /sbin/nologin |
| 5 | lp | 19 | A | 4 | 7 | student | /var/spool/lpd | /sbin/nologin |
| 6 | sync | 19 | A | 5 | 0 | student | /sbin | /bin/sync |
| 7 | shutdown | 19 | A | 6 | 0 | student | /sbin | /sbin/shutdown |
| 8 | halt | 19 | A | 7 | 0 | student | /sbin | /sbin/halt |
| 9 | mail | 19 | A | 8 | 12 | student | /var/spool/mail | /sbin/nologin |
| 10 | operator | 19 | A | 11 | 0 | student | /root | /sbin/nologin |
| 11 | games | 19 | A | 12 | 100 | student | /usr/games | /sbin/nologin |
| 12 | ftp | 19 | A | 14 | 50 | student | /var/ftp | /sbin/nologin |
| 13 | nobody | 19 | A | 99 | 99 | student | / | /sbin/nologin |
| 14 | systemd-network | 19 | A | 192 | 192 | student | / | /sbin/nologin |
| 15 | dbus | 19 | A | 81 | 81 | student | / | /sbin/nologin |
| 16 | polkitd | 19 | A | 999 | 998 | student | / | /sbin/nologin |
| 17 | sshd | 19 | A | 74 | 74 | student | /var/empty/sshd | /sbin/nologin |
| 18 | postfix | 19 | A | 89 | 89 | student | /var/spool/postfix | /sbin/nologin |
| 19 | chrony | 19 | A | 998 | 996 | student | /var/lib/chrony | /sbin/nologin |
| 20 | mysql | 19 | A | 1000 | 1000 | student | /home/mysql | /sbin/nologin |
| 25 | yaya9 | 19 | NULL | NULL | NULL | NULL | NULL | NULL |
| 26 | y7aya | 19 | NULL | NULL | NULL | NULL | NULL | NULL |
| 27 | ya6ya | 19 | NULL | NULL | NULL | NULL | NULL | NULL |
| 28 | 3yaya | 19 | NULL | NULL | NULL | NULL | NULL | NULL |
+----+-----------------+-----+----------+------+------+---------+--------------------+----------------+
[root@mrloam ~]# mysql -uroot -p123 < /mysqlbak/2023-09-12_fullbak.sql
mysqldump 的缺点: 新产生的数据无法恢复 、备份数据 和恢复数据 都会 锁表
4.增量备份
启用mysql服务的binlog日志文件
[root@mrloam ~]# grep "log-bin" /etc/my.cnf
log-bin = /data/mysql/binlog/mysql-binlog
[root@mrloam ~]# mysqlbinlog /data/mysql/binlog/mysql-binlog.000013 | mysql -uroot -p123
命令格式 ]# mysqlbinlog 选项 目录/日志名 | mysql -uroot -p密码
选项:
时间范围 --start-datetime="yyyy/mm/dd hh:mm:ss" --stop-datetime="yyyy/mm/dd hh:mm:ss"
偏移量范围 --start-position=数字 --stop-position=数字
]# mysqlbinlog --start-position=325 --stop-position=774 /data/mysql/binlog/mysql-binlog.000012 | mysql -uroot -p123
]# mysqlbinlog --start-datetime="2023/09/12 00:00:59"
--stop-datetime="2023/09/12 17:31:26" /data/mysql/binlog/mysql-binlog.000012 | mysql -uroot -p123