《MySQL备份与恢复Mysql学习》要点:
本文介绍了MySQL备份与恢复Mysql学习,希望对您有用。如果有疑问,可以联系我们。
- [root@localhost ~]# mysql
- mysql> create database auth;
- Query OK, 1 row affected (0.00 sec)
- mysql> use auth;
- Database changed
- mysql> create table user(name char(10) not null,ID int(48));
- Query OK, 0 rows affected (0.04 sec)
- mysql> insert into user values('crushlinux','123');
- Query OK, 1 row affected (0.01 sec)
- mysql> select * from user;
- +------------+------+
- | name | ID |
- +------------+------+
- | crushlinux | 123 |
- +------------+------+
- 1 row in set (0.00 sec)
- mysql> exit
- Bye
- [root@localhost ~]# service mysqld stop
- Shutting down MySQL.. [ OK ]
- [root@localhost ~]# yum -y install xz
- [root@localhost ~]# tar Jcf mysql_all-$(date +%F).tar.xz /usr/local/mysql/data/
- tar: Removing leading `/' from member names
MYSQL教程模拟数据丢失!
- [root@localhost ~]# mkdir bak
- [root@localhost ~]# mv /usr/local/mysql/data/* bak/
MYSQL教程恢复数据:
- [root@localhost ~]# mkdir restore
- [root@localhost ~]# tar xf mysql_all-2016-12-08.tar.xz -C restore/
- [root@localhost ~]# mv restore/usr/local/mysql/data/* /usr/local/mysql/data/
- [root@localhost ~]# service mysqld start
- Starting MySQL.. [ OK ]
- [root@localhost ~]# mysql
- mysql> select * from auth.user;
- +------------+------+
- | name | ID |
- +------------+------+
- | crushlinux | 123 |
- +------------+------+
- 1 row in set (0.00 sec)
MYSQL教程2、使用专用备份工具 mysqldump
MySQL 自带的备份工具,相当方便对 MySQL 进行备份.通过该命令工具可以将数据库、
数据表或全部的库导出为 SQL 脚本,在需要恢复时可进行数据恢复.
(1)对单个库进行完全备份
格式:mysqldump -u 用户名 -p[密码] [选项] [数据库名] > /备份路径/备份文件名
示例:
- [root@localhost ~]# mkdir /backup
- [root@localhost ~]# mysqldump -uroot -p123123 auth > /backup/auth-$(date +%Y%m%d).sql
- [root@localhost ~]# echo $?
- 0
- [root@localhost ~]# cat /backup/auth-20161208.sql
MYSQL教程(2)对多个库进行完全备份
格式:mysqldump -u 用户名 -p [密码] [选项] --databases 库名 1 [库名 2]… > /备份路径/备份
文件名
示例:
- [root@localhost ~]# mysqldump -uroot -p123123 --databases mysql auth >
- /backup/mysql+auth-$(date +%Y%m%d).sql
- [root@localhost ~]# cat /backup/mysql+auth-20161208.sql
MYSQL教程(3)对所有库进行完全备份
格式:mysqldump -u 用户名 -p [密码] [选项] --all-databases > /备份路径/备份文件名
示例:
- [root@localhost ~]# mysqldump -uroot -p123123 --opt --all-
- databases >/backup/mysql_all.$(date +%Y%m%d).sql
- [root@localhost ~]# cat /backup/mysql_all.20161208.sql
- //--opt 加快备份速度,当备份数据量大时使用<br>[root@localhost ~]# cat /backup/mysql_all.20160505.sql
MYSQL教程(4)对表进行完全备份
格式:mysqldump -u 用户名 -p [密码] [选项] 数据库名 表名 > /备份路径/备份文件名
示例:
- [root@localhost ~]# mysqldump -uroot -p123123 auth user >/backup/auth_user-$(date
- +%Y%m%d).sql
- [root@localhost ~]# cat /backup/auth_user-20161208.sql
MYSQL教程(5)对表结构的备份
格式:mysqldump -u 用户名 -p [密码] -d 数据库名 表名 > /备份路径/备份文件名
示例:
- [root@localhost ~]# mysqldump -uroot -p123123 -d mysql user >/backup/desc_mysql_user-
- $(date +%Y%m%d).sql
- [root@localhost ~]# cat /backup/desc_mysql_user-20161208.sql
MYSQL教程4、使用 mysqldump 备份后,恢复数据库
1、source 命令
登录到 MySQL 数据库,执行 source 备份 sql 脚本路径
示例
- [root@localhost ~]# mysql
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | auth |
- | mysql |
- | performance_schema |
- | test |
- | usr |
- +--------------------+
- 6 rows in set (0.00 sec)
- mysql> drop database auth;
- Query OK, 1 row affected (0.12 sec)
- mysql> source /backup/mysql_all.20161208.sql
- mysql> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | auth |
- | mysql |
- | performance_schema |
- | test |
- | usr |
- +--------------------+
- 6 rows in set (0.00 sec)
MYSQL教程2、mysql 命令
格式:mysql -u 用户名 -p [密码] < 库备份脚本的路径
mysql -u 用户名 -p [密码] 库名 < 表备份脚本的路径
示例:
- [root@localhost ~]# mysql -uroot -p123123 -e 'show databases;'
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | auth |
- | mysql |
- | performance_schema |
- | test |
- | usr |
- +--------------------+
- [root@localhost ~]# mysql -uroot -p123123 -e 'drop database auth;'
- [root@localhost ~]# mysql -uroot -p123123 < /backup/mysql_all.20161208.sql
- [root@localhost ~]# mysql -uroot -p123123 -e 'show databases;'
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | auth |
- | mysql |
- | performance_schema |
- | test |
- | usr |
- +--------------------+
- [root@localhost ~]# mysql -uroot -p123123 -e 'drop table auth.user;'
- [root@localhost ~]# mysql -uroot -p123123 auth< /backup/auth_user-20161208.sql
- [root@localhost ~]# mysql -uroot -p123123 -e 'select * from auth.user;'
MYSQL教程+------------+------+
| name | ID |
+------------+------+
| crushlinux | 123 |
+------------+------+
转载请注明本页网址:
http://www.vephp.com/jiaocheng/5593.html