mysql数据库备份恢复目录概要
- 备份库 mysqldump -uroot -p123456 mysql > /tmp/mysql.sql
- 恢复库 mysql -uroot -p123456 mysql < /tmp/mysql.sql
- 备份表 mysqldump -uroot -p123456 mysql user > /tmp/user.sql
- 恢复表 mysql -uroot -p123456 mysql < /tmp/user.sql
- 备份全部库 mysqldump -uroot -p -A >/tmp/123.sql
- 只备份表结构 mysqldump -uroot -p123456 -d mysql > /tmp/mysql.sql
mysql数据库备份恢复
备份库
- 在执行mysqldump -uroot -p123456 mysql的时候会看到不少信息,屏幕上显示的这些就是备份的数据
- 备份mysql库文件
- mysqlbak.sql文件就是mysql的备份库文件
[root@hf-01 ~]# mysqldump -uroot -p'hanfeng' mysql > /tmp/mysqlbak.sql
Warning: Using a password on the command line interface can be insecure.
[root@hf-01 ~]#
- 咱们能够经过mysqlbak.sql来恢复数据库,还能够恢复到另一个数据库里面去
- 建立一个新的库mysql2
[root@hf-01 ~]# mysql -uroot -p'hanfeng' -e "create database mysql2"
Warning: Using a password on the command line interface can be insecure.
[root@hf-01 ~]#
- 恢复库
- mysql -uroot -phanfeng mysql < /tmp/mysql.sql
[root@hf-01 ~]# mysql -uroot -p'hanfeng' mysql < /tmp/mysqlbak.sql
Warning: Using a password on the command line interface can be insecure.
[root@hf-01 ~]#
- 进入到数据库里面,在后面加一个mysql2 就会进入到mysql2数据库里面
- mysql -uroot -p'hanfeng' mysql2
[root@hf-01 ~]# mysql -uroot -p'hanfeng' mysql2
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.35 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
- 查看数据库
mysql> select database();
+------------+
| database() |
+------------+
| mysql2 |
+------------+
1 row in set (0.00 sec)
mysql>
备份表
- 针对库里面的某一个表去作备份,只须要在 库后面 加上 表名字 便可备份
- 先库 在表,中间是空格
- 备份表 mysqldump -uroot -p123456 mysql user > /tmp/user.sql
- 能看到备份的时候,库存在的话,先把库drop掉,而后建立库,表存在的话,先把表drop掉,而后建立表,而后在一步一步的插入每一行数据
[root@hf-01 ~]# mysqldump -uroot -phanfeng mysql user > /tmp/user.sql
Warning: Using a password on the command line interface can be insecure.
[root@hf-01 ~]# less /tmp/user.sql 查看备份表
- 恢复表的时候,只须要写库的名字,不须要去写表的名字
- 恢复表 mysql -uroot -p123456 mysql < /tmp/user.sql
- 恢复mysql2库里面的表
[root@hf-01 ~]# mysql -uroot -phanfeng mysql2 < /tmp/user.sql
Warning: Using a password on the command line interface can be insecure.
[root@hf-01 ~]#
备份全部的库
- 备份全部库 mysqldump -uroot -phanfeng -A >/tmp/123.sql
[root@hf-01 ~]# mysqldump -uroot -phanfeng -A > /tmp/mysql_all.sql
Warning: Using a password on the command line interface can be insecure.
[root@hf-01 ~]#
[root@hf-01 ~]# less /tmp/mysql_all.sql
- 只备份表结构 mysqldump -uroot -phanfeng -d mysql > /tmp/mysql.sql
- 备份mysql2的表结构
[root@hf-01 ~]# mysqldump -uroot -phanfeng -d mysql2 > /tmp/mysql.sql
Warning: Using a password on the command line interface can be insecure.
[root@hf-01 ~]#
[root@hf-01 ~]# less /tmp/mysql.sql
示例
- 两个机器的库备份,一个库备份到另外一台机器上
- 解决:
- 首先两台机器可以通讯
- 而后mysqldump -h 远程mysql-ip -uuser-ppassword dbname > /本地backup.sql
- 这样既可备份