当前位置:网站首页>MySQL database maintenance
MySQL database maintenance
2022-07-30 11:00:00 【Big tiger who loves beef】
文章目录
1 数据库维护
数据库维护是运维工程师或者DBA主要工作,包括性能监控、性能分析、性能调优、数据库备份和恢复等.
1.1 数据库文件
MySQL的每个数据库都对应存放在一个与数据库同名的文件夹中,MySQL数据库文件包括MySQL所建数据库文件和MySQL所用存储引擎创建的数据库文件.
1.1.1 MySQL创建并管理的数据库文件
.frm文件:存储数据表的框架结构,文件名与表名相同,每个表对应一个同名frm文件,与操作系统和存储引擎无关,即不管MySQL运行在何种操作系统上,使用何种存储引擎,都有这个文件.
除了必有的.frm文件,根据MySQL所使用的存储引擎的不同(MySQL常用的两个存储引擎是MyISAM和InnoDB),存储引擎会创建各自不同的数据库文件
MyISAM数据库表文件:
.MYD文件:即MY Data,表数据文件.MYI文件:即MY Index,索引文件.log文件:日志文件
InnoDB采用表空间(tablespace)来管理数据,存储表数据和索引,InnoDB数据库文件(即InnoDB文件集,ib-file set):
ibdata1、ibdata2等:系统表空间文件,存储InnoDB系统信息和用户数据库表数据和索引,所有表共用.ibd文件:单表表空间文件,每个表使用一个表空间文件(file per table),存放用户数据库表数据和索引- 日志文件: ib_logfile1、ib_logfile2
1.1.2 MySQL数据库存放位置
MySQL如果使用MyISAM存储引擎,数据库文件类型就包括.frm、.MYD、.MYI,默认存放位置是C:\Documentsand Settings\All Users\Application Data\MySQL\MySQL Server 5.1\data
MySQL如果使用InnoDB存储引擎,数据库文件类型就包括.frm、ibdata1、.ibd,.frm和.ibd文件默认存放位置是MySQL安装目录下的data文件夹
1.2 性能状态关键指标QPS和TPS
QPS,Queries Per Second:每秒查询数,一台数据库每秒能够处理的查询次数TPS,Transactions Per Second:每秒处理事务数
通过show status查看运行状态,会有300多条状态信息记录,其中有几个值帮可以我们计算出QPS和TPS,如下:
Uptime:服务器已经运行的实际,单位秒Questions:已经发送给数据库查询数Com_select:查询次数,实际操作数据库的Com_insert:插入次数Com_delete:删除次数Com_update:更新次数Com_commit:事务次数Com_rollback:回滚次数
那么,计算方法来了,基于Questions计算出QPS:
mysql> show global status like 'Questions';
mysql> show global status like 'Uptime';
QPS = Questions / Uptime
基于Com_commit和Com_rollback计算出TPS:
mysql> show global status like 'Com_commit';
mysql> show global status like 'Com_rollback';
mysql> show global status like 'Uptime';
TPS = (Com_commit + Com_rollback) / Uptime
另一计算方式:基于Com_select、Com_insert、Com_delete、Com_update计算出QPS
mysql> show global status where Variable_name in('com_select','com_insert','com_delete','com_update');
等待1秒再执行,获取间隔差值,第二次每个变量值减去第一次对应的变量值,就是QPS
TPS计算方法:
mysql> show global status where Variable_name in('com_insert','com_delete','com_update');
计算TPS,就不算查询操作了,计算出插入、删除、更新四个值即可.
经网友对这两个计算方式的测试得出,当数据库中myisam表比较多时,使用Questions计算比较准确.当数据库中innodb表比较多时,则以Com_*计算比较准确.
1.3 开启慢查询日志
MySQL开启慢查询日志,分析出哪条SQL语句比较慢,使用set设置变量,重启服务失效,可以在my.cnf添加参数永久生效.
mysql> set global slow-query-log=on #开启慢查询功能
mysql> set global slow_query_log_file='/var/log/mysql/mysql-slow.log'; #指定慢查询日志文件位置
mysql> set global log_queries_not_using_indexes=on; #记录没有使用索引的查询
mysql> set global long_query_time=1; #只记录处理时间1s以上的慢查询
分析慢查询日志,可以使用MySQL自带的mysqldumpslow工具,分析的日志较为简单.
mysqldumpslow -t 3 /var/log/mysql/mysql-slow.log
#查看最慢的前三个查询
也可以使用percona公司的pt-query-digest工具,日志分析功能全面,可分析slow log、binlog、general log.
分析慢查询日志:pt-query-digest /var/log/mysql/mysql-slow.log
分析binlog日志:mysqlbinlog mysql-bin.000001 >mysql-bin.000001.sql
pt-query-digest –type=binlog mysql-bin.000001.sql
分析普通日志:pt-query-digest –type=genlog localhost.log
1.4 数据库备份
备份数据库是最基本的工作,也是最重要的,否则后果很严重,但由于数据库比较大,上百G,往往备份都很耗费时间,所以就该选择一个效率高的备份策略,对于数据量大的数据库,一般都采用增量备份.常用的备份工具有mysqldump、mysqlhotcopy、xtrabackup等,mysqldump比较适用于小的数据库,因为是逻辑备份,所以备份和恢复耗时都比较长.mysqlhotcopy和xtrabackup是物理备份,备份和恢复速度快,不影响数据库服务情况下进行热拷贝,建议使用xtrabackup,支持增量备份.
1.4.1 myqldump示例
使用myqldump时,注意myqldump不是sql语句,mysqldump是mysql用于转存储数据库的实用程序
不能在MySQLVisualizer orDOS里的mysql下直接执行.
- 导出整个数据库
导出文件默认是存在mysql\bin目录下
mysqldump -u 用户名 -p 数据库名 > 导出的文件名
mysqldump -u user_name -p123456 database_name > outfile_name.sql
- 导出一个表
mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名
mysqldump -u user_name -p database_name table_name > outfile_name.sql
- 导出一个数据库结构
mysqldump -u user_name -p -d -add-drop-table database_name > outfile_name.sql
-d 没有数据 –add-drop-table 在每个create语句之前增加一个drop table
- 带语言参数导出
mysqldump -uroot -p –default-character-set=latin1 –set-charset=gbk –skip-optdatabase_name > outfile_name.sql
1.5 数据库修复
有时候MySQL服务器突然断电、异常关闭,会导致表损坏,无法读取表数据.这时就可以用到MySQL自带的两个工具进行修复,myisamchk和mysqlcheck
1.5.1 myisamchk修复
myisamchk: 只能修复myisam表,需要停止数据库
常用参数:
- -f –force 强制修复,覆盖老的临时文件,一般不使用
- -r –recover 恢复模式
- -q –quik 快速恢复
- -a –analyze 分析表
- -o –safe-recover 老的恢复模式,如果-r无法修复,可以使用此参数试试
- -F –fast 只检查没有正常关闭的表
快速修复weibo数据库:
cd /var/lib/mysql/weibo
myisamchk -r -q *.MYI
1.5.2 mysqlcheck修复
mysqlcheck: myisam和innodb表都可以用,不需要停止数据库,如修复单个表,可在数据库后面添加表名,以空格分割
常用参数:
- -a –all-databases 检查所有的库
- -r –repair 修复表
- -c –check 检查表,默认选项
- -a –analyze 分析表
- -o –optimize 优化表
- -q –quik 最快检查或修复表
- -F –fast 只检查没有正常关闭的表
快速修复weibo数据库:
mysqlcheck -r -q -uroot -p123 weibo
1.5.3 .frm文件修复
在MYSQL中建立任何一张数据表,在其数据目录对应的数据库目录下都有对应表的.frm文件,.frm文件is used to save each data table元数据(meta)信息,包括表结构的定义等,.frm文件It has nothing to do with the database storage engine,也就是任何存储引擎的数据表都必须有.frm文件,命名方式为数据表名.frm,如user.frm. .frm文件Can be used to restore the table structure in the event of a database crash.
1.5.3.1 InnoDB表结构的恢复
假定:MYSQL数据库已经崩溃,目前只有对应表的.frm文件,大家都知道,.frm文件Cannot be viewed with a text editor,因为如果不恢复,基本上来说对我们没什么用.这里我们为了测试,假定该文件为test_innodb.frm
该表创建脚本如下(After the table is created, the database server where the table is located is forcibly closed,Simulate crash scenarios)
mysql> create table test_innodb
-> (A int(11) default NULL,
-> B varchar(30) default NULL,
-> C date default NULL) engine=innodb;
Query OK, 0 rows affected (0.05 sec)
Introduction to recovery methods(过程):
- 在新的正常工作的
MYSQL环境下建立一个数据库,比如aa - 在aa数据库下建立同名的数据表
test_innodb,表结构随意,这里只有一个id字段,操作过程片段如下:
mysql> create table test_innodb (id bigint not null)engine=InnoDB;
Query OK, 0 rows affected (0.09 sec)
mysql> show tables;
+--------------+
| Tables_in_aa |
+--------------+
| test_innodb |
+--------------+
2 rows in set (0.00 sec)
mysql> desc test_innodb;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | bigint(20) | NO | | NULL | |
+-------+------------+------+-----+---------+-------+
1 row in set (0.00 sec)
- Left behind after a system crash
test_innodb.frmThe files are copied to the data directory of the normal database hereaa下,覆盖掉下边同名的.frm文件 - 重新启动MYSQL服务.
- Test whether the recovery is successful,进入aa数据库,用desc命令测试下:
mysql> desc test_innodb;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| A | int(11) | YES | | NULL | |
| B | varchar(30) | YES | | NULL | |
| C | date | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
OK,It is found that the table structure has been restored.
1.5.3.2 MyISAM表结构的恢复
MyISAMType table recovery is relatively straightforward.
同样先假定需要恢复的表的FRM文件为test_myisam.frm,表结构为
mysql> create table test_myisam
-> (A int(11) default NULL,
-> B varchar(30) default NULL,
-> C date default NULL) engine=myisam;
Query OK, 0 rows affected (0.05 sec)
恢复过程如下:
- 直接将
test_myisam.frm拷贝到正常数据库对应的数据目录下.这时测试
mysql> show tables;
+--------------+
| Tables_in_aa |
+--------------+
| test_innodb |
| test_myisam |
+--------------+
3 rows in set (0.00 sec)
mysql> desc test_myisam;
ERROR 1017 (HY000): Can't find file: 'test_myisam' (errno: 2)
发现只能通过show tables命令看见表名,但是表结构还是没有恢复,desc命令报错.
- 在与
test_myisam.frm同一目录建立以下2个文件,文件内容可以为空:test_myisam.MYD表数据文件和test_myisam.MYI索引文件 - 在
MYSQL命令行使用MYSQL本身的数据表恢复命令repair命令恢复表,如下:
mysql> repair table test_myisam USE_FRM;
+-----------------+--------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+-----------------+--------+----------+----------+
| aa.test_myisam | repair | status | OK |
+-----------------+--------+----------+----------+
1 row in set (0.00 sec)
根据结果可以知道,恢复命令执行成功,下边用desc命令测试下:
mysql> desc test_myisam;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| A | int(11) | YES | | NULL | |
| B | varchar(30) | YES | | NULL | |
| C | date | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.02 sec)
Sure enough, the recovery was successful.
也可以用show create table命令测试下:
mysql> show create table test_myisam;
+--------------+-----------------------------------------------------------------
----------------------------------------------------------------------+
| Table | Create Table
|
+--------------+-----------------------------------------------------------------
----------------------------------------------------------------------+
| test_myisam | Create TABLE `test_myisam` (
`A` int(11) DEFAULT NULL,
`B` varchar(30) DEFAULT NULL,
`C` date DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
在恢复MyISAM表结构时,提到MYD文件和MYI文件,Both files are exclusive to MyISAM存储引擎的,The former is used to saveMyISAM表的数据,The latter is used for storageMyISAM表的索引信息.
边栏推荐
- 4. yolov5-6.0 ERROR: AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor' solution
- Security Thought Project Summary
- The method of parameter passing
- OC - Manual Reference Counting Memory Management
- Shell system learning function
- spark udf accepts and handles null values.
- Domino Server SSL Certificate Installation Guide
- OC-关于alloc和dealloc(还没开始写)
- Easy gene: human tRNA gene loci showed age-related high DNA methylation | research articles
- MFCC转音频,效果不要太逗>V<!
猜你喜欢

图像去噪——Neighbor2Neighbor: Self-Supervised Denoising from Single Noisy Images

Domino Server SSL Certificate Installation Guide

【HMS core】【FAQ】HMS Toolkit典型问题合集1

鸿湖万联扬帆富设备开发板正式合入OpenHarmony主干

WARN: Establishing SSL connection without server's identity verification is not recommended when connecting to mysql

零代码开发入门:快速上手DIY函数公式的5个步骤

Flask's routing (app.route) detailed

Alibaba Cloud OSS Object Storage

FPGA刷题——计数器(简易秒表、可置位计数器、加减计数器)

Matplotlib--plot markers
随机推荐
AIX shell获取前几个月时间
Telerik2022 R2,有效的自动化测试
易基因:人类tRNA基因位点表现出与衰老相关的DNA高甲基化|研究文章
PyQt5 - draw sine curve with pixels
安全提示:Qt中的FreeType
Swift 常用扩展类和简单封装
【HarmonyOS】【ARK UI】HarmonyOS ets语言怎么实现双击返回键退出
Do you really understand the 5 basic data structures of Redis?
360发布面向未来的EDR,全方位守护政企用户终端安全
死锁的理解
还在用Swagger?我推荐这款零代码侵入的接口管理神器
360 released a future-oriented EDR to protect the security of government and enterprise user terminals in an all-round way
paging
OC-ARC(Automatic Reference Counting)自动引用计数
MySQL |子查询
【HMS core】【FAQ】HMS Toolkit典型问题合集1
活动速递| Apache Doris 性能优化实战系列直播课程初公开,诚邀您来参加!
Still using Swagger?I recommend this interface management artifact with zero code intrusion
Is it too late to apply for PMP now to take the September exam?Share agile full-true mock questions
oracle export dmp file type as "crash dump file"