当前位置: 首页 > news >正文

无锡住房和城乡建设部网站武汉建站优化厂家

无锡住房和城乡建设部网站,武汉建站优化厂家,个人网站建设模板,手机软件怎么做目录 一、数据备份 1、概述 2、MySQLdump命令备份 1)备份单个数据库中的所有表 2) 备份数据中某个或多个表 3) 备份所有数据库 4)备份多个库 5) 只备份一个表或多个表结构 二、数据恢复 三、数据备份与恢复应用 一、数据备份 1、概述 数据备…

目录

一、数据备份

1、概述

2、MySQLdump命令备份

1)备份单个数据库中的所有表

2) 备份数据中某个或多个表

3) 备份所有数据库

4)备份多个库

5) 只备份一个表或多个表结构

二、数据恢复

三、数据备份与恢复应用


一、数据备份

1、概述

数据备份是数据库管理员非常重要的工作之一。系统意外崩溃或者硬件的损坏都可能导致数据库的丢失,因此MySQL数据管理员需要定期进行数据库备份,使得意外发生尽可能的减少损失。

2、MySQLdump命令备份

该备份方式是系统自己提供的一种备份方式,可以更具需求选择选项。

基本语法

mysqldump -u 用户名 -h 主机名 -p 密码 数据库名[ 表名] > 备份文件目录/文件名.sql

mysqldump 常用选项:
            --defaults-file=                 备份到默认配置文件
            -A, --all-databases          备份所有库
            -B, --databases               备份结果多了创建库和切换库命令---这个便于数据恢复。
            -d, --no-data                    只备份结构,不备份数据
            -R                                    可以备份存储过程和函数

可以用 mysqldump --help命令来查看其他选项

1)备份单个数据库中的所有表

mysqldump -u用户名 -p密码 数据库名 > /备份目录/文件名.sql
mysqldump -u用户名 -p密码 -B 数据库名 > /备份目录/文件名.sql --会备份库的创建和切换到这个库的命令

2) 备份数据中某个或多个表

        多个表空格间隔

mysqldump -u用户名 -p密码 库名 表名1 [表名2……] > /备份目录/[表名1|表名2|……].sql

3) 备份所有数据库

mysqldump -u用户名 -p密码 -A > /备份目录/文件名.sql

4)备份多个库

mysqldump -u用户名 -p密码 --databases 数据库1 [数据库2 ……] > /备份目录/文件.sql

5) 只备份一个表或多个表结构

mysqldump -u用户名 -p密码 -d 库名 表名1 [表名2……] > /备份目录/[表名1|表名2|……].sql

二、数据恢复

1)使用mysql命令恢复

mysql -u用户名 -p'密码' 数据库 < /选择备份数据的路径.sql文件

2)进入数据库,使用 source 加载备份文件恢复

需要创建数据,然后切换到该数据库。

mysql -u用户名 -p'密码' -e 'source /恢复文件的路径.sql文件'#方法2 
#进入数据库,创建一个数据库,然后切换到创建的数据库,再执行下面命令
source 文件路径

三、数据备份与恢复应用

素材

CREATE DATABASE booksDB;
use booksDB;CREATE TABLE books
(
bk_id  INT NOT NULL PRIMARY KEY,
bk_title VARCHAR(50) NOT NULL,
copyright YEAR NOT NULL
);INSERT INTO books
VALUES (11078, 'Learning MySQL', 2010),
(11033, 'Study Html', 2011),
(11035, 'How to use php', 2003),
(11072, 'Teach youself javascript', 2005),
(11028, 'Learing C++', 2005),
(11069, 'MySQL professional', 2009),
(11026, 'Guide to MySQL 5.5', 2008),
(11041, 'Inside VC++', 2011);CREATE TABLE authors
(
auth_id     INT NOT NULL PRIMARY KEY,
auth_name  VARCHAR(20),
auth_gender CHAR(1)
);INSERT INTO authors  
VALUES (1001, 'WriterX' ,'f'),
(1002, 'WriterA' ,'f'),
(1003, 'WriterB' ,'m'),
(1004, 'WriterC' ,'f'),
(1011, 'WriterD' ,'f'),
(1012, 'WriterE' ,'m'),
(1013, 'WriterF' ,'m'),
(1014, 'WriterG' ,'f'),
(1015, 'WriterH' ,'f');CREATE TABLE authorbook
(
auth_id  INT NOT NULL,
bk_id   INT NOT NULL,
PRIMARY KEY (auth_id, bk_id),
FOREIGN KEY (auth_id) REFERENCES authors (auth_id),
FOREIGN KEY (bk_id) REFERENCES books (bk_id)
);INSERT INTO authorbook
VALUES (1001, 11033), (1002, 11035), (1003, 11072), (1004, 11028),
(1011, 11078), (1012, 11026), (1012, 11041), (1014, 11069);

