标签:sql命令 技能 防范 数据库备份 复数 时间 mysql命令 0 rows 热备
MySQL完全备份与恢复#数据库文件都在/usr/local/mysql/data文件夹里
[root@localhost ~]# cd /usr/local/mysql/data/
[root@localhost data] ls #整个data文件夹进行备份
@94f6@884c@5ba2@6237@8868 ib_buffer_pool ib_logfile0 ibtmp1 performance_schema sys
auto.cnf ibdata1 ib_logfile1 mysql shcool
[root@localhost ~] tar Jcvf /opt/mysql-$(date +%F).tar.xz /usr/local/mysql/data/
[root@localhost ~] cd /opt/
[root@localhost opt] ls #会压缩成tar.xz格式的压缩包
mysql-2018-07-02.tar.xz
[root@localhost opt] du -sh /usr/local/mysql/data/
134M /usr/local/mysql/data/
[root@localhost opt] du -sh #用du -sh查看包的大小节省了很多空间现在把data的文件夹删除都没事 /opt/mysql-2018-07-02.tar.xz
704K /opt/mysql-2018-07-02.tar.xz
#解压缩恢复数据文件
[root@localhost opt] tar Jxvf /opt/mysql-2018-07-02.tar.xz /usr/local/mysql/data/
mysql> show databases; #对shcool数据库进行完全备份
+--------------------+
| Database |
+--------------------+
| information_schema |
| ×××表 |
| mysql |
| performance_schema |
| shcool |
| sys |
+--------------------+
6 rows in set (0.00 sec)
[root@localhost opt]# mysqldump -u root -p shcool > /opt/shcool.sql #-p后面跟密码可以免交互
Enter password:
[root@localhost opt]# ls
shcool.sql
mysql> show tables;
+------------------+
| Tables_in_shcool |
+------------------+
| info |
+------------------+
1 row in set (0.00 sec)
[root@localhost opt] mysqldump -u root -p shcool info > /opt/info.sql
mysql> show databases; #对shcool数据库和mysql数据库进行备份
+--------------------+
| Database |
+--------------------+
| information_schema |
| ×××表 |
| mysql |
| performance_schema |
| shcool |
| sys |
+--------------------+
[root@localhost ~] mysqldump -u root -p --databases shcool mysql > /opt/shcool-mysql.sql
[root@localhost ~] cd /opt/
[root@localhost opt] ls
shcool-mysql.sql
[root@localhost opt] mysqldump -u root -p --all-databases > /opt/all.sql
Enter password:
[root@localhost opt] ls
all.sql
[root@localhost opt] mysqldump -u root -p -d mysql usr > /opt/usr.sql
Enter password:
[root@localhost opt] ls
usr.sql
#把备份的shcool数据库恢复(注数据库是由表构成的所以要恢复数据要先创建chcool的数据库进入数据库用source恢复)
mysql> show databases; #这边没有shcool数据库先创建
+--------------------+
| Database |
+--------------------+
| information_schema |
| ×××表 |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> create database shcool; #创建数据库
Query OK, 1 row affected (0.00 sec)
mysql> show databases; #已经有这个数据库了
+--------------------+
| Database |
+--------------------+
| information_schema |
| ×××表 |
| mysql |
| performance_schema |
| shcool |
| sys |
+--------------------+
6 rows in set (0.00 sec)
mysql> use shcool; #进入数据库看有没有表
Database changed
mysql> show tables; #里面没有任何表
Empty set (0.00 sec)
mysql> source /opt/shcool.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)
mysql> show tables; #数据库恢复成功
+------------------+
| Tables_in_shcool |
+------------------+
| info |
+------------------+
1 row in set (0.00 sec)
#同样先创建同名数据库
[root@localhost opt] mysql -u root -p shcool < /opt/shcool.sql
Enter password:
[root@localhost opt] mysql -u root -p
Enter password:
mysql> show tables; #恢复完成
+------------------+
| Tables_in_shcool |
+------------------+
| info |
+------------------+
1 row in set (0.00 sec)
标签:sql命令 技能 防范 数据库备份 复数 时间 mysql命令 0 rows 热备
原文地址:http://blog.51cto.com/13645280/2135049