MySQL database security backup

Related Links:

The significance of data security backup

  1. In the event of an accident (hard disk damage, breakpoints, hacker attacks), to facilitate data recovery
  2. Export production data for R&D personnel or testers to test and learn
  3. High-level personnel's operation error caused data loss for recovery

Backup type

  • Full backup: backup of the entire database
  • Partial backup: Partial backup of data (one or more tables)
    • Incremental backup: Back up the changed data based on the previous backup
    • Differential backup: based on the first full backup to back up the changed data

Backup method

  • Logical backup: directly generate sql statement, execute sql statement when restoring data
  • Physical backup: copy relevant library files and perform data backup ( data storage directory pointed to by my.cnf )

the difference

  1. Logical backup efficiency is low, data recovery efficiency is low, and space saving
  2. Physical backup wastes space, and backup data is efficient

Backup scenario

  • Hot backup: during backup, it does not affect the read and write operations of the database
  • Warm backup: during backup, you can read but not write
  • Cold backup: When backing up, the mysql service is closed and no read and write operations can be performed

Mysqldump backup (cross-machine)

Single library syntax

备份基础语法:
mysqldump -u用户 -hip -p密码 数据库名 表名 | 压缩方式 > 绝对路径+文件名

Cross-machine backup

跨机器备份:

备份描述:mac本上安装了mysql数据库(172.20.10.2),使用自搭Linux(172.20.10.4)机器上的mysql备份mac本上的nba库,并使用压缩文件方式,备份至:/mysql_data_back下
来到linux的mysql安装目录(172.20.10.4):
创建目录:mkdir /mysql_data_back
切换:cd /usr/local/mysql/bin
备份:./mysqldump -uroot -proot -h172.20.10.2 nba | gzip > /mysql_data_back/nba.sql.gz

Local backup

本机备份:

备份描述:linux(自搭),备份本就上的db1库,并使用压缩方式,备份至:/mysql_data_back下
备份:./mysqldump -uroot -proot db1 | gzip > /mysql_data_back/db1.sql.gz

A table in the backup library

语法:./mysqldump -uroot -proot -h172.20.10.2 nba | gzip > /mysql_data_back/nba.sql.gz

备份描述:在Linux(自搭,172.20.10.4)上,远程备份mac本(172.20.10.2)nba(库)的nba_player(表)
在原来的基础上,nba(库名) 在追加表名即可
./mysqldump -uroot -proot -h172.20.10.2 nba nba_player | gzip > /mysql_data_back/nba-nba_player.sql.gz

Backup multiple libraries

语法:./mysqldump -u用户 -p密码 --databases 库1 库2 | gzip > 绝对路径+文件名
备份描述:备份本机的:db1、db2两个库
备份:./mysqldump -uroot -proot --databases db1 db2 | gzip > /mysql_data_back/db1-db2.sql.gz

note

Only the table structure is backed up, the data is not backed up!

Backup the entire library

Description: If there are many databases on the remote server, you can use full database backup

语法:
./mysqldump -uroot -proot -all --databases | gzip > /mysql_data_back/all.sql.gz

Mysql data recovery

For the backup data, do not add –databases, there is no database creation statement!

先备份数据:
./mysqldump -uroot -proot --databases db1 | gzip > /mysql_data_back/db1.sql.gz

删除库:
drop database db1;

还原数据:
2、解压gz文件:gunzip -d db1.sql.gz
1、登录数据库:mysql -uroot -proot -h 127.0.0.1 < /mysql_data_back/db1.sql

You can also specify the database after restoring the data

语法:
    mysql -u用户 -p密码 -h ip地址 数据库 < 绝对路径+文件名
示例:mysql -uroot -proot -h 127.0.0.1 issdb_1 < /mysql_data_back/issdb_1.sql

View Mysql database source files

method one

登录mysql:mysql -uroot -proot
查看:show variables like 'datadir%';
===========================
mysql> show variables like 'datadir%';
+---------------+------------------------+
| Variable_name | Value                  |
+---------------+------------------------+
| datadir       | /usr/local/mysql/data/ |
+---------------+------------------------+
1 row in set (0.01 sec)

Way two

Just view the my.cnf file

img

Guess you like

Origin blog.csdn.net/An1090239782/article/details/110800263