MYSQL備份與恢復

數據庫常用備份方案

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:
MYSQL備份與恢復
如圖2:
MYSQL備份與恢復

'創建用戶和組'
[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 <<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
[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 <all-201808191905.sql 
Enter password: 
[root@yxr ~]# mysql -uroot -p -e'show databases;'
Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| yaoxiaorong        |
+--------------------+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章