MYSQL备份与恢复

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQL备份与恢复相关的知识,希望对你有一定的参考价值。

数据库常用备份方案

1.全量备份
是指对某一个时间点上的所有数据或应用进行的一个完全拷贝
优点:数据恢复快
缺点:备份时间长

2.增量备份
是指在一次全备份或上一次增量备份后,以后每次的备份只需备份与前一次相比增加和或者被删除的文件。这就意味着,第一次增量备份的对象是进行全备后所产生的增加和修改的文件;第二次增量备份的对象是进行第一次增量备份后所产生的增加和修改的文件。
优点:没有重复的备份数据;备份时间短;
缺点:恢复数据时必须按一定的顺序进行

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

二进制格式安装mysql

‘提前将mysql软件包下载到本地:
 https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
‘
[[email protected] ~]# cd /usr/src/
[[email protected] src]# ls
debug  kernels

通过xftp传到/usr/src
如图1:
技术分享图片
如图2:
技术分享图片

‘创建用户和组‘
[[email protected] src]# groupadd -r mysql
[[email protected] src]# useradd -M -s /sbin/nologin -g mysql mysql
‘解压软件至/usr/local‘
[[email protected] src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[[email protected] src]# ls /usr/local/
bin      libexec
etc      mysql-5.7.22-linux-glibc2.12-x86_64
games    sbin
include  share
lib      src
lib64
‘将解压的软件成一个软链接‘

[[email protected] src]# cd /usr/local/
[[email protected] local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.22-linux-glibc2.12-x86_64/’
[[email protected] 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的属主属组‘
[[email protected] local]# chown -R mysql.mysql /usr/local/mysql
[[email protected] 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/
‘添加环境变量‘
[[email protected] local]# echo ‘export PATH=/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh
[[email protected] local]# cd
[[email protected] ~]# . /etc/profile.d/mysql.sh 
[[email protected] ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
‘建立数据存放目录‘
[[email protected] ~]# mkdir /opt/data
[[email protected] ~]# chown -R mysql.mysql /opt/data/
[[email protected] ~]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Aug 19 17:34 data
‘初始化数据库‘
[[email protected] ~]# /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 ‘[email protected]: gp0Ct)0Rkygg这是密码,随机密码‘
‘生成配置文件‘
[[email protected] ~]# cat > /etc/my.cnf <<EOF
> [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
[[email protected] ~]# 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将无法正常处理连接请求‘
‘配置服务启动脚本‘
[[email protected] ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[[email protected] ~]# sed -ri ‘s#^(basedir=).*#1/usr/local/mysql#g‘ /etc/init.d/mysqld 
[[email protected] ~]# sed -ri ‘s#^(datadir=).*#1/opt/data#g‘ /etc/init.d/mysqld 
‘启动mysql‘
[[email protected] ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to ‘/opt/data/yxr.err‘.
. SUCCESS! 
[[email protected] ~]# 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
[[email protected] ~]# 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                 :::*                  
‘修改密码,先使用临时密码登录‘
[[email protected] ~]# /usr/local/mysql/bin/mysql -uroot
-p
ERROR 1045 (28000): Access denied for user ‘root-p‘@‘localhost‘ (using password: NO)
[[email protected] ~]# /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‘

[[email protected] ~]# /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)

备份整个数据库(全备)

[[email protected] ~]# mysqldump -uroot -p --all-databases > all-201808191905.sql
Enter password: 
[[email protected] ~]# ls
all-201808191905.sql  anaconda-ks.cfg

备份yaoxiaorong库的student表和Ruby表

[[email protected] ~]# mysqldump -uroot -p yaoxiaorong student Ruby > table-201808191909.sql
Enter password: 
[[email protected] ~]# ls
all-201808191905.sql  table-201808191909.sql
anaconda-ks.cfg

备份yaoxiaorong库

[[email protected] ~]# mysqldump -uroot -p --databases yaoxiaorong > yxr-201808191914.sql
Enter password: 
[[email protected] ~]# ls
all-201808191905.sql  table-201808191909.sql
anaconda-ks.cfg       yxr-201808191914.sql

模拟误删yaoxiaorong数据库

[[email protected] ~]# /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库

[[email protected] ~]# mysql -uroot -p < all-201808191905
Enter password: 
[[email protected] ~]# mysql -uroot -p -e‘show databases;‘
Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| yaoxiaorong        |
+--------------------+
‘恢复yaoxiaorong数据库的student表和Ruby表‘
[[email protected] ~]# 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
‘恢复整个数据库‘
[[email protected] ~]# ls
all-201808191905.sql  table-201808191909.sql
anaconda-ks.cfg       yxr-201808191914.sql
[[email protected] ~]# mysql -uroot -p <all-201808191905.sql 
Enter password: 
[[email protected] ~]# mysql -uroot -p -e‘show databases;‘
Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| yaoxiaorong        |
+--------------------+

以上是关于MYSQL备份与恢复的主要内容,如果未能解决你的问题,请参考以下文章

详解Mysql自动备份与恢复的几种方法(图文教

MySQL备份与恢复

Mysql备份与恢复

MySQL备份与恢复

MySQL备份与恢复

Mysql 备份恢复与 xtrabackup备份