mysql数据库备份与恢复
备份单个数据库
[root@master var]# mkdir /backup
[root@master var]# mysqldump -uroot -p luodi_test >/backup/$(date +%F).backup.sql <--备份luodi_test数据库
Enter password:
[root@master var]# ls -l /backup/2013-10-24.bakcup.sql
-rw-r--r-- 1 root root 2032 Oct 24 18:04 /backup/2013-10-24.backup.sql
[root@master var]# cat /backup/backup.sql
-- MySQL dump 10.13 Distrib 5.5.3-m3, for pc-linux-gnu (i686)
--
-- Host: localhost Database: luodi_test
-- ------------------------------------------------------
-- Server version 5.5.3-m3-log
删除原有数库测试
mysql> drop database luodi_test;
Query OK, 1 row affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> create database luodi_test; <--还要创建一个原有数据库名
Query OK, 1 row affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
恢复数据
[root@master var]# mysql -uroot -p luodi_test</backup/2013-10-24.backup.sql
Enter password:
[root@master var]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.5.3-m3-log Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| luodi_test |
| mysql |
| performance_schema |
| web_press |
+--------------------+
8 rows in set (0.00 sec)
mysql> use luodi_test;
Database changed
mysql> show tables; <--发现原有的数据已回来
+----------------------+
| Tables_in_luodi_test |
+----------------------+
| test |
+----------------------+
1 row in set (0.00 sec)
mysql>
1.如何在恢复时不先建立原数据库呢?可以在备份时使用-B选项
[root@master var]# mysqldump -uroot –p –B luodi_test >/backup/$(date +%F).backup.sql
2.在备份时能支持压缩吗?可以使用gzip选项
[root@master var]# mysqldump -uroot -p -B luodi_test|gzip>/backup/$(date +%F).backup.sql.2
[root@master var]# ls -l /backup/2013-10-24.backup.sql
-rw-r--r-- 1 root root 2032 Oct 24 18:04 /backup/2013-10-24.backup.sql
[root@master var]# ls -l /backup/2013-10-24.backup.sql.2
-rw-r--r-- 1 root root 857 Oct 24 18:16 /backup/2013-10-24.bakcup.sql.2
[root@master var]#
3.备份单个数库中的某个表
例:luodi_test数据库中的test表
[root@master var]# mysqldump -uroot -p luodi_test test | gzip>/backup/$(date +%F).test.sql
Enter password:
[root@master var]#
4.如何只备份表结构不备份表的数据呢?
[root@master var]# mysqldump -uroot -p -d luodi_test test>/backup/$(date +%F).test.biao.sql
Enter password:
[root@master var]#
5.导入数据库 常用source 命令 进入mysql数据库控制台, 如mysql -u root -p
mysql>use 数据库
然后使用source命令,后面参数为脚本文件(如这里用到的.sql) mysql>source d:/wcnc_db.sql
mysql> use luodi_test;
Database changed
mysql> source 2013-10-24.bakcup.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)