前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >13.6 mysql数据库备份恢复

13.6 mysql数据库备份恢复

作者头像
运维小白
发布2018-02-06 16:04:50
4.5K0
发布2018-02-06 16:04:50
举报
文章被收录于专栏:运维小白运维小白

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数据库备份恢复

备份库

  1. 在执行mysqldump -uroot -p123456 mysql的时候会看到很多信息,屏幕上显示的这些就是备份的数据
  2. 备份mysql库文件
  • mysqlbak.sql文件就是mysql的备份库文件
代码语言:javascript
复制
[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 ~]# 
  1. 我们可以通过mysqlbak.sql来恢复数据库,还可以恢复到另外一个数据库里面去
  2. 创建一个新的库mysql2
代码语言:javascript
复制
[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 ~]# 
  1. 恢复库
  • mysql -uroot -phanfeng mysql < /tmp/mysql.sql
代码语言:javascript
复制
[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 ~]# 
  1. 进入到数据库里面,在后面加一个mysql2 就会进入到mysql2数据库里面
  • mysql -uroot -p'hanfeng' mysql2
代码语言:javascript
复制
[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> 
  1. 查看数据库
代码语言:javascript
复制
mysql> select database();
+------------+
| database() |
+------------+
| mysql2     |
+------------+
1 row in set (0.00 sec)

mysql> 

备份表

  • 针对库里面的某一个表去做备份,只需要在 库后面 加上 表名字 即可备份
    • 先库 在表,中间是空格
    • 备份表 mysqldump -uroot -p123456 mysql user > /tmp/user.sql
  • 能看到备份的时候,库存在的话,先把库drop掉,然后创建库,表存在的话,先把表drop掉,然后创建表,然后在一步一步的插入每一行数据
代码语言:javascript
复制
[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库里面的表
代码语言:javascript
复制
[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
    • -A 表示all所有的意思
代码语言:javascript
复制
[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的表结构
代码语言:javascript
复制
[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
    • 这样既可备份
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • mysql数据库备份恢复目录概要
  • mysql数据库备份恢复
    • 备份库
      • 备份表
        • 备份所有的库
        • 示例
        相关产品与服务
        云数据库 SQL Server
        腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档