mySQL配置文件、备份与恢复
mysql配置文件
mysql的配置文件为/etc/my.cnf
配置文件查找次序:若在多个配置文件中均有设定,则最后找到的最终生效
/etc/my.cnf --> /etc/mysql/my.cnf --> --default-extra-file=/PATH/TO/CONF_FILE --> ~/.my.cnf
mysql常用配置文件参数:
| 参数 | 说明 |
|---|---|
| port = 3306 | 设置监听端口 |
| socket = /tmp/mysql.sock | 指定套接字文件位置 |
| basedir = /usr/local/mysql | 指定MySQL的安装路径 |
| datadir = /data/mysql | 指定MySQL的数据存放路径 |
| pid-file = /data/mysql/mysql.pid | 指定进程ID文件存放路径 |
| user = mysql | 指定MySQL以什么用户的身份提供服务 |
| skip-name-resolve | 禁止MySQL对外部连接进行DNS解析,使用这一选项可以消除MySQL进行DNS解析的时间。若开启该选项,则所有远程主机连接授权都要使用IP地址方式,否则MySQL将无法正常处理连接请求 |
mysql数据库备份
数据库常用备份方案
数据库备份方案:
- 全量备份:全量备份就是指对某一个时间点上的所有数据或应用进行的一个完全拷贝。
- 数据恢复快。
- 备份时间长
- 增量备份:增量备份是指在一次全备份或上一次增量备份后,以后每次的备份只需备份与前一次相比增加和者被修改的文件。这就意味着,第一次增量备份的对象是进行全备后所产生的增加和修改的文件;第二次增量备份的对象是进行第一次增量备份后所产生的增加和修改的文件,如此类推。
- 没有重复的备份数据
- 备份时间短
- 恢复数据时必须按一定的顺序进行
- 差异备份:备份上一次的完全备份后发生变化的所有文件。差异备份是指在一次全备份后到进行差异备份的这段时间内对那些增加或者修改文件的备份。在进行恢复时,我们只需对第一次全量备份和最后一次差异备份进行恢复。
mysql备份工具mysqldump
语法:
mysqldump [OPTIONS] database [tables ...]
mysqldump [OPTIONS] --all-databases [OPTIONS]
mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
常用选项:
-uUSERNAME 指定数据库用户名
-hHOST 指定服务器主机,请使用ip地址
-pPASSWORD 指定数据库用户的密码
-P# 指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3
mysql> show tables;
+----------------+
| Tables_in_lynk |
+----------------+
| armor |
| mastersword |
+----------------+
2 rows in set (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lynk |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
mysql> use lynk
Database changed
mysql> show tables;
+----------------+
| Tables_in_lynk |
+----------------+
| armor |
| mastersword |
+----------------+
2 rows in set (0.00 sec)
#全备
[root@lynk ~]# mysqldump -uroot -p -h127.0.0.1 --all-databases > all-201902211531.sql
Enter password:
[root@lynk ~]# ls
all-201902211531.sql anaconda-ks.cfg
#备份lynk库的mastersword和armor表
[root@lynk ~]# mysqldump -uroot -p -h127.0.0.1 lynk mastersword armor > table-201902211533.sql
Enter password:
[root@lynk ~]# ls
all-201902211531.sql anaconda-ks.cfg table-201902211533.sql
#备份lynk库
[root@lynk ~]# mysqldump -uroot -p -h127.0.0.1 --databases lynk > lynk-201902211536.sql
Enter password:
[root@lynk ~]# ls
all-201902211531.sql anaconda-ks.cfg lynk-201902211536.sql table-201902211533.sql
数据恢复
#模拟误删数据库
mysql> drop database lynk;
Query OK, 2 rows affected (0.03 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
#恢复lynk数据库
[root@lynk ~]# mysql -uroot -p -h127.0.0.1 < all-201902211531.sql
Enter password:
[root@lynk ~]# mysql -uroot -p
Enter password:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lynk |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
#恢复表
mysql> use lynk
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> source table-201902211533.sql
Query OK, 0 rows affected (0.00 sec)
···
mysql> show tables;
+----------------+
| Tables_in_lynk |
+----------------+
| armor |
| mastersword |
+----------------+
2 rows in set (0.00 sec)
#模拟删除整个数据库
mysql> drop database lynk;
Query OK, 2 rows affected (0.04 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
#恢复整个数据库
[root@lynk ~]# mysql -uroot -p -h127.0.0.1 < all-201902211531.sql
Enter password:
[root@lynk ~]# mysql -uroot -p -h127.0.0.1 -e 'show databases;'
Enter password:
+--------------------+
| Database |
+--------------------+
| information_schema |
| lynk |
| mysql |
| performance_schema |
| sys |
+--------------------+
差异备份
差异备份与全备和增备的操作不同,
差异备份是通过记录对数据库的操作而进行备份还原,其优点在于可以恢复到任意状态,而不是备份时的状态。
#开启mysql二进制日志功能
[root@lynk ~]# cat >> /etc/my.cnf <<EOF
server-id=1
log-bin=mysql_bin
EOF
[root@lynk ~]# service mysqld restart
#这是我的数据库表格
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lynk |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> show tables from lynk;
+----------------+
| Tables_in_lynk |
+----------------+
| student |
+----------------+
1 row in set (0.00 sec)
mysql> select * from lynk.student;
+------+-------------+------+
| id | name | age |
+------+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+------+-------------+------+
11 rows in set (0.00 sec)
#先对数据库进行一次全备
[root@lynk ~]# mysqldump -uroot -plynk123~ --single-transaction --flush-logs --master-data=2 --all-databases --delete-master-logs > all-20190222.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@lynk ~]# ll
总用量 630088
-rw-r--r--. 1 root root 802782 2月 22 19:18 all-20190222.sql
-rw-------. 1 root root 1269 2月 18 17:34 anaconda-ks.cfg
#新增内容
mysql> use lynk
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from student;
+------+-------------+------+
| id | name | age |
+------+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+------+-------------+------+
11 rows in set (0.00 sec)
mysql> update student set age = 4 where id = 1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+------+-------------+------+
| id | name | age |
+------+-------------+------+
| 1 | tom | 4 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+------+-------------+------+
11 rows in set (0.00 sec)
mysql差异备份恢复数据
#模拟删库
[root@lynk ~]# mysql -uroot -plynk123~ -e 'drop database lynk;'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@lynk ~]# mysql -uroot -plynk123~ -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
#模拟跑路ε=ε=(ノ ゚Д ゚)ノ(误)
#刷新二进制文件来创建一个新的
[root@lynk ~]# mysqladmin -uroot -plynk123~ flush-logs
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
[root@lynk ~]# ll /opt/data
总用量 122952
-rw-r-----. 1 mysql mysql 56 2月 22 15:20 auto.cnf
-rw-r-----. 1 mysql mysql 346 2月 22 19:12 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 2月 22 19:24 ibdata1
-rw-r-----. 1 mysql mysql 50331648 2月 22 19:24 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 2月 22 15:20 ib_logfile1
-rw-r-----. 1 mysql mysql 12582912 2月 22 19:18 ibtmp1
-rw-r-----. 1 mysql mysql 8648 2月 22 19:12 localhost.localdomain.err
-rw-r-----. 1 mysql mysql 4315 2月 22 19:12 lynk.err
drwxr-x---. 2 mysql mysql 4096 2月 22 15:20 mysql
-rw-r-----. 1 mysql mysql 636 2月 22 19:26 mysql_bin.000002
-rw-r-----. 1 mysql mysql 154 2月 22 19:26 mysql_bin.000003
-rw-r-----. 1 mysql mysql 38 2月 22 19:26 mysql_bin.index
-rw-r-----. 1 mysql mysql 6 2月 22 19:12 mysql.pid
drwxr-x---. 2 mysql mysql 8192 2月 22 15:20 performance_schema
drwxr-x---. 2 mysql mysql 8192 2月 22 15:20 sys
#进行全备恢复
[root@lynk ~]# mysql -uroot -plynk123~ < all-20190222.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
#但是我们将进行过全备之后,我们的数据是处于新增内容之前的状态
[root@lynk ~]# mysql -uroot -plynk123~ -e 'select * from lynk.student;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+-------------+------+
| id | name | age |
+------+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+------+-------------+------+
#进行差备恢复
#查询误删数据库位置
mysql> show binlog events in 'mysql_bin.000002';
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql_bin.000002 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.23-log, Binlog ver: 4 |
| mysql_bin.000002 | 123 | Previous_gtids | 1 | 154 | |
| mysql_bin.000002 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql_bin.000002 | 219 | Query | 1 | 291 | BEGIN |
| mysql_bin.000002 | 291 | Table_map | 1 | 345 | table_id: 110 (lynk.student) |
| mysql_bin.000002 | 345 | Update_rows | 1 | 401 | table_id: 110 flags: STMT_END_F |
| mysql_bin.000002 | 401 | Xid | 1 | 432 | COMMIT /* xid=479 */ |
| mysql_bin.000002 | 432 | Anonymous_Gtid | 1 | 497 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql_bin.000002 | 497 | Query | 1 | 589 | drop database lynk |
| mysql_bin.000002 | 589 | Rotate | 1 | 636 | mysql_bin.000003;pos=4 |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
10 rows in set (0.00 sec)
#可以看到是在位置497进行了drop操作,所以我们要从497的位置进行恢复
[root@lynk ~]# mysqlbinlog --stop-position=497 /opt/data/mysql_bin.000002 |mysql -uroot -plynk123~
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@lynk ~]# mysql -uroot -plynk123~ -e 'select * from lynk.student;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+-------------+------+
| id | name | age |
+------+-------------+------+
| 1 | tom | 4 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+------+-------------+------+
mySQL配置文件、备份与恢复的更多相关文章
- linux下mysql配置文件my.cnf最详细解释
MySQL配置文件在Windows下叫my.ini,在MySQL的安装根目录下:在Linux下叫my.cnf,该文件位于/etc/my.cnf. 可以查找下:find / -name my.cnf m ...
- 2020重新出发,MySql基础,MySql数据库备份与恢复
@ 目录 MySQL数据库备份与恢复 数据库为什么需要备份 MySQL备份类型 MySQL热备份及恢复 逻辑备份 mysqldump SELECT INTO-OUTFILE mydumper 裸文件备 ...
- MySQL配置文件my.cnf 例子最详细翻译
转的 MySQL配置文件my.cnf 例子最详细翻译,可以保存做笔记用. #BEGIN CONFIG INFO#DESCR: 4GB RAM, 只使用InnoDB, ACID, 少量的连接, 队列负载 ...
- mysql配置文件my.cnf详解
原文地址:mysql配置文件my.cnf详解 作者:gron basedir = path 使用给定目录作为根目录(安装目录). character-sets-dir = path 给出存放着字符集的 ...
- MySQL配置文件改变了datadir值
从Noinstall Zip Archive中安装MySQL正在从Noinstall软件包安装MySQL的用户可以使用这个说明来手动安装MySQL.从Zip archive 中安装MySQL的 步骤如 ...
- MySQL配置文件详解
MYSQL 配置文件详解 “全局缓存”.“线程缓存”,全局缓存是所有线程共享,线程缓存是每个线程连接上数据时创建一个线程(如果没有设置线程池),假如有200连接.那就是200个线程,如果参数设定值是1 ...
- MySQL的备份与恢复
Linux下的mysql的备份与恢复 备份: 比如我们要备份mysql中已经存在的名为linux的数据库,要用到命令mysqldump 命令格式如下: [root@linuxsir01 root]# ...
- mysql配置文件转载
#BEGIN CONFIG INFO#DESCR: 4GB RAM, 只使用InnoDB, ACID, 少量的连接, 队列负载大#TYPE: SYSTEM#END CONFIG INFO ## 此my ...
- Linux中MySQL配置文件my.cnf参数优化
MySQL参数优化这东西不好好研究还是比较难懂的,其实不光是MySQL,大部分程序的参数优化,是很复杂的.MySQL的参数优化也不例外,对于不同的需求,还有硬件的配置,优化不可能又最优选择,只能慢慢的 ...
随机推荐
- Windows程序设计_21_Win32文件操作
没什么新的内容,自己的练习代码,供大家点评. /* Windows系统编程--实例 1)复制文件 */ #define UNICODE //#define _UNICODE #include < ...
- PAT 乙级1093 字符串A+B (20 分)
1093 字符串A+B (20 分) 给定两个字符串 A 和 B,本题要求你输出 A+B,即两个字符串的并集.要求先输出 A,再输出 B,但重复的字符必须被剔除. 输入格式: 输入在两行中分别给出 A ...
- Promise的实现原理
1.Promise 介绍 Promise类似一个事务管理器,将用户异步操作流程用流水的形式来表达,用来延迟deferred和异步asynchronous. 特点如下: (1)对象的状态不受外界影响 P ...
- 通过spark sql 将 hdfs上文件导入到mongodb
功能:通过spark sql 将hdfs 中文件导入到mongdo 所需jar包有:mongo-spark-connector_2.11-2.1.2.jar.mongo-java-driver-3.8 ...
- Java虚拟机------JVM介绍
Java平台和语言最开始只是SUN公司在1990年12月开始研究的一个内部项目: Java的平台无关性 Java平台和语言最开始只是SUN公司在1990年12月开始研究的一个内部项目[stealth ...
- kubernetes学习笔记之十:RBAC
第一章.RBAC介绍 在Kubernetes中,授权有ABAC(基于属性的访问控制).RBAC(基于角色的访问控制).Webhook.Node.AlwaysDeny(一直拒绝)和AlwaysAllow ...
- JavaSE中的小知识点分析
1.System.out.println(); 调用System类中的public static final PrintStream out,输出为PrintStream(字节形式的输出流,为Outp ...
- nginx ------反向代理和负载均衡
最近由于公司的业务增长 服务器承受不住压力经常出现崩溃现象 为了解决 使用nginx的负载均衡解决,以下是操作步骤: 1.nginx 的负载均衡:将压力分散到不同的机器上 nginx不单可以作为强大的 ...
- Spring MVC 之 请求url 带后缀的情况
RequestMappingInfoHandlerMapping 在处理http请求的时候, 如果 请求url 有后缀,如果找不到精确匹配的那个@RequestMapping方法.那么,就把后缀去掉, ...
- (14)其他Linux命令
*****拷贝文件cp somefile.1 /home/hadoop/ 以原文件名进行拷贝 cp somefile.1 /home/hadoop/somefile.2 以新文件名进行 ...