MysqL数据库备份和恢复数据

环境准备:
系统: Centos 7.3

[root@ localhost backup]# cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 
# 创建备份的目录
mkdir /backup
# 备份命令
mysqldump -uroot -p123 test > /backup/test.sql
cd /backup/
[root@ localhost backup]# ls
test.sql
[root@ localhost backup]# more test.sql 
-- MySQL dump 10.14  Distrib 5.5.64-MariaDB, for Linux (x86_64)
--
-- Host: localhost    Database: test
-- ------------------------------------------------------
-- Server version	5.5.64-MariaDB

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;

模拟数据库被删除恢复数据

[root@ localhost backup]# mysql -uroot -p123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| szm                |
| test               |
| testdb             |
+--------------------+
6 rows in set (0.00 sec)

MariaDB [(none)]> drop database test;
Query OK, 1 row affected (0.06 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| szm                |
| testdb             |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> exit

再次登录至数据库

 mysql -uroot -p123
 # 这里虽然创建了库,但是是没有数据的空库
 create database test;
 mysql -uroot -p123 test < /backup/test.sql 

恢复数据方式一

MysqL数据库备份和恢复数据_第1张图片

恢复数据方式二

# 模拟数据库里的表被误删导致数据丢失
MariaDB [test]> drop table szm;
Query OK, 0 rows affected (0.01 sec)

# 表已为空
MariaDB [test]> show tables;
Empty set (0.00 sec)

# 加载并恢复数据
MariaDB [test]> source /backup/test.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.01 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, 2 rows affected (0.00 sec)
Records: 2  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)

# 再次查看表
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| szm            |
+----------------+
1 row in set (0.00 sec)

# 表里的数据已经回来了
MariaDB [test]> select * from szm;
+------+----------+
| id   | password |
+------+----------+
|    1 | test1    |
|   12 | test12   |
+------+----------+
2 rows in set (0.00 sec)

备份所有的库

# 备份所有的数据库
 mysqldump -uroot -p123 -A > /backup/all.sql
 mysqldump -uroot -p123 -all > /backup/all.sql 
# 创建目录
 mkdir /backup/all
 cd /var/lib/mysql/
 mv * /backup/all/
 systemctl restart mariadb
# 恢复所有的数据
 mysql -uroot < /backup/all.sql

再查看的时候数据已经恢复了

[root@ localhost ~]# cd /var/lib/mysql/
[root@ localhost mysql]# ls
aria_log.00000001  ib_logfile1                mysql-bin.000001  mysql-bin.000005  mysql-bin.000009  mysql-bin.000013    szm
aria_log_control   localhost.localdomain.err  mysql-bin.000002  mysql-bin.000006  mysql-bin.000010  mysql-bin.index     test
ibdata1            localhost.localdomain.pid  mysql-bin.000003  mysql-bin.000007  mysql-bin.000011  mysql.sock          testdb
ib_logfile0        mysql                      mysql-bin.000004  mysql-bin.000008  mysql-bin.000012  performance_schema
[root@ localhost mysql]# 

MysqL数据库备份和恢复数据_第2张图片

MysqL数据库备份和恢复数据_第3张图片

[root@ localhost backup]# cd /backup/
[root@ localhost backup]# ls
all  all.sql  test.sql

# 这里的大B是备份多个数据库后面对应的是数据库的名称
mysqldump -uroot -p123 -B szm test testdb > /backup/szm.sql
# 这里备份的是test库下的szm表
mysqldump -uroot -p123 test szm > /backup/student.sql

MysqL数据库备份和恢复数据_第4张图片

下面开始做冷备份

直接备份/var/lib/mysql/目录下的数据库 (目录)

恢复: 将备份的文件复制到/var/lib/mysql/目录下,然后将属主和属组修改为mysql,然后重启mysql数据库服务即可完成恢复

