一、创建数据库
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个数据库。
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。
二、进行数据备份
[root@localhost /]# mkdir mysqlback
[root@localhost /]# cp -a /var/lib/mysql/* /mysqlback ***//这里是重点,保留文件权限***
[root@localhost /]# cd mysqlback
[root@localhost 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
三、删除数据库,模拟数据丢失
[root@localhost mysqlback]# rm -rf /var/lib/mysql/*
四、数据还原并检查
[root@localhost mysqlback]# cp -a /mysqlback/* /var/lib/mysql/
[root@localhost mysqlback]# mysql -uroot
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib /mysql/mysql.sock' (111)
[root@localhost mysqlback]# systemctl restart mariadb
[root@localhost mysqlback]# mysql -uroot
最后可以看到yang1,yang2,yang3数据库被还原成功,2个表也成功还原。
扫码下方二维码关注公众号,获取更多学习资源及视频教程 (Linux\Python\Java和大数据开发)。
工众号回复【我要资源】免费获取。
联系邮箱:898109004@qq.com
Talk is cheap. Show me the code。————Linus Torvalds
**