①第一步创建数据库
使用cp指令备份MySQL数据库(1)
MariaDB [(none)]> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
±-------------------+
3 rows in set (0.01 sec)

MariaDB [(none)]> create database yang1;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> create database yang2;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> create database yang3;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| yang1 |
| yang2 |
| yang3 |
±-------------------+

可以看到上面创建了yang1,yang2,yang3这3个数据库。
使用cp指令备份MySQL数据库(1)
MariaDB [(none)]> use yang1
Database changed
MariaDB [yang1]> CREATE TABLE table_name (ID INT);
Query OK, 0 rows affected (0.22 sec)
MariaDB [yang1]> CREATE TABLE table_name2 (ID INT);
Query OK, 0 rows affected (0.03 sec)
这里给yang1这个数据库添加了两张表分别是table_name,table_name2。

②第二步进行数据备份
使用cp指令备份MySQL数据库(1)
[[email protected] /]# mkdir mysqlback
[[email protected] /]# cp -a /var/lib/mysql/* /mysqlback //这里是重点,保留文件权限
[[email protected] /]# cd mysqlback
[[email protected] mysqlback]# ll
总用量 28700
-rw-rw----. 1 mysql mysql 16384 3月 7 12:30 aria_log.00000001
-rw-rw----. 1 mysql mysql 52 3月 7 12:30 aria_log_control
-rw-rw----. 1 mysql mysql 18874368 3月 7 16:56 ibdata1
-rw-rw----. 1 mysql mysql 5242880 3月 7 16:56 ib_logfile0
-rw-rw----. 1 mysql mysql 5242880 3月 7 12:30 ib_logfile1
drwx------. 2 mysql mysql 4096 3月 7 15:57 mysql
srwxrwxrwx. 1 mysql mysql 0 3月 7 12:30 mysql.sock
drwx------. 2 mysql mysql 4096 3月 7 12:30 performance_schema
drwx------. 2 mysql mysql 65 3月 7 16:56 yang1
drwx------. 2 mysql mysql 20 3月 7 16:50 yang2
drwx------. 2 mysql mysql 20 3月 7 16:51 yang3

③第三部删除数据库,模拟数据丢失
使用cp指令备份MySQL数据库(1)
[[email protected] mysqlback]# rm -rf /var/lib/mysql/*

④第四部数据还原并检查
使用cp指令备份MySQL数据库(1)
[[email protected] mysqlback]# cp -a /mysqlback/* /var/lib/mysql/
[[email protected] mysqlback]# mysql -uroot
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib /mysql/mysql.sock’ (111)
[[email protected] mysqlback]# systemctl restart mariadb
[[email protected] mysqlback]# mysql -uroot

最后可以看到yang1,yang2,yang3数据库被还原成功,2个表也成功还原。

相关文章:

  • 2022-12-23
  • 2021-08-06
  • 2021-11-27
  • 2021-10-03
  • 2022-12-23
  • 2021-05-26
  • 2022-02-04
猜你喜欢
  • 2021-07-01
  • 2022-12-23
  • 2021-12-08
  • 2021-06-03
  • 2022-12-23
  • 2021-06-07
  • 2021-10-20
相关资源
相似解决方案