数据库名为school
数据表如下;
3.备份数据库school到/backup目录
mkdir backup
[root@locahost ~]# mysqldump -uroot -pRedHat@123 -B school > backup/school_db.sql
4.备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库
[root@localhost backup]# mysqldump --add-drop-table -uroot -pRedHat@123 school > /root/backup/school_table.sql
5.直接将MySQL数据库压缩备份
[root@localhost backup]# mysqldump -uroot -pRedHat@123 school | gzip > /root/backup/school_zip.sql.gz
6.备份MySQL数据库某个(些)表。此例备份student表
[root@localhost backup]# mysqldump -uroot -pRedHat@123 school student > table/student_table.sql
7.同时备份多个MySQL数据库(其他数据库素材自行准备)
[root@localhost backup]# mysqldump -uroot -pRedHat@123 -B school kun db1 > /root/backup/database_two.sql
8.仅仅备份数据库结构
[root@localhost ~]# mydumper -u root -p RedHat@123 -B school -o backup/school/
9.备份服务器上所有数据库
[root@localhost ~]# mysqldump -uroot -pRedHat@123 -A > backup/db/all_db.sql
10.还原MySQL数据库
[root@localhost school]# mysql -uroot -pRedHat@123 -e "drop database school;"
[root@localhost backup]# scp 192.168.227.133:backup/school_db.sql /opt/
mysql> source /opt/school_db.sql;
11.还原压缩的mySQL数据库
[root@localhost backup]# gunzip < school_zip.sql | mysql -uroot -pRedHat@123 school
12.使用xtrabackup 备份数据库
[root@localhost ~]# innobackupex --defaults-file=/etc/my.cnf --user=root --password='RedHat@123' /backup
13.在另外的数据库服务器上还原xtrabackup 备份
root@localhost ~]# scp -r /backup/2023-07-17_00-52-50/ root@192.168.227.133:/backups/
[root@localhost ~]# innobackupex --apply-log /backups/2023-07-17_00-52-50/
systemctl stop mysqld
另一台服务器
[root@localhost ~]# rm -rf /usr/local/mysql/data
[root@localhost ~]# innobackupex --copy-back /backups/2023-07-17_00-52-50/
[root@localhost ~]# ll /usr/local/mysql/data/
#修改属主属组为mysql
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql/data/
#启动服务
[root@localhost ~]# systemctl start mysqld
#查看数据是否恢复
[root@localhost ~]# mysql -uroot -p -e"show databases;"
14.使用mydumper备份数据
[root@localhost backup]# mydumper -u root -p RedHat@123 -B school -o /root/backup/mydumper
15.使用mydumper恢复数据库
[root@localhost mydumper]# mysql -uroot -pRedHat@123 -e 'drop database school'
[root@localhost mydumper]# mysql -uroot -pRedHat@123 -e 'drop database school'