数据库常用备份方案

1.全量备份

是指对某一个时间点上的所有数据或应用进行的一个完全拷贝

优点:数据恢复快

缺点:备份时间长

2.增量备份

是指在一次全备份或上一次增量备份后,以后每次的备份只需备份与前一次相比增加和或者被删除的文件。这就意味着,第一次增量备份的对象是进行全备后所产生的增加和修改的文件;第二次增量备份的对象是进行第一次增量备份后所产生的增加和修改的文件。

优点:没有重复的备份数据;备份时间短;

缺点:恢复数据时必须按一定的顺序进行

差异备份

备份上一次的完全备份后发生变化的所有文件,对哪些增加或者修改文件的备份。在进行恢复时,我们只需对第一次全量备份和最后一次差异备份进行恢复。

二进制格式安装mysql

'提前将MySQL软件包下载到本地:

https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

'

[root@yxr ~]# cd /usr/src/

[root@yxr src]# ls

debug kernels

通过xftp传到/usr/src

如图1:

如图2:

'创建用户和组'

[root@yxr src]# groupadd -r mysql

[root@yxr src]# useradd -M -s /sbin/nologin -g mysql mysql

'解压软件至/usr/local'

[root@yxr src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

[root@yxr src]# ls /usr/local/

bin libexec

etc mysql-5.7.22-linux-glibc2.12-x86_64

games sbin

include share

lib src

lib64

'将解压的软件成一个软链接'

[root@yxr src]# cd /usr/local/

[root@yxr local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql

‘mysql’ -> ‘mysql-5.7.22-linux-glibc2.12-x86_64/’

[root@yxr local]# ll

total 0

drwxr-xr-x. 2 root root 6 Nov 5 2016 bin

drwxr-xr-x. 2 root root 6 Nov 5 2016 etc

drwxr-xr-x. 2 root root 6 Nov 5 2016 games

drwxr-xr-x. 2 root root 6 Nov 5 2016 include

drwxr-xr-x. 2 root root 6 Nov 5 2016 lib

drwxr-xr-x. 2 root root 6 Nov 5 2016 lib64

drwxr-xr-x. 2 root root 6 Nov 5 2016 libexec

lrwxrwxrwx. 1 root root 36 Aug 19 17:27 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/

drwxr-xr-x. 9 root root 129 Aug 19 17:22 mysql-5.7.22-linux-glibc2.12-x86_64

drwxr-xr-x. 2 root root 6 Nov 5 2016 sbin

drwxr-xr-x. 5 root root 49 Aug 10 14:26 share

drwxr-xr-x. 2 root root 6 Nov 5 2016 src

'修改目录/usr/local/mysql的属主属组'

[root@yxr local]# chown -R mysql.mysql /usr/local/mysql

[root@yxr local]# ll /usr/local/mysql -d

lrwxrwxrwx. 1 mysql mysql 36 Aug 19 17:27 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/

'添加环境变量'

[root@yxr local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh

[root@yxr local]# cd

[root@yxr ~]# . /etc/profile.d/mysql.sh

[root@yxr ~]# echo $PATH

/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

'建立数据存放目录'

[root@yxr ~]# mkdir /opt/data

[root@yxr ~]# chown -R mysql.mysql /opt/data/

[root@yxr ~]# ll /opt/

total 0

drwxr-xr-x. 2 mysql mysql 6 Aug 19 17:34 data

'初始化数据库'

[root@yxr ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/

2018-08-19T09:35:42.456244Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2018-08-19T09:35:45.297165Z 0 [Warning] InnoDB: New log files created, LSN=45790

2018-08-19T09:35:45.860507Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2018-08-19T09:35:46.031406Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 3fc6ce6b-a393-11e8-ac64-000c29989243.

2018-08-19T09:35:46.033662Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.

2018-08-19T09:35:46.043424Z 1 [Note] A temporary password is generated for 'root@localhost: gp0Ct)0Rkygg这是密码,随机密码'

'生成配置文件'

[root@yxr ~]# cat > /etc/my.cnf <

> [mysqld]

> basedir = /usr/local/mysql

> datadir = /opt/data

> socket = /tmp/mysql.sock

> port = 3306

> pid-file = /opt/data/mysql.pid

> user = mysql

> skip-name-resolve

> EOF

[root@yxr ~]# cat /etc/my.cnf 'mysql的配置文件为/etc/my.cnf'

[mysqld]

basedir = /usr/local/mysql '指定Mysql的安装路径'

datadir = /opt/data '指定mysql的数据存放路径'

socket = /tmp/mysql.sock '指定套接字文件位置'

port = 3306 '设置监听端口'

pid-file = /opt/data/mysql.pid '指定进程ID文件存放路径'

user = mysql '指定mysql以什么用户的身份提供服务'

skip-name-resolve '禁止mysql对外部连接进行DNS解析,使用这一选项可以消除mysql进行DNS解析的时间。若开启该选项,则所有远程主机连接授权都要使用IP地址方式否则mysql将无法正常处理连接请求'

'配置服务启动脚本'

[root@yxr ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

[root@yxr ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld

[root@yxr ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld

'启动mysql'

[root@yxr ~]# /etc/init.d/mysqld start

Starting MySQL.Logging to '/opt/data/yxr.err'.

. SUCCESS!

[root@yxr ~]# ps -ef|grep mysql

root 15787 1 0 17:57 pts/2 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid

mysql 15965 15787 4 17:57 pts/2 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=yxr.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306

root 15997 1669 0 17:57 pts/2 00:00:00 grep --color=auto mysql

[root@yxr ~]# ss -antl

State Recv-Q Send-Q Local Address:Port Peer Address:Port

LISTEN 0 128 *:22 *:*

LISTEN 0 100 127.0.0.1:25 *:*

LISTEN 0 128 :::22 :::*

'修改密码,先使用临时密码登录'

[root@yxr ~]# /usr/local/mysql/bin/mysql -uroot

-p

ERROR 1045 (28000): Access denied for user 'root-p'@'localhost' (using password: NO)

[root@yxr ~]# /usr/local/mysql/bin/mysql -uroot

Enter password:

Welcome to the MySQL monitor. Commands end with

Your MySQL connection id is 3

Server version: 5.7.22

Copyright (c) 2000, 2018, Oracle and/or its affi

Oracle is a registered trademark of Oracle Corpo

affiliates. Other names may be trademarks of the

owners.

Type 'help;' or '\h' for help. Type '\c' to clea

'设置新密码'

mysql> set password = password('yaoxiaorong!');

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>

创建一个以你名字为名的数据库,并创建一张表student,该表包含三个字段(id,name,age)

'进入mysql'

[root@yxr ~]# /usr/local/mysql/bin/mysql -uroot -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 7

Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| sys |

+--------------------+

4 rows in set (0.00 sec)

'创建数据库yaoxiaorong'

mysql> create database yaoxiaorong;

Query OK, 1 row affected (0.00 sec)

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| sys |

| yaoxiaorong |

+--------------------+

5 rows in set (0.00 sec)

'进入yaoxiaorong数据库'

mysql> use yaoxiaorong;

Database changed

'在数据库yaoxiaorong里创建表student'

mysql> create table student(id int not null,name varchar(100) not null,age tinyint);

Query OK, 0 rows affected (0.03 sec)

'在数据库yaoxiaorong里创建表Rudy'

mysql> create table Ruby(id int not null,name varcharchar(100) not null,age tinyint);

Query OK, 0 rows affected (0.03 sec)

'查看当前数据库有哪些表'

mysql> show tables;

+-----------------------+

| Tables_in_yaoxiaorong |

+-----------------------+

| Ruby |

| student |

+-----------------------+

2 rows in set (0.00 sec)

'查看表结构'

mysql> desc yaoxiaorong.student;

+-------+--------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+--------------+------+-----+---------+-------+

| id | int(11) | NO | | NULL | |

| name | varchar(100) | NO | | NULL | |

| age | tinyint(4) | YES | | NULL | |

+-------+--------------+------+-----+---------+-------+

3 rows in set (0.05 sec)

往新建的student表中插入数据(用insert语句)

mysql> INSERT INTO student(id,name,age) VALUE (1,'tom',20);

Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO student (id,name,age) VALUE(2,'jerry',23),(3,'wangqing',25),(4,'sean',28),(5,'zhangshan',26),(6,'zhangshan',20),(7,'lisi',NULL),(8,'chenshou',10),(9,'wangwu',3),(10,'qiuyi',15),(11,'qiuxiaotian',20);

Query OK, 10 rows affected (0.00 sec)

Records: 10 Duplicates: 0 Warnings: 0

mysql> select * from student;

+----+-------------+------+

| id | name | age |

+----+-------------+------+

| 1 | tom | 20 |

| 2 | jerry | 23 |

| 3 | wangqing | 25 |

| 4 | sean | 28 |

| 5 | zhangshan | 26 |

| 6 | zhangshan | 20 |

| 7 | lisi | NULL |

| 8 | chenshou | 10 |

| 9 | wangwu | 3 |

| 10 | qiuyi | 15 |

| 11 | qiuxiaotian | 20 |

+----+-------------+------+

11 rows in set (0.01 sec)

'修改lisi的年龄为50'

mysql> update student set age = 50 where name = 'lisi';

Query OK, 1 row affected (0.01 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from student where name = 'lisi';

+----+------+------+

| id | name | age |

+----+------+------+

| 7 | lisi | 50 |

+----+------+------+

1 row in set (0.00 sec)

以age字段降序排序

mysql> select * from student order by age desc;

+----+-------------+------+

| id | name | age |

+----+-------------+------+

| 7 | lisi | 50 |

| 4 | sean | 28 |

| 5 | zhangshan | 26 |

| 3 | wangqing | 25 |

| 2 | jerry | 23 |

| 1 | tom | 20 |

| 6 | zhangshan | 20 |

| 11 | qiuxiaotian | 20 |

| 10 | qiuyi | 15 |

| 8 | chenshou | 10 |

| 9 | wangwu | 3 |

+----+-------------+------+

11 rows in set (0.01 sec)

备份整个数据库(全备)

[root@yxr ~]# mysqldump -uroot -p --all-databases > all-201808191905.sql

Enter password:

[root@yxr ~]# ls

all-201808191905.sql anaconda-ks.cfg

备份yaoxiaorong库的student表和Ruby表

[root@yxr ~]# mysqldump -uroot -p yaoxiaorong student Ruby > table-201808191909.sql

Enter password:

[root@yxr ~]# ls

all-201808191905.sql table-201808191909.sql

anaconda-ks.cfg

备份yaoxiaorong库

[root@yxr ~]# mysqldump -uroot -p --databases yaoxiaorong > yxr-201808191914.sql

Enter password:

[root@yxr ~]# ls

all-201808191905.sql table-201808191909.sql

anaconda-ks.cfg yxr-201808191914.sql

模拟误删yaoxiaorong数据库

[root@yxr ~]# /usr/local/mysql/bin/mysql -uroot -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 14

Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| sys |

| yaoxiaorong |

+--------------------+

5 rows in set (0.01 sec)

mysql> drop database yaoxiaorong;

Query OK, 2 rows affected (0.04 sec)

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| sys |

+--------------------+

4 rows in set (0.00 sec)

恢复yaoxiaorong库

[root@yxr ~]# mysql -uroot -p < all-201808191905

Enter password:

[root@yxr ~]# mysql -uroot -p -e'show databases;'

Enter password:

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| sys |

| yaoxiaorong |

+--------------------+

'恢复yaoxiaorong数据库的student表和Ruby表'

[root@yxr ~]# mysql -uroot -p ;

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 18

Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| sys |

| yaoxiaorong |

+--------------------+

5 rows in set (0.01 sec)

mysql> use yaoxiaorong;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> source table-201808191909.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.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.05 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, 11 rows affected (0.01 sec)

Records: 11 Duplicates: 0 Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 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.02 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.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> show tables;

+-----------------------+

| Tables_in_yaoxiaorong |

+-----------------------+

| Ruby |

| student |

+-----------------------+

2 rows in set (0.00 sec)

模拟删除整个数据库

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| sys |

| yaoxiaorong |

+--------------------+

5 rows in set (0.01 sec)

mysql> drop database yaoxiaorong;

Query OK, 2 rows affected (0.01 sec)

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| sys |

+--------------------+

4 rows in set (0.00 sec)

mysql> exit

Bye

'恢复整个数据库'

[root@yxr ~]# ls

all-201808191905.sql table-201808191909.sql

anaconda-ks.cfg yxr-201808191914.sql

[root@yxr ~]# mysql -uroot -p

Enter password:

[root@yxr ~]# mysql -uroot -p -e'show databases;'

Enter password:

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| sys |

| yaoxiaorong |

+--------------------+

mysql备份与恢复方案_MYSQL备份与恢复相关推荐

  1. 13.mysql架构方案_MYSQL常见架构

    MYSQL常见架构 MYSQL常见架构 数据库如果只用一个的话,万一出现故障,会影响网页的正常访问.为解决安全问题,我们为服务器搭建集群,保证一台机器挂了,会有另一台站起来.除此之外,如果我们部署一台 ...

  2. mysql配置方案_MySQL的安装与配置

    对于一个纯小白来说,安装一个MySQL不是那么容易,本来是按照老师给的步骤,一步一步进行应该不会出现什么错误的,但由于个人电脑内部的配置问题,在安装过程中不断出问题,我觉得更重要的原因应该在于我,我作 ...

  3. mysql sharding 方案_mysql sharding 方案 分库分表(sharding)系列(4)

    图1. Sharding实现层面与相关框架/产品 在DAO层实现 当团队决定自行实现sharding的时候,DAO层可能是嵌入sharding逻辑的首选位置,因为在这个层面上,每一个DAO的方法都明确 ...

  4. MYSQL集群的备份与恢复最终版本

    [原创]MYSQL集群的备份与恢复最终版本 中午刚刚弄明白了MYSQL集群的备份与恢复. 写下来,以后就不用为这个问题浪费时间了. 1.在管理节点上进行备份. ndb_mgm> start ba ...

  5. MySQL日志管理、备份与恢复

    MySQL日志管理.备份与恢复 前言 一.MySQL日志管理 (1)日志分类 ①错误日志 ②通用查询日志 ③二进制日志 ④慢查询日志 ⑤中继日志 (2)日志配置 (3)日志查询 ①查询通用日志是否开启 ...

  6. oracle8i数据库修复,Oracle8i数据库应用系统的备份与恢复方案的选择

    Oracle8i数据库应用系统的备份与恢复方案的选择 刘长生;孟松 [期刊名称]<电脑知识与技术> [年(卷),期]2004(000)007 [摘要]本文介绍了数据库联机备份及备份的内容. ...

  7. mysql主从整套方案_Mysql主从方案

    Mysql主从方案 序言 Mysql数据库可以通过源码编译安装,也可以通过RPM或yum安装,安装方法自选,但是我推荐用源码编译安装. 数据库安装 mysql主从复制中: 第一步:master记录二进 ...

  8. mysql产品优化方案_mysql的优化方案

    简介 在本文中,主要写一下自己所查阅和理解的mysql优化方案. 我的理解是数据库的优化对于我们'非专业'人员,mysql的优化也没那么复杂了,真的要玩转mysql的话,肯定得需要很多年的经验了. 参 ...

  9. mysql知识结构图_MySql知识结构说明

    MySQL发展历史     MySQL历史背景 MySQL优势特性 数据库基础        数据库基础概念     数据库基础概念 数据库模型   层次模型 网状模型 关系模型 磁盘存储 常见数据库 ...

  10. mysql 迁移方案_不同场景下 MySQL 的迁移方案

    本文内容 为什么要迁移 MySQL 迁移方案概览 MySQL 迁移实战 注意事项 技巧 总结 一.为什么要迁移 MySQL 迁移是 DBA 日常维护中的一个工作.迁移,是把实际存在的物体挪走,保证该物 ...

最新文章

  1. springboot项目实例_Springboot项目的接口防刷(实例)
  2. QT安装由问题的,安装后发现有些控件标签名显示不了
  3. #23 Counter Cache Column
  4. WriteStrToReg() ReadStrFormReg()
  5. Lesson 8.5 SOFTMAX回归
  6. matlab练习程序(LMS)
  7. 结构专业规范大全_2019年一、二级注册结构师专业考试所用的规范、标准、规程...
  8. 10.python网络编程(socket server 实现并发 part 2)
  9. 动态区间第k小:树状数组套权值线段树
  10. Yii2语言国际化配置
  11. ubuntu下安装及配置git的方法(最全超详细教程github)
  12. java抓取网页数据_简易数据分析 10 | Web Scraper 翻页——抓取滚动加载类型网页...
  13. android pdf 乱码怎么解决方案,pdf文件打开是乱码怎么办?pdf字体乱码的解决办法...
  14. JS 截取视频某一帧图片 实现视频截图
  15. 微信好友只有昵称没有微信号_只知道昵称怎么查他的微信号
  16. 现代计算机eniac的诞生,eniac诞生于哪一年(第一台电脑eniac诞生在哪国)
  17. PTA-520钻石争霸赛题解【c/c++ 完整思路+代码】
  18. 为ThinkCentre M910t-NO76重装Windows7(64位)系统
  19. h5邮件的邮箱 支持_Coremail发布H5邮箱移动模板新版本,多项新功能让办公更便捷...
  20. 线性代数 --- 什么是高斯消元法,什么又是高斯-若尔当消元法?

热门文章

  1. 5e检索服务器信息,5Eplay易玩网服务器出租常见问题解答(FAQ)
  2. android基带版本,Android - 基带版本为未知时自动隐藏
  3. html中鼠标点击图片变动,JS实现页面鼠标点击出现图片特效
  4. css3动画 --- transform属性值:旋转rotate、扭曲skew、缩放scale和移动translate以及矩阵变形matrix
  5. 百度地图图层开发java_Android百度地图应用之图层展示
  6. 微分中值定理——(罗尔定理、拉格朗日定理、导数极限定理、达布定理、柯西定理)
  7. CSS峰会圆桌论道丨共享产业数字化升级中的安全探索
  8. 0x00000050蓝屏(0x00000050蓝屏 xp)
  9. llama是什么动物_羊驼(Alpaca)与骆马(Llama)
  10. Processing绘制随风飘扬的名画