MySQL 数据库完全备份案例

MySQL完全备份案例

需求描述:

北京移电通信公司的用户信息数据库为client,用户资费数据表为user_info,表结构如下所示。请为该公司指定合理的备份策略,依据所指定的策略备份数据,模拟数据丢失进行数据恢复。

 

创建数据及表,录入数据:

[root@localhost ~]# mysql -uroot -p123456

mysql> create database client;

Query OK, 1 row affected (0.00 sec)

 

mysql> use client;

Database changed

mysql> show variables like 'character_set_%';//查看字符集是否支持中文

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

| Variable_name            | Value                            |

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

| character_set_client     | utf8                             |

| character_set_connection | utf8                             |

| character_set_database   | utf8                             |

| character_set_filesystem | binary                           |

| character_set_results    | utf8                             |

| character_set_server     | utf8                             |

| character_set_system     | utf8                             |

| character_sets_dir       | /usr/local/mysql/share/charsets/ |

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

8 rows in set (0.00 sec)

///////【1】create database client DEFAULT CHARACTER  SET utf-8;  //在创建库的时候声明库的字符集

【2】vim /etc/my.conf

在中间添加 

default-storage-engine=INNODB

character-set-server=utf8

collation-server=utf8_general_ci ////////这个定义是所有的库

[client]

default-character-set=utf8   ///////定义某个用户//////////////////////  然后重启mariadb

mysql> create table user_info(身份证 int(20),姓名 char(20),性别 char(2),用户ID int(110),资费 int(10));

Query OK, 0 rows affected (0.01 sec)

desc user_info   //查看描述信息

 

mysql> insert into user_info values('000000001','孙空武','','011','100');

Query OK, 1 row affected (0.01 sec) 

mysql> insert into user_info values('000000002','蓝凌','','012','98');

Query OK, 1 row affected (0.01 sec)

 

mysql> insert into user_info values('000000003','姜纹','','013','12');

Query OK, 1 row affected (0.00 sec)

 

mysql> insert into user_info values('000000004','关园','','014','38');

Query OK, 1 row affected (0.01 sec)

 

mysql> insert into user_info values('000000004','罗中昆','','015','39');

Query OK, 1 row affected (0.01 sec)

 

mysql> select * from user_info;

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

| 身份证    | 姓名      | 性别   | 用户ID    | 资费   |

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

|         1 | 孙空武    |      |          11 |    100 |

|         2 | 蓝凌      |      |          12 |     98 |

|         3 | 姜纹      |      |          13 |     12 |

|         4 | 关园      |      |          14 |     38 |

|         4 | 罗中昆    |      |          15 |     39 |

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

5 rows in set (0.00 sec)

 

完整备份client.user_info表:

[root@localhost ~]# mysqldump -uroot -p123456 client user_info > backup/client.user_info-$(date +%Y%m%d).sql

grep -Ev "^$|^/|^-" backup/client.user...sql //查看

模拟数据丢失恢复数据:

[root@localhost ~]# mysql -uroot -p123456 -e 'drop table client.user_info;'

[root@localhost ~]# mysql -uroot -p123456 -e 'use client; show tables;'

[root@localhost ~]# mysql -uroot -p123456 client < backup/client.user_info-20181214.sql

[root@localhost ~]# mysql -uroot -p123456 -e 'select * from client.user_info;'

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

| 身份证    | 姓名      | 性别   | 用户ID    | 资费   |

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

|         1 | 孙空武    |      |          11 |    100 |

|         2 | 蓝凌      |      |          12 |     98 |

|         3 | 姜纹      |      |          13 |     12 |

|         4 | 关园      |      |          14 |     38 |

|         4 | 罗中昆    |      |          15 |     39 |

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

 

定期备份数据:

[root@localhost ~]# which mysqldump

/usr/local/mysql/bin/mysqldump

 

[root@localhost ~]# vim /opt/bak_client.sh

#!/bin/bash

# 备份client.user_info 脚本

mysqldump -uroot -p123456 client user_info >backup/client.user_info-$(date +%Y%m%d).sql

 

[root@localhost ~]# chmod +x /opt/bak_client.sh

[root@localhost ~]# crontab -e

0       0       *       *       *       /opt/bak_client.sh//每天0:00备份

重启计划任务 systemtcl restart crontab

 

posted @ 2019-11-30 10:32  elin菻  阅读(562)  评论(0编辑  收藏  举报