[root@ localhost backup]# cd /var/lib/mysql/
[root@ localhost mysql]# ls
aria_log.00000001  ib_logfile1               mysql             mysql-bin.000004  mysql.sock          test
aria_log_control   mariadb-relay-bin.000001  mysql-bin.000001  mysql-bin.000005  performance_schema  testdb
ibdata1            mariadb-relay-bin.index   mysql-bin.000002  mysql-bin.000006  relay-log.info
ib_logfile0        master.info               mysql-bin.000003  mysql-bin.index   szm
[root@ localhost mysql]# rm -rf *
[root@ localhost mysql]# ls
[root@ localhost all]# cd /backup/all/
[root@ localhost all]# ls
aria_log.00000001  ib_logfile0               mariadb-relay-bin.index  mysql-bin.000001  mysql-bin.000004  mysql-bin.index     szm
aria_log_control   ib_logfile1               master.info              mysql-bin.000002  mysql-bin.000005  performance_schema  test
ibdata1            mariadb-relay-bin.000001  mysql                    mysql-bin.000003  mysql-bin.000006  relay-log.info      testdb
# 拷贝
cp -r /backup/all/* /var/lib/mysql/

数据恢复了

[root@ localhost all]# cd /var/lib/mysql/
[root@ localhost mysql]# ls
aria_log.00000001  ib_logfile0               mariadb-relay-bin.index  mysql-bin.000001  mysql-bin.000004  mysql-bin.index     szm
aria_log_control   ib_logfile1               master.info              mysql-bin.000002  mysql-bin.000005  performance_schema  test
ibdata1            mariadb-relay-bin.000001  mysql                    mysql-bin.000003  mysql-bin.000006  relay-log.info      testdb
[root@ localhost mysql]# 

MysqL数据库对当前目录下所有的文件没有任何权限,它们当前是root的属主属组,这样会导致mysql数据库没有查看也不能读也不能写的权限,所以要更改为mysql的属主属组

[root@ localhost mysql]# ls -l
total 28744
-rw-r----- 1 root root    16384 Dec 26 16:24 aria_log.00000001
-rw-r----- 1 root root       52 Dec 26 16:24 aria_log_control
-rw-r----- 1 root root 18874368 Dec 26 16:24 ibdata1
-rw-r----- 1 root root  5242880 Dec 26 16:24 ib_logfile0
-rw-r----- 1 root root  5242880 Dec 26 16:24 ib_logfile1
-rw-r----- 1 root root      245 Dec 26 16:24 mariadb-relay-bin.000001
-rw-r----- 1 root root       27 Dec 26 16:24 mariadb-relay-bin.index
-rw-r----- 1 root root       38 Dec 26 16:24 master.info
drwx------ 2 root root     4096 Dec 26 16:24 mysql
-rw-r----- 1 root root     1966 Dec 26 16:24 mysql-bin.000001
-rw-r----- 1 root root     2211 Dec 26 16:24 mysql-bin.000002
-rw-r----- 1 root root      682 Dec 26 16:24 mysql-bin.000003
-rw-r----- 1 root root      264 Dec 26 16:24 mysql-bin.000004
-rw-r----- 1 root root      949 Dec 26 16:24 mysql-bin.000005
-rw-r----- 1 root root     2701 Dec 26 16:24 mysql-bin.000006
-rw-r----- 1 root root      114 Dec 26 16:24 mysql-bin.index
drwx------ 2 root root     4096 Dec 26 16:24 performance_schema
-rw-r----- 1 root root       32 Dec 26 16:24 relay-log.info
drwx------ 2 root root       20 Dec 26 16:24 szm
drwx------ 2 root root       21 Dec 26 16:24 test
drwx------ 2 root root       35 Dec 26 16:24 testdb
# 修改当前目录下所有文件的属主属组
[root@ localhost mysql]# chown -R mysql.mysql *
[root@ localhost mysql]# ls -l
total 28744
-rw-r----- 1 mysql mysql    16384 Dec 26 16:24 aria_log.00000001
-rw-r----- 1 mysql mysql       52 Dec 26 16:24 aria_log_control
-rw-r----- 1 mysql mysql 18874368 Dec 26 16:24 ibdata1
-rw-r----- 1 mysql mysql  5242880 Dec 26 16:24 ib_logfile0
-rw-r----- 1 mysql mysql  5242880 Dec 26 16:24 ib_logfile1
-rw-r----- 1 mysql mysql      245 Dec 26 16:24 mariadb-relay-bin.000001
-rw-r----- 1 mysql mysql       27 Dec 26 16:24 mariadb-relay-bin.index
-rw-r----- 1 mysql mysql       38 Dec 26 16:24 master.info
drwx------ 2 mysql mysql     4096 Dec 26 16:24 mysql
-rw-r----- 1 mysql mysql     1966 Dec 26 16:24 mysql-bin.000001
-rw-r----- 1 mysql mysql     2211 Dec 26 16:24 mysql-bin.000002
-rw-r----- 1 mysql mysql      682 Dec 26 16:24 mysql-bin.000003
-rw-r----- 1 mysql mysql      264 Dec 26 16:24 mysql-bin.000004
-rw-r----- 1 mysql mysql      949 Dec 26 16:24 mysql-bin.000005
-rw-r----- 1 mysql mysql     2701 Dec 26 16:24 mysql-bin.000006
-rw-r----- 1 mysql mysql      114 Dec 26 16:24 mysql-bin.index
drwx------ 2 mysql mysql     4096 Dec 26 16:24 performance_schema
-rw-r----- 1 mysql mysql       32 Dec 26 16:24 relay-log.info
drwx------ 2 mysql mysql       20 Dec 26 16:24 szm
drwx------ 2 mysql mysql       21 Dec 26 16:24 test
drwx------ 2 mysql mysql       35 Dec 26 16:24 testdb
[root@ localhost mysql]# 
# 重启数据库
systemctl restart mariadb

MysqL数据库备份和恢复数据_第5张图片

MysqL数据库备份和恢复数据_第6张图片

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