写在前面
最近有个学弟问我这样的问题,他的hive在mysql中映射的元数据丢了,导致其元数据服务起不来,所以写这篇文章介绍mysql数据常见的备份方式。
一、数据备份
1、mysqdump
1.备份单个数据库
#备份一个数据库
mysqldump -u username -p password dbname tbname >bakfile.sql
#例程
mysqldump -u root -pok hive312 >~/bak/hive312.sql
#备份多个数据库
mysqldump -u username -P --databases dbname1 dbname2 ... > filename.sql
#例程
mysqldump -u root -p ok --databases test mysql>~\testandmysql.sql
#备份所有数据库
mysqldump -u username -P --all-databases>filename.sql
#例程
mysqldump -u root -p --all-databases >~\all.sql
4.数据恢复
#数据恢复
mysql -u username -P [dbname] < filename.sql
#例程
mysql -u root -p < ~\all.sql
#导出数据
SELECT 列名 FROM table [WHERE 语句] INTO OUTFILE '目标文件'[OPTIONS]
#例程
SELECT * FROM yest.student INTO OUTFILE '~/student.txt';
#文件导入
LOAD DATA INFILE '~/file.txt'
INTO TABLE test_db.tb_student_copy
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '?';
- 5
- 6
3、binlog日志
1.查看binlog日志
#查看binlog日志
show variables like '%log_bin%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| log_bin | OFF |
| log_bin_basename | |
| log_bin_index | |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+-------+
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
2.开始binlog日志
#查找my.cnf文件
find / -name my.cnf
#编辑my.cnf文件
vim my.cnf
#输入以下内容
#-------------------
[mysqld]
# 开启binlog
log-bin = mysql-bin
#------------------
#重启mysql服务
systemctl restart mysql
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
3.查看binlog日志
#登录mysql,查看日志是否开启
how variables like '%log_bin%';
+---------------------------------+--------------------------------+
| Variable_name | Value |
+---------------------------------+--------------------------------+
| log_bin | ON |
| log_bin_basename | /var/lib/mysql/mysql-bin |
| log_bin_index | /var/lib/mysql/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+--------------------------------+
#查看所有binlog日志文件列表
show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 120 |
+------------------+-----------+
#查看最后一个binlog日志的编号名称及其最后一个操作事件pos结束点的
show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27