1. 物理冷备份与恢复
[root@localhost ~]# systemctl stop mysqld ##关闭数据库
[root@localhost ~]# mkdir /backup ##在根下新建backup目录
[root@localhost ~]# tar zcf /backup/mysql_all-$(date +%F).tar.gz /usr/local/mysql/data/ ##/usr/local/mysql/data备份压缩
[root@localhost ~]# cd /backup/ ##进入/backup/
[root@localhost backup]# ll ##查看备份
total 1384
-rw-r--r-- 1 root root 1414007 Sep 13 15:57 mysql_all-2020-09-13.tar.gz
##模拟故障##
[root@localhost backup]# mkdir /bak ##在根下新建bak目录
[root@localhost backup]# mv /usr/local/mysql/data/ /bak/ ##将目录data数据移动到bak 里面去
[root@localhost backup]# cd /usr/local/mysql/ 进mysql目录查看data
[root@localhost mysql]# ll ##查看后data目录被移走 ,模拟故障成功
total 64
drwxr-xr-x 2 mysql mysql 4096 Sep 7 16:15 bin
-rw-r--r-- 1 mysql mysql 17987 Sep 13 2017 COPYING
-rw-r--r-- 1 mysql mysql 17987 Sep 13 2017 COPYING-test
drwxr-xr-x 2 mysql mysql 55 Sep 7 16:15 docs
drwxr-xr-x 3 mysql mysql 4096 Sep 7 16:15 include
drwxr-xr-x 4 mysql mysql 191 Sep 7 16:15 lib
drwxr-xr-x 4 mysql mysql 30 Sep 7 16:15 man
drwxr-xr-x 10 mysql mysql 4096 Sep 7 16:15 mysql-test
-rw-r--r-- 1 mysql mysql 2478 Sep 13 2017 README
-rw-r--r-- 1 mysql mysql 2478 Sep 13 2017 README-test
drwxr-xr-x 28 mysql mysql 4096 Sep 7 16:15 share
drwxr-xr-x 2 mysql mysql 90 Sep 7 16:15 support-files
drwxr-xr-x 3 mysql mysql 17 Sep 7 16:15 usr
[root@localhost mysql]# mkdir /restore ##根目录新建restore 目录
[root@localhost mysql]# tar xzvf /backup/mysql_all-2020-09-13.tar.gz -C /restore/ ##将备份数据库解压到 /restore目录下
[root@localhost mysql]# mv /restore/usr/local/mysql/data/ /usr/local/mysql/ ##将备份数据移动到到/usr/local/mysql中
[root@localhost mysql]# systemctl start mysqld.service ##启动mysql数据库
[root@localhost mysql]# systemctl status mysqld.service ##查看mysql数据库启动状态 ,日志文件显示正常。
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2020-09-13 16:04:27 CST; 5s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 50222 ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 50201 ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 50225 (mysqld)
Tasks: 27
CGroup: /system.slice/mysqld.service
└─50225 /usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/mysql...
Sep 13 16:04:27 localhost.localdomain mysqld[50222]: 2020-09-13T08:04:27.643615Z 0 [Note] ...e.
Sep 13 16:04:27 localhost.localdomain mysqld[50222]: 2020-09-13T08:04:27.643628Z 0 [Note] ...';
Sep 13 16:04:27 localhost.localdomain mysqld[50222]: 2020-09-13T08:04:27.643639Z 0 [Note] ...'.
Sep 13 16:04:27 localhost.localdomain mysqld[50222]: 2020-09-13T08:04:27.653986Z 0 [Note] ...ts
Sep 13 16:04:27 localhost.localdomain mysqld[50222]: 2020-09-13T08:04:27.655178Z 0 [Note] ...s.
Sep 13 16:04:27 localhost.localdomain mysqld[50222]: Version: '5.7.20' socket: '/usr/loca...on
Sep 13 16:04:27 localhost.localdomain mysqld[50222]: 2020-09-13T08:04:27.655204Z 0 [Note] ...k.
Sep 13 16:04:27 localhost.localdomain mysqld[50222]: 2020-09-13T08:04:27.655208Z 0 [Note] ...es
Sep 13 16:04:27 localhost.localdomain mysqld[50222]: 2020-09-13T08:04:27.681836Z 0 [Note] ...es
Sep 13 16:04:27 localhost.localdomain systemd[1]: Started MySQL Server.
Hint: Some lines were ellipsized, use -l to show in full.
2. mysqldump备份与恢复
##备份恢复数据表##
[root@localhost mysql]# mysql -uroot -p ##登录数据库
Enter password:
mysql> show databases; ##查看数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbs |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use mysql;
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> show tables; ##查看数据表
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
31 rows in set (0.00 sec)
mysql> exit ##退出数据库
Bye
[root@localhost mysql]# mysqldump -u root -p mysql user > mysql-user.sql ##备份数据路mysql中的user表 ,文件保存在当前操作目录下面
Enter password:
[root@localhost mysql]# grep -v "^--" mysql-user.sql | grep -v "^/" | grep -v "^$" ##查看备份文件
[root@localhost mysql]# mysql -uroot -p
Enter password:
mysql> create database text; ##新建数据库text
Query OK, 1 row affected (0.00 sec)
mysql> exit
Bye
[root@localhost mysql]# mysql -u root -p text < mysql-user.sql ##导入数据库
Enter password:
[root@localhost mysql]# mysql -u root -p -e 'show tables from text' ##验证输出结果
Enter password:
+----------------+
| Tables_in_text |
+----------------+
| user |
+----------------+
##备份恢复数据库##
[root@localhost mysql]# mysqldump -u root -p --databases bbs >bbs.sql ##备份bbs数据库
Enter password:
[root@localhost mysql]# mysql -u root -p -e 'drop database bbs' ##模拟故障删除bbs数据库
Enter password:
[root@localhost mysql]# mysql -u root -p -e 'show databases' ##查看databases所有数据库有没有bbs,发现没有了
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| text |
+--------------------+
[root@localhost mysql]# mysql -u root -p < bbs.sql ##导入数据库bbs
Enter password:
[root@localhost mysql]# mysql -u root -p -e 'show databases' ##查看databases所有数据库有没有auth,发现有了
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbs |
| mysql |
| performance_schema |
| sys |
| text |
+--------------------+
[root@localhost mysql]# vi /etc/my.cnf
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
log-bin=/usr/local/mysql/data/mysql-bin ##这个地方添加命令,开启二进制日志功能
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
[root@localhost mysql]# systemctl restart mysqld ##重启数据库
[root@localhost mysql]# ll /usr/local/mysql/data/
total 122924
-rw-r----- 1 mysql mysql 56 Sep 7 16:16 auto.cnf
drwxr-x--- 2 mysql mysql 20 Sep 13 16:24 bbs
-rw-r----- 1 mysql mysql 329 Sep 13 16:28 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 Sep 13 16:28 ibdata1
-rw-r----- 1 mysql mysql 50331648 Sep 13 16:28 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Sep 7 16:16 ib_logfile1
-rw-r----- 1 mysql mysql 12582912 Sep 13 16:28 ibtmp1
drwxr-x--- 2 mysql mysql 4096 Sep 7 16:16 mysql
-rw-r----- 1 mysql mysql 154 Sep 13 16:28 mysql-bin.000001 ##二进制日志文件
-rw-r----- 1 mysql mysql 39 Sep 13 16:28 mysql-bin.index
drwxr-x--- 2 mysql mysql 8192 Sep 7 16:16 performance_schema
drwxr-x--- 2 mysql mysql 8192 Sep 7 16:16 sys
drwxr-x--- 2 mysql mysql 68 Sep 13 16:16 text
[root@localhost mysql]# mysql -uroot -p
Enter password:
mysql> create database client; ##创建数据库client
Query OK, 1 row affected (0.01 sec)
mysql> use client; ##进入数据库
Database changed
mysql> create table user_info(身份证 char(20) not null,姓名 char(20) not null,姓别 char(4),用户ID号 char(10) not null,资费 int(10)); ##创建表结构
Query OK, 0 rows affected (0.00 sec)
mysql> insert into user_info values('000006','张三','男','016','10'); ##插入数据
Query OK, 1 row affected (0.02 sec)
mysql> insert into user_info values('000007','李四','女','017','91');
Query OK, 1 row affected (0.00 sec)
mysql> insert into user_info values('000008','王五','女','018','23');
Query OK, 1 row affected (0.00 sec)
mysql> select * from user_info; ##查看表内容
+-----------+--------+--------+-------------+--------+
| 身份证 | 姓名 | 姓别 | 用户ID号 | 资费 |
+-----------+--------+--------+-------------+--------+
| 000006 | 张三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
+-----------+--------+--------+-------------+--------+
3 rows in set (0.00 sec)
mysql> flush tables with read lock; ##备份前需要将数据库加读锁,防止数据在备份时写入
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@localhost ~]# mkdir /mysql_bak ##创建/mysql_bak 目录
[root@localhost ~]# mysqldump -u root -p client user_info > /mysql_bak/client_userinfo-$(date +%F).sql ##完全备份
Enter password:
[root@localhost ~]# ll /mysql_bak/
total 4
-rw-r--r-- 1 root root 2055 Sep 13 16:37 client_userinfo-2020-09-13.sql
[root@localhost ~]# mysqladmin -u root -p flush-logs
Enter password:
[root@localhost ~]# ll /usr/local/mysql/data/
total 122928
-rw-r----- 1 mysql mysql 56 Sep 7 16:16 auto.cnf
drwxr-x--- 2 mysql mysql 20 Sep 13 16:24 bbs
drwxr-x--- 2 mysql mysql 62 Sep 13 16:33 client
-rw-r----- 1 mysql mysql 329 Sep 13 16:28 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 Sep 13 16:33 ibdata1
-rw-r----- 1 mysql mysql 50331648 Sep 13 16:33 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Sep 7 16:16 ib_logfile1
-rw-r----- 1 mysql mysql 12582912 Sep 13 16:37 ibtmp1
drwxr-x--- 2 mysql mysql 4096 Sep 7 16:16 mysql
-rw-r----- 1 mysql mysql 1539 Sep 13 16:39 mysql-bin.000001
-rw-r----- 1 mysql mysql 154 Sep 13 16:39 mysql-bin.000002
-rw-r----- 1 mysql mysql 78 Sep 13 16:39 mysql-bin.index
drwxr-x--- 2 mysql mysql 8192 Sep 7 16:16 performance_schema
drwxr-x--- 2 mysql mysql 8192 Sep 7 16:16 sys
drwxr-x--- 2 mysql mysql 68 Sep 13 16:16 text
##继续录入新的内容并进行增量备份##
[root@localhost ~]# mysql -uroot -p
Enter password:
mysql> unlock tables; ##解锁表锁
Query OK, 0 rows affected (0.00 sec)
mysql> use client;
mysql> insert into user_info values('000009','赵六','男','019','37');
Query OK, 1 row affected (0.00 sec)
mysql> insert into user_info values('000010','孙七','男','020','36');
Query OK, 1 row affected (0.00 sec)
mysql> select * from user_info;
+-----------+--------+--------+-------------+--------+
| 身份证 | 姓名 | 姓别 | 用户ID号 | 资费 |
+-----------+--------+--------+-------------+--------+
| 000006 | 张三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
| 000009 | 赵六 | 男 | 019 | 37 |
| 000010 | 孙七 | 男 | 020 | 36 |
+-----------+--------+--------+-------------+--------+
5 rows in set (0.00 sec)
mysql> exit
Bye
[root@localhost ~]# mysqladmin -u root -p flush-logs
Enter password:
[root@localhost ~]# ll /usr/local/mysql/data/
total 122932
-rw-r----- 1 mysql mysql 56 Sep 7 16:16 auto.cnf
drwxr-x--- 2 mysql mysql 20 Sep 13 16:24 bbs
drwxr-x--- 2 mysql mysql 62 Sep 13 16:33 client
-rw-r----- 1 mysql mysql 329 Sep 13 16:28 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 Sep 13 16:42 ibdata1
-rw-r----- 1 mysql mysql 50331648 Sep 13 16:42 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Sep 7 16:16 ib_logfile1
-rw-r----- 1 mysql mysql 12582912 Sep 13 16:37 ibtmp1
drwxr-x--- 2 mysql mysql 4096 Sep 7 16:16 mysql
-rw-r----- 1 mysql mysql 1539 Sep 13 16:39 mysql-bin.000001
-rw-r----- 1 mysql mysql 797 Sep 13 16:43 mysql-bin.000002
-rw-r----- 1 mysql mysql 154 Sep 13 16:43 mysql-bin.000003
-rw-r----- 1 mysql mysql 117 Sep 13 16:43 mysql-bin.index
drwxr-x--- 2 mysql mysql 8192 Sep 7 16:16 performance_schema
drwxr-x--- 2 mysql mysql 8192 Sep 7 16:16 sys
drwxr-x--- 2 mysql mysql 68 Sep 13 16:16 text
[root@localhost ~]# cp /usr/local/mysql/data/mysql-bin.000002 /mysql_bak/
[root@localhost ~]# mysql -u root -p -e 'drop table client.user_info' ##模拟误操作删除 user_info表
Enter password:
[root@localhost ~]# mysql -u root -p -e 'select * from client.user_info' ##查看数据表,发现不存在,删除成功
Enter password:
ERROR 1146 (42S02) at line 1: Table 'client.user_info' doesn't exist
[root@localhost ~]# mysql -u root -p client < /mysql_bak/client_userinfo-2020-09-13.sql ##数据恢复
Enter password:
[root@localhost ~]# mysql -u root -p -e 'select * from client.user_info' ##查看恢复的数据表内容
Enter password:
+-----------+--------+--------+-------------+--------+
| 身份证 | 姓名 | 姓别 | 用户ID号 | 资费 |
+-----------+--------+--------+-------------+--------+
| 000006 | 张三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
+-----------+--------+--------+-------------+--------+
[root@localhost ~]# mysqlbinlog --no-defaults /mysql_bak/mysql-bin.000002 | mysql -u root -p
Enter password:
[root@localhost ~]# mysql -u root -p -e 'select * from client.user_info'
Enter password:
+-----------+--------+--------+-------------+--------+
| 身份证 | 姓名 | 姓别 | 用户ID号 | 资费 |
+-----------+--------+--------+-------------+--------+
| 000006 | 张三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
| 000009 | 赵六 | 男 | 019 | 37 |
| 000010 | 孙七 | 男 | 020 | 36 |
+-----------+--------+--------+-------------+--------+
补充:
锁定,解锁的两种方法
第一种
show processlist;
找到锁进程,kill id ;
第二种
mysql>UNLOCK TABLES;
###锁表###
锁定数据表,避免在备份过程中,表被更新
mysql>LOCK TABLES tbl_name READ;
为表增加一个写锁定:
mysql>LOCK TABLES tbl_name WRITE;
2. 基于位置恢复
##完全备份##
[root@localhost ~]# mysql -u root -p client < /mysql_bak/client_userinfo-2020-09-13.sql
Enter password:
[root@localhost ~]# mysql -u root -p -e'select * from client.user_info'
Enter password:
+-----------+--------+--------+-------------+--------+
| 身份证 | 姓名 | 姓别 | 用户ID号 | 资费 |
+-----------+--------+--------+-------------+--------+
| 000006 | 张三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
+-----------+--------+--------+-------------+--------+
[root@localhost ~]# mysqlbinlog --no-defaults --base64-output=decode-rows -v /mysql_bak/mysql-bin.000002
...... ##省略内容
[root@localhost ~]# mysqlbinlog --no-defaults --stop-position='517' /mysql_bak/mysql-bin.000002 |mysql -u root -p
Enter password:
[root@localhost ~]# mysql -u root -p -e'select * from client.user_info'
Enter password:
+-----------+--------+--------+-------------+--------+
| 身份证 | 姓名 | 姓别 | 用户ID号 | 资费 |
+-----------+--------+--------+-------------+--------+
| 000006 | 张三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
| 000009 | 赵六 | 男 | 019 | 37 |
+-----------+--------+--------+-------------+--------+
[root@localhost ~]# mysqlbinlog --no-defaults --start-position='517' /mysql_bak/mysql-bin.000002 |mysql -u root -p
Enter password:
[root@localhost ~]# mysql -u root -p -e'select * from client.user_info'
Enter password:
+-----------+--------+--------+-------------+--------+
| 身份证 | 姓名 | 姓别 | 用户ID号 | 资费 |
+-----------+--------+--------+-------------+--------+
| 000006 | 张三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
| 000009 | 赵六 | 男 | 019 | 37 |
| 000010 | 孙七 | 男 | 020 | 36 |
+-----------+--------+--------+-------------+--------+
说明:基于位置恢复的操作,节点不要选择太靠近操作的节点,否则容易恢复失败。
3. 基于时间点恢复
##选择这个时间段:200913 16:42:20##
[root@localhost ~]# mysqlbinlog --no-defaults --stop-datetime='2020-9-13 16:42:20' /mysql_bak/mysql-bin.000002 |mysql -u root -p
Enter password:
[root@localhost ~]# mysql -u root -p -e'select * from client.user_info'
Enter password:
+-----------+--------+--------+-------------+--------+
| 身份证 | 姓名 | 姓别 | 用户ID号 | 资费 |
+-----------+--------+--------+-------------+--------+
| 000006 | 张三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
| 000009 | 赵六 | 男 | 019 | 37 |
| 000010 | 孙七 | 男 | 020 | 36 |
| 000009 | 赵六 | 男 | 019 | 37 |
+-----------+--------+--------+-------------+--------+
[root@localhost ~]# mysqlbinlog --no-defaults --start-datetime='2020-9-13 16:42:20' /mysql_bak/mysql-bin.000002 |mysql -u root -p
Enter password:
[root@localhost ~]# mysql -u root -p -e'select * from client.user_info'
Enter password:
+-----------+--------+--------+-------------+--------+
| 身份证 | 姓名 | 姓别 | 用户ID号 | 资费 |
+-----------+--------+--------+-------------+--------+
| 000006 | 张三 | 男 | 016 | 10 |
| 000007 | 李四 | 女 | 017 | 91 |
| 000008 | 王五 | 女 | 018 | 23 |
| 000009 | 赵六 | 男 | 019 | 37 |
| 000010 | 孙七 | 男 | 020 | 36 |
| 000009 | 赵六 | 男 | 019 | 37 |
| 000010 | 孙七 | 男 | 020 | 36 |
+-----------+--------+--------+-------------+--------+