1、使用mysqldump命令备份数据库中的所有表

先在根目录下创建一个备份文件的目录

[root@master ~]# mkdir /backup

 利用mysqldump备份

[root@master ~]# mysqldump -uroot -pRedHat@123 -B booksDB > /backup/booksDB.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@master ~]# cat /backup/booksDB.sql
-- MySQL dump 10.13  Distrib 5.7.18, for Linux (x86_64)
--
-- Host: localhost    Database: booksDB
-- ------------------------------------------------------
-- Server version	5.7.18/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;--
-- Current Database: `booksDB`
--CREATE DATABASE /*!32312 IF NOT EXISTS*/ `booksDB` /*!40100 DEFAULT CHARACTER SET latin1 */;USE `booksDB`;--
-- Table structure for table `authorbook`
--DROP TABLE IF EXISTS `authorbook`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `authorbook` (`auth_id` int(11) NOT NULL,`bk_id` int(11) NOT NULL,PRIMARY KEY (`auth_id`,`bk_id`),KEY `bk_id` (`bk_id`),CONSTRAINT `authorbook_ibfk_1` FOREIGN KEY (`auth_id`) REFERENCES `authors` (`auth_id`),CONSTRAINT `authorbook_ibfk_2` FOREIGN KEY (`bk_id`) REFERENCES `books` (`bk_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;--
-- Dumping data for table `authorbook`
--LOCK TABLES `authorbook` WRITE;
/*!40000 ALTER TABLE `authorbook` DISABLE KEYS */;
INSERT INTO `authorbook` VALUES (1012,11026),(1004,11028),(1001,11033),(1002,11035),(1012,11041),(1014,11069),(1003,11072),(1011,11078);
/*!40000 ALTER TABLE `authorbook` ENABLE KEYS */;
UNLOCK TABLES;--
-- Table structure for table `authors`
--DROP TABLE IF EXISTS `authors`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `authors` (`auth_id` int(11) NOT NULL,`auth_name` varchar(20) DEFAULT NULL,`auth_gender` char(1) DEFAULT NULL,PRIMARY KEY (`auth_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;--
-- Dumping data for table `authors`
--LOCK TABLES `authors` WRITE;
/*!40000 ALTER TABLE `authors` DISABLE KEYS */;
INSERT INTO `authors` VALUES (1001,'WriterX','f'),(1002,'WriterA','f'),(1003,'WriterB','m'),(1004,'WriterC','f'),(1011,'WriterD','f'),(1012,'WriterE','m'),(1013,'WriterF','m'),(1014,'WriterG','f'),(1015,'WriterH','f');
/*!40000 ALTER TABLE `authors` ENABLE KEYS */;
UNLOCK TABLES;--
-- Table structure for table `books`
--DROP TABLE IF EXISTS `books`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `books` (`bk_id` int(11) NOT NULL,`bk_title` varchar(50) NOT NULL,`copyright` year(4) NOT NULL,PRIMARY KEY (`bk_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;--
-- Dumping data for table `books`
--LOCK TABLES `books` WRITE;
/*!40000 ALTER TABLE `books` DISABLE KEYS */;
INSERT INTO `books` VALUES (11026,'Guide to MySQL 5.5',2008),(11028,'Learing C++',2005),(11033,'Study Html',2011),(11035,'How to use php',2003),(11041,'Inside VC++',2011),(11069,'MySQL professional',2009),(11072,'Teach youself javascript',2005),(11078,'Learning MySQL',2010);
/*!40000 ALTER TABLE `books` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;-- Dump completed on 2023-08-21 18:00:50

2、备份booksDB数据库中的books表

[root@master ~]# mysqldump -uroot -pRedHat@123 booksDB books > /backup/booksDB_books.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@master backup]# ls
booksDB_books.sql  booksDB.sql
[root@master backup]# cat booksDB_books.sql 
-- MySQL dump 10.13  Distrib 5.7.18, for Linux (x86_64)
--
-- Host: localhost    Database: booksDB
-- ------------------------------------------------------
-- Server version	5.7.18/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;--
-- Table structure for table `books`
--DROP TABLE IF EXISTS `books`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `books` (`bk_id` int(11) NOT NULL,`bk_title` varchar(50) NOT NULL,`copyright` year(4) NOT NULL,PRIMARY KEY (`bk_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;--
-- Dumping data for table `books`
--LOCK TABLES `books` WRITE;
/*!40000 ALTER TABLE `books` DISABLE KEYS */;
INSERT INTO `books` VALUES (11026,'Guide to MySQL 5.5',2008),(11028,'Learing C++',2005),(11033,'Study Html',2011),(11035,'How to use php',2003),(11041,'Inside VC++',2011),(11069,'MySQL professional',2009),(11072,'Teach youself javascript',2005),(11078,'Learning MySQL',2010);
/*!40000 ALTER TABLE `books` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;-- Dump completed on 2023-08-21 18:28:06

3、使用mysqldump备份booksDB和test数据库

[root@master ~]# mysqldump -uroot -pRedHat@123 --databases booksDB test > /backup/DB_booksDB_test.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

可以查看备份

4、使用mysqldump备份服务器中的所有数据库

[root@master ~]# mysqldump -uroot -pRedHat@123 -A > /backup/DB_all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.

查看备份的数据,内容较多 

5、使用mysql命令还原第二题导出的books表

把传在一个全新的主机上

[root@master ~]# scp 192.168.78.143:/backup/booksDB_books.sql /backup

在主机2上去查看

因为我们之前备份的时候没有选择备份数据库,创建一个数据库

mysql> create database DB1;
Query OK, 1 row affected (0.00 sec)mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| DB1                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

先查看该数据库里面是没有表

再进行备份

[root@master2 ~]# mysql -uroot -p'Root@123;MySQL' DB1 < /backup/booksDB_books.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.

再查看你备份后的数据库

6、进入数据库使用source命令还原第二题导出的book表。

同样要先创建一个数据库

mysql> create database DB2;
Query OK, 1 row affected (0.00 sec)mysql> use DB2;
Database changed
mysql> show tables;
Empty set (0.00 sec)

再进行恢复数据

#先切换到要恢复的数据库中,再用下面的source恢复
mysql> source /backup/booksDB_books.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)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected, 1 warning (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.01 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, 8 rows affected (0.00 sec)
Records: 8  Duplicates: 0  Warnings: 0
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, 1 warning (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)
Query OK, 0 rows affected (0.00 sec)

再查看

mysql> select * from books;
+-------+--------------------------+-----------+
| bk_id | bk_title                 | copyright |
+-------+--------------------------+-----------+
| 11026 | Guide to MySQL 5.5       |      2008 |
| 11028 | Learing C++              |      2005 |
| 11033 | Study Html               |      2011 |
| 11035 | How to use php           |      2003 |
| 11041 | Inside VC++              |      2011 |
| 11069 | MySQL professional       |      2009 |
| 11072 | Teach youself javascript |      2005 |
| 11078 | Learning MySQL           |      2010 |
+-------+--------------------------+-----------+
8 rows in set (0.00 sec)
http://www.hkea.cn/news/307994/

相关文章:

  • 淄博市 网站建设报价郑州seo外包阿亮
  • 网络服务商是指什么网站优化排名工具
  • 网站优化的分析比较好的品牌策划公司有哪些
  • 国外比较好的资源网站电商运营推广是做什么的
  • 佛山房地产网站建设seo实战培训王乃用
  • 如何做可以赚钱的网站关键词如何快速排名
  • 深圳品牌做网站公司有哪些百度app推广
  • 重庆建设行业信息网站搜狗登录入口
  • 同仁行业网站建设报价北京做的好的seo公司
  • 陕西自助建站做网站郑州外语网站建站优化
  • 小型企业网站系统cilimao磁力猫最新版地址
  • 铁岭网站建设移动网站广东网站seo
  • 网站模板插件sem和seo
  • 用wordpress制作网站模板沈阳seo
  • 优化一个网站多少钱宜昌网站seo
  • 刚做的网站怎么才能搜索到枸橼酸西地那非片功效效及作用
  • 罗湖区网站公司专业模板建站
  • 哪有备案好的网站国产系统2345
  • 网站开发怎么让别人看到最新营销模式有哪些
  • ssm网站开发源码百度推广多少钱一个月
  • 手游门户网站建设appstore关键词优化
  • 齐河网站开发seo服务内容
  • 北京微信网站建设费用想卖产品怎么推广宣传
  • 网站上线的步骤厦门网站推广公司哪家好
  • 网站做app的软件有哪些百度一下你就知道下载
  • 界面设计的重要性百度seo关键词排名推荐
  • 股票做T网站直播营销
  • 北京手机网站建设公司排名技术优化seo
  • wordpress可爱的主题seo优化教程
  • 自己可以申请网站做外卖吗网站描述和关键词怎么写