數(shù)據(jù)結(jié)構(gòu)
- 非結(jié)構(gòu)化數(shù)據(jù)已慢,各種文檔、圖片霹购、視頻/音頻等都屬于非結(jié)構(gòu)化數(shù)據(jù)佑惠。對(duì)于這類數(shù)據(jù),我們一般直接整體進(jìn)行存儲(chǔ)齐疙,而且一般存儲(chǔ)為二進(jìn)制的數(shù)據(jù)格式(如文件膜楷、圖片、視頻贞奋、語(yǔ)音等需存入文件系統(tǒng)中)
- 結(jié)構(gòu)化數(shù)據(jù)赌厅,結(jié)構(gòu)化的數(shù)據(jù)是指可以使用關(guān)系型數(shù)據(jù)庫(kù)表示和存儲(chǔ),表現(xiàn)為二維形式的數(shù)據(jù)忆矛。一般特點(diǎn)是:數(shù)據(jù)以行為單位察蹲,一行數(shù)據(jù)表示一個(gè)實(shí)體的信息,每一行數(shù)據(jù)的屬性是相同的(如行數(shù)據(jù)等需存入關(guān)系型數(shù)據(jù)庫(kù)中)
- 半結(jié)構(gòu)化數(shù)據(jù)催训,半結(jié)構(gòu)化數(shù)據(jù)是結(jié)構(gòu)化數(shù)據(jù)的一種形式洽议,它并不符合關(guān)系型數(shù)據(jù)庫(kù)或其他數(shù)據(jù)表的形式關(guān)聯(lián)起來(lái)的數(shù)據(jù)模型結(jié)構(gòu),但包含相關(guān)標(biāo)記漫拭,用來(lái)分隔語(yǔ)義元素以及對(duì)記錄和字段進(jìn)行分層亚兄。因此,它也被稱為自描述的結(jié)構(gòu)采驻。(常見(jiàn)的半結(jié)構(gòu)數(shù)據(jù)有XML和JSON审胚,可存入NoSQL數(shù)據(jù)庫(kù)中)
簡(jiǎn)介
- 關(guān)系型數(shù)據(jù)庫(kù),瑞典基于C++語(yǔ)言開(kāi)發(fā)
- 小巧、實(shí)用礼旅、性能高
- 其他數(shù)據(jù)庫(kù)如Oracle(甲骨文)膳叨、SQLServer(微軟)、DB2(IBM)
特點(diǎn)
- 開(kāi)源
- 社區(qū)版免費(fèi)
- 跨平臺(tái)
- 安全性高
- 成本低
- 支持各種開(kāi)發(fā)語(yǔ)言
- 支持強(qiáng)大的內(nèi)置函數(shù)
- 數(shù)據(jù)存儲(chǔ)量大
架構(gòu)
安裝方式
- 可使用xdja_centos7.4裁剪版自帶MySQL5.7安裝包一鍵安裝
- 可使用以下腳本設(shè)置連接權(quán)限
常用命令
- 連接數(shù)據(jù)庫(kù)
mysql -uroot -p
- 顯示數(shù)據(jù)庫(kù)
show databases;
- 選擇數(shù)據(jù)庫(kù)
use xxx(databasename);
- 顯示數(shù)據(jù)庫(kù)表
show tables;
- 查看表描述
DESC xxx(datatable);
- 顯示數(shù)據(jù)庫(kù)版本痘系、時(shí)間等
SELECT VERSION(),CURRENT_DATE(),CURRENT_TIME();
- 顯示運(yùn)行的進(jìn)程
show processlist;
- 查看配置項(xiàng)
show variables like '%tx_isolation%';
- 查看innodb狀態(tài)
show engine innodb status\G;
DDL
- DDL(Data Definition Languages):數(shù)據(jù)定義語(yǔ)言菲嘴,定義不同的數(shù)據(jù)段、數(shù)據(jù)庫(kù)汰翠、表龄坪、列、索引等數(shù)據(jù)庫(kù)對(duì)象复唤,常用的關(guān)鍵字包括create健田、drop、alter等
- 創(chuàng)建數(shù)據(jù)庫(kù)test1
create database test1;
- 刪除指定數(shù)據(jù)庫(kù)
drop database test1;
- 修改表字段
alter table emp modify ename varchar(20);
- 增加佛纫、刪除表字段
alter table emp add column age int(3);
alter table emp drop column age;
DML
DML(Data Manipulation Language):數(shù)據(jù)操作語(yǔ)言妓局,用于添加、刪除呈宇、更新和查詢數(shù)據(jù)庫(kù)記錄跟磨,并檢查數(shù)據(jù)完整性,常用的關(guān)鍵字主要包括insert攒盈、delete抵拘、update和select等
插入記錄
insert into emp(ename,sal,deptno) values('zhangsan','2015-08-01','2000',1);
insert into emp(ename,sal,deptno) values('lisi','2015-08-01','3000',1);
create table dept(deptno int(3),deptname varchar(20);
insert into dept values(1,'tech'),(2,'sales'),(3,'fin');
- 更新、刪除記錄
update emp set sal=4000 where ename='lisi';
delete from emp where ename='lisi';
- 查詢指定列型豁、查詢不重復(fù)記錄
select ename,hiredate,sal,deptno from emp;
select distinct deptno from emp;
- 條件查詢與排序
select * from emp where deptno =1 and sal<3000;
select * from emp order by sal;
- 分組統(tǒng)計(jì)
select count(1) from emp;
select deptno,count(1) as empnum from emp group by deptno
- 分組統(tǒng)計(jì)+條件過(guò)濾
select deptno,count(1) as empnum from emp group by deptno with rollup;
select deptno,count(1) as empnum from emp group by deptno having count(1)>1;
- 聚合函數(shù)與多表關(guān)聯(lián)查詢
select sum(sal),max(sal),min(sal) from emp;
select ename,deptname from emp,dept where emp.deptno=dept.deptno;
存儲(chǔ)引擎
- 存儲(chǔ)引擎就是如何存儲(chǔ)數(shù)據(jù)僵蛛、如何為存儲(chǔ)的數(shù)據(jù)建立縮影和如何更新、查詢數(shù)據(jù)等技術(shù)的實(shí)現(xiàn)方法迎变;
- 在關(guān)系數(shù)據(jù)中數(shù)據(jù)的存儲(chǔ)是以表的形式存儲(chǔ)充尉,所以存儲(chǔ)引擎也可以稱為表類型(即存儲(chǔ)和操作此表的類型);
- 類型有MyISAM衣形、InnoDB驼侠、MERGE姿鸿、MEMORY(HEAP)等;
show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.03 sec)
- InnoDB
- 數(shù)據(jù)和索引合并存儲(chǔ)為一個(gè)文件倒源,.frm(描述表的結(jié)構(gòu)) .ibd(表數(shù)據(jù)文件)
- 支持外鍵苛预,事務(wù)處理
- 行鎖定
- 具有提交、回滾和崩潰恢復(fù)能力的事務(wù)安全
- 并行讀寫笋熬,適用于大量的寫操作的表
備份
備份所有數(shù)據(jù)庫(kù)
mysqldump -uroot -p --all-databases > itsca.sql
- 輸入密碼后即可備份所有庫(kù)
[root@xdja wch]# mysqldump -uroot -p --all-databases > itsca.sql
Enter password:
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.
備份指定數(shù)據(jù)庫(kù)
mysqldump -u root -p --databases 數(shù)據(jù)庫(kù)1 數(shù)據(jù)庫(kù)2 > xxx.sql
還原mysql備份內(nèi)容
- 在系統(tǒng)命令行中热某,輸入如下實(shí)現(xiàn)還原:
mysql -uroot -p123456 < /data/mysqlDump/mydb.sql
- 在登錄進(jìn)入mysql系統(tǒng)中,通過(guò)source指令找到對(duì)應(yīng)系統(tǒng)中的文件進(jìn)行還原:
mysql> source /data/mysqlDump/mydb.sql
基礎(chǔ)優(yōu)化
存儲(chǔ)引擎的選擇、字段設(shè)計(jì)胳螟、索引昔馋、SQL語(yǔ)句等都是影響MySQL性能的重要因素,本次暫不詳細(xì)討論糖耸。
僅從MySQL數(shù)據(jù)庫(kù)參數(shù)配置入手秘遏,探究設(shè)置合理的參數(shù)值來(lái)提升MySQL數(shù)據(jù)庫(kù)性能。
innodb_page_size
從InnoDB1.2.x版本開(kāi)始嘉竟,可通過(guò)參數(shù)innodb_page_size將頁(yè)的大小設(shè)置為4K垄提、8K、16K周拐。若設(shè)置完成铡俐,則所有表中頁(yè)的大小都為innodb_page_size,不可以對(duì)其再次修改妥粟,否則啟動(dòng)會(huì)報(bào)錯(cuò)审丘,踩過(guò)坑。
默認(rèn)是16k
Consider using a page size that matches the internal sector size of the disk. Early-generation SSD devices often have a 4KB sector size. Some newer devices have a 16KB sector size. The default InnoDB page size is 16KB. Keeping the page size close to the storage device block size minimizes the amount of unchanged data that is rewritten to disk.
請(qǐng)考慮使用與磁盤的內(nèi)部扇區(qū)大小匹配的頁(yè)面大小勾给。早期 SSD 器件通常具有 4KB 扇區(qū)大小滩报。某些較新的設(shè)備具有 16KB 扇區(qū)大小。默認(rèn) InnoDB 頁(yè)面大小為 16KB播急。使頁(yè)面大小接近存儲(chǔ)設(shè)備塊大小可最大限度地減少重寫到磁盤的未更改數(shù)據(jù)量脓钾。
參考文檔
https://dev.mysql.com/doc/refman/5.7/en/optimizing-innodb-diskio.html
innodb_buffer_pool_size
從其作用可以看出,當(dāng)系統(tǒng)的IO比較空閑的時(shí)候桩警,可以適當(dāng)將這個(gè)參數(shù)設(shè)大可训,當(dāng)IO吃緊時(shí),需要適當(dāng)減小捶枢,一般設(shè)置為內(nèi)存總大小的50%-75%
Buffer pool size must always be equal to or a multiple of innodb_buffer_pool_chunk_size * innodb_buffer_pool_instances. If you alter the buffer pool size to a value that is not equal to or a multiple of innodb_buffer_pool_chunk_size * innodb_buffer_pool_instances, buffer pool size is automatically adjusted to a value that is equal to or a multiple of innodb_buffer_pool_chunk_size * innodb_buffer_pool_instances.
When you increase or decrease buffer pool size, the operation is performed in chunks. Chunk size is defined by the innodb_buffer_pool_chunk_size variable, which has a default of 128 MB.
-
參考文章
https://dev.mysql.com/doc/refman/5.7/en/innodb-parameters.html#sysvar_innodb_buffer_pool_size
sync_binlog
sync_binlog=0握截,當(dāng)事務(wù)提交之后,MySQL不做fsync之類的磁盤同步指令刷新binlog_cache中的信息到磁盤烂叔,而讓Filesystem自行決定什么時(shí)候來(lái)做同步谨胞,或者cache滿了之后才同步到磁盤。
當(dāng)sync_binlog=1時(shí), MySQL在寫1次二進(jìn)制日志binary log時(shí), 會(huì)使用fdatasync()函數(shù)將二進(jìn)制binary log同步到disk中去(安全性最高的配置)蒜鸡。
sync_binlog=n胯努,當(dāng)每進(jìn)行n次事務(wù)提交之后牢裳,MySQL將進(jìn)行一次fsync之類的磁盤同步指令來(lái)將binlog_cache中的數(shù)據(jù)強(qiáng)制寫入磁盤。
-
參考文檔
https://dev.mysql.com/doc/refman/5.7/en/replication-options-binary-log.html#sysvar_sync_binlog
innodb_flush_log_at_trx_commit
當(dāng)innodb_flush_log_at_trx_commit=1時(shí)(默認(rèn))叶沛,每次事務(wù)提交時(shí), MySQL會(huì)把log buffer的數(shù)據(jù)寫入log file, 并且將log file flush到硬盤中蒲讯。這樣做的好處是數(shù)據(jù)安全性最佳,不足之處在于每次提交事務(wù)恬汁,都要進(jìn)行磁盤寫入的操作。在大并發(fā)的場(chǎng)景下辜伟,過(guò)于頻繁的磁盤讀寫會(huì)導(dǎo)致 CPU 資源浪費(fèi)氓侧,系統(tǒng)效率變低。(效率低导狡,安全性高)
設(shè)置為0 表示每隔 1 秒將數(shù)據(jù)寫入日志约巷,并將日志寫入磁盤;(效率高旱捧,安全性低)
設(shè)置為2 表示每次提交事務(wù)的時(shí)候都將數(shù)據(jù)寫入日志独郎,但是日志每間隔 1 秒寫入磁盤。(效率中枚赡,安全性中)
-
參考文檔
https://dev.mysql.com/doc/refman/5.7/en/innodb-parameters.html#sysvar_innodb_flush_log_at_trx_commit
雙1就是 sync_binlog=1和 innodb_flush_log_at_trx_commit=1氓癌。這兩個(gè)參數(shù)線上要保證為1,前者保證binlog的安全贫橙,后者保證redo的安全贪婉,它們?cè)跀?shù)據(jù)庫(kù)crash recovery的時(shí)候起到了關(guān)鍵做用,不設(shè)置為雙1可能導(dǎo)致數(shù)據(jù)丟失卢肃。
innodb_lru_scan_depth
innodb_lru_scan_depth是5.6新增加的參數(shù)疲迂,根據(jù)官方文檔描述,它會(huì)影響page cleaner線程每次刷臟頁(yè)的數(shù)量莫湘,這是一個(gè)每1秒loop一次的線程尤蒿。在Innodb內(nèi)部,這個(gè)參數(shù)對(duì)應(yīng)變量為srv_LRU_scan_depth
page cleaner 線程刷臟頁(yè)的長(zhǎng)度幅垮,從尾部開(kāi)始刷srv_LRU_scan_depth
默認(rèn)值為1024腰池,當(dāng)IO吃緊時(shí),應(yīng)適當(dāng)降低此值
-
參考文檔
https://www.cnblogs.com/zengkefu/p/5692803.html
https://dev.mysql.com/doc/refman/5.7/en/innodb-parameters.html#sysvar_innodb_lru_scan_depth
innodb_io_capacity
The default setting of 200 is generally sufficient for a lower-end non-rotational storage device. For higher-end, bus-attached devices, consider a higher setting such as 1000.
參數(shù) :innodb_io_capacity :數(shù)據(jù)庫(kù)落盤臟頁(yè)個(gè)數(shù) ,配置壓力和磁盤的性能相關(guān)忙芒,如果過(guò)大巩螃,IO能力不足,則出現(xiàn)卡頓匕争。
innodb_io_capacity默認(rèn)是200避乏,單位是頁(yè),該參數(shù)的設(shè)置大小取決于硬盤的IOPS甘桑,即每秒的輸入輸出量(或讀寫次數(shù))
-
參考文檔
https://dev.mysql.com/doc/refman/5.7/en/optimizing-innodb-diskio.html
innodb_io_capacity_max
If you specify an
innodb_io_capacity
setting at startup but do not specify a value forinnodb_io_capacity_max
,innodb_io_capacity_max
defaults to twice the value ofinnodb_io_capacity
or 2000, whichever value is greater.-
參考文檔
https://dev.mysql.com/doc/refman/5.7/en/optimizing-innodb-diskio.html
https://dev.mysql.com/doc/refman/5.7/en/innodb-configuring-io-capacity.html
innodb_max_dirty_pages_pct
- innodb_max_dirty_pages_pct 是 MySQL InnoDB 存儲(chǔ)引擎非常重要的一個(gè)參數(shù)拍皮,用來(lái)控制buffer pool中臟頁(yè)的百分比歹叮,當(dāng)臟頁(yè)數(shù)量占比超過(guò)這個(gè)參數(shù)設(shè)置的值時(shí),InnoDB會(huì)啟動(dòng)刷臟頁(yè)的操作铆帽。該參數(shù)只控制臟頁(yè)百分比咆耿,并不會(huì)影響刷臟頁(yè)的速度。
- 默認(rèn)值:75%
- 除了臟頁(yè)百分比達(dá)到閾值innodb_max_dirty_pages_pct觸發(fā)刷臟頁(yè)以外爹橱,還有很多條件萨螺,也會(huì)觸發(fā)刷臟頁(yè),主要包括:
- REDO日志快滿的時(shí)候愧驱。
- 為了保證MySQL中的空閑頁(yè)面的數(shù)量慰技,會(huì)從LRU鏈表尾部淘汰一部分頁(yè)面作為空閑頁(yè)。如果對(duì)應(yīng)的頁(yè)面是臟頁(yè)的話组砚,就需要先將頁(yè)面刷到磁盤吻商。
- MySQL
- MySQL實(shí)例正常關(guān)閉時(shí)候。
innodb_flush_neighbors
innodb_flush_neighbors 參數(shù)是InnoDB用來(lái)控制buffer pool刷臟頁(yè)時(shí)是否把臟頁(yè)鄰近的其他臟頁(yè)一起刷到磁盤糟红,在傳統(tǒng)的機(jī)械硬盤時(shí)代艾帐,打開(kāi)這個(gè)參數(shù)能夠減少磁盤尋道的開(kāi)銷,顯著提升性能盆偿。
取值范圍:0柒爸,1,2
默認(rèn)值:5.7版本為1事扭, 8.0版本為0
含義:
- 設(shè)置為0時(shí)揍鸟,表示刷臟頁(yè)時(shí)不刷其附近的臟頁(yè)。
- 設(shè)置為1時(shí)句旱,表示刷臟頁(yè)時(shí)連帶其附近毗連的臟頁(yè)一起刷掉阳藻。
- 設(shè)置為2時(shí),表示刷臟頁(yè)時(shí)連帶其附近區(qū)域的臟頁(yè)一起刷掉谈撒。1與2的區(qū)別是2刷的區(qū)域更大一些腥泥。
如果MySQL服務(wù)器磁盤是傳統(tǒng)的HDD存儲(chǔ)設(shè)備,打開(kāi)該參數(shù)啃匿,能夠減少I/O磁盤尋道的開(kāi)銷蛔外,提高性能,而對(duì)于SSD設(shè)備溯乒,尋道時(shí)間的性能影響很小夹厌,關(guān)閉該參數(shù),反而能夠分散寫操作裆悄,提高數(shù)據(jù)庫(kù)性能矛纹。由于SSD設(shè)備的普及,MySQL 8.0 將該參數(shù)的默認(rèn)值由1調(diào)整為0光稼。
-
參考文檔
https://www.mytecdb.com/blogDetail.php?id=117
https://dev.mysql.com/doc/refman/5.7/en/innodb-parameters.html#sysvar_innodb_flush_neighbors
innodb_page_cleaners
show global status like '%Innodb_buffer_pool_wait_free%';
如果值很大或南,則需要增加innodb_page_cleaners值孩等,同時(shí)增加寫線程。-
參考文檔
http://www.reibang.com/p/6991304a8e26
http://www.reibang.com/p/ddb24f9afae0https://dev.mysql.com/doc/refman/5.7/en/innodb-parameters.html#sysvar_innodb_flush_neighbors
innodb_log_file_size
Redo log的空間通過(guò)
innodb_log_file_size
和innodb_log_files_in_group
(默認(rèn)2)參數(shù)來(lái)調(diào)節(jié)采够,將這倆參數(shù)相乘即可得到總的可用Redo log 空間肄方。可以使用MySQL監(jiān)控PMM來(lái)進(jìn)行詳細(xì)分析,具體參考以下文章
-
參考文檔
https://blog.csdn.net/kai404/article/details/80242262
https://dev.mysql.com/doc/refman/5.7/en/innodb-parameters.html#sysvar_innodb_log_file_size
優(yōu)化結(jié)果
- 為我司安裝MySQL默認(rèn)參數(shù)蹬癌,需根據(jù)不同機(jī)器硬件配置進(jìn)行調(diào)整
[mysqld]
########basic settings########
server-id = 11
port = 3306
user = mysql
#bind_address = 10.166.224.32 #根據(jù)實(shí)際情況修改
autocommit = 1 #5.6.X安裝時(shí)权她,需要注釋掉,安裝完成后再打開(kāi)
character_set_server=utf8mb4
skip_name_resolve = 1
max_connections = 800
max_connect_errors = 1000
datadir = /home/mysql/data #根據(jù)實(shí)際情況修改,建議和程序分離存放
transaction_isolation = READ-COMMITTED
#explicit_defaults_for_timestamp = 1
join_buffer_size = 134217728
tmp_table_size = 67108864
tmpdir = /tmp
max_allowed_packet = 16777216
sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
interactive_timeout = 1800
wait_timeout = 1800
read_buffer_size = 16777216
read_rnd_buffer_size = 33554432
sort_buffer_size = 33554432
########log settings########
log_error = /home/mysql/logs/error.log
slow_query_log = 1
slow_query_log_file = /home/mysql/logs/slow.log
log_queries_not_using_indexes = 1
log_slow_admin_statements = 1
log_slow_slave_statements = 1
log_throttle_queries_not_using_indexes = 10
expire_logs_days = 60
long_query_time = 1
min_examined_row_limit = 100
lower_case_table_names = 1
########replication settings########
master_info_repository = TABLE
relay_log_info_repository = TABLE
log_bin = /home/mysql/binlog/bin.log
sync_binlog = 1
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates
binlog_format = row
relay_log = /home/mysql/relaylog/relay.log
relay_log_recovery = 1
binlog_gtid_simple_recovery = 1
slave_skip_errors = ddl_exist_errors
########innodb settings########
#innodb_page_size = 8192
innodb_buffer_pool_size = 24G #根據(jù)實(shí)際情況修改
innodb_buffer_pool_instances = 12
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_lru_scan_depth = 2000
innodb_lock_wait_timeout = 5
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_flush_method = O_DIRECT
innodb_file_format = Barracuda
innodb_file_format_max = Barracuda
innodb_log_group_home_dir = /home/mysql/redolog/ #根據(jù)實(shí)際情況修改
innodb_undo_directory = /home/mysql/undolog/ #根據(jù)實(shí)際情況修改
innodb_undo_logs = 128
innodb_undo_tablespaces = 3
innodb_flush_neighbors = 1
innodb_log_file_size = 8G #根據(jù)實(shí)際情況修改
innodb_log_buffer_size = 16777216
innodb_purge_threads = 4
innodb_large_prefix = 1
innodb_thread_concurrency = 64
innodb_print_all_deadlocks = 1
innodb_strict_mode = 1
innodb_sort_buffer_size = 67108864
innodb_flush_log_at_trx_commit = 1
[mysqld-5.7]
innodb_buffer_pool_dump_pct = 40
innodb_page_cleaners = 12
innodb_undo_log_truncate = 1
innodb_max_undo_log_size = 2G
innodb_purge_rseg_truncate_frequency = 128
binlog_gtid_simple_recovery=1
log_timestamps=system
transaction_write_set_extraction=MURMUR32
show_compatibility_56=on
- MySQL數(shù)據(jù)庫(kù)服務(wù)器安裝fio
[root@xdja wch]# tar -zxvf fio-2.1.10.tar.gz
[root@xdja wch]# cd fio-2.1.10/
[root@xdja fio-2.1.10]# ./configure
[root@xdja fio-2.1.10]# make
[root@xdja fio-2.1.10]# make Install
[root@xdja fio-2.1.10]# cd ..
[root@xdja wch]# fio -filename=test0628 -direct=1 -iodepth 1 -thread -rw=randrw -ioengine=psync -bs=16k -size=500M -numjobs=10 -runtime=10 -group_reporting -name=mytest
direct=1 測(cè)試過(guò)程繞過(guò)機(jī)器自帶的buffer逝薪,使測(cè)試結(jié)果更真實(shí)
rw=randrw 測(cè)試隨機(jī)混合寫和讀的I/O
ioengine=psync io引擎使用pync方式
bs=16k 單次io的塊文件大小為16k
numjobs=10 本次的測(cè)試線程為10
mytest: (g=0): rw=randrw, bs=16K-16K/16K-16K/16K-16K, ioengine=psync, iodepth=1
...
fio-2.1.10
Starting 10 threads
mytest: Laying out IO file(s) (1 file(s) / 500MB)
Jobs: 10 (f=10): [mmmmmmmmmm] [100.0% done] [1328KB/896KB/0KB /s] [83/56/0 iops] [eta 00m:00s]
mytest: (groupid=0, jobs=10): err= 0: pid=23464: Mon Jun 28 12:52:55 2021
read : io=11632KB, bw=1156.2KB/s, iops=72, runt= 10061msec
clat (msec): min=1, max=273, avg=84.36, stdev=55.83
lat (msec): min=1, max=273, avg=84.36, stdev=55.83
clat percentiles (msec):
| 1.00th=[ 5], 5.00th=[ 12], 10.00th=[ 18], 20.00th=[ 31],
| 30.00th=[ 48], 40.00th=[ 62], 50.00th=[ 77], 60.00th=[ 94],
| 70.00th=[ 113], 80.00th=[ 130], 90.00th=[ 159], 95.00th=[ 186],
| 99.00th=[ 249], 99.50th=[ 258], 99.90th=[ 273], 99.95th=[ 273],
| 99.99th=[ 273]
bw (KB /s): min= 56, max= 191, per=10.04%, avg=116.07, stdev=29.91
write: io=11424KB, bw=1135.5KB/s, iops=70, runt= 10061msec
clat (usec): min=277, max=205220, avg=54698.78, stdev=44032.60
lat (usec): min=277, max=205221, avg=54699.64, stdev=44032.65
clat percentiles (usec):
| 1.00th=[ 828], 5.00th=[ 1272], 10.00th=[ 1672], 20.00th=[ 3472],
| 30.00th=[23168], 40.00th=[37632], 50.00th=[53504], 60.00th=[64768],
| 70.00th=[79360], 80.00th=[92672], 90.00th=[112128], 95.00th=[134144],
| 99.00th=[171008], 99.50th=[193536], 99.90th=[205824], 99.95th=[205824],
| 99.99th=[205824]
bw (KB /s): min= 29, max= 206, per=10.19%, avg=115.64, stdev=38.23
lat (usec) : 500=0.21%, 750=0.21%, 1000=0.69%
lat (msec) : 2=4.93%, 4=5.27%, 10=3.68%, 20=5.55%, 50=18.18%
lat (msec) : 100=34.35%, 250=26.44%, 500=0.49%
cpu : usr=0.01%, sys=0.06%, ctx=1492, majf=0, minf=7
IO depths : 1=100.0%, 2=0.0%, 4=0.0%, 8=0.0%, 16=0.0%, 32=0.0%, >=64=0.0%
submit : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.0%
complete : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.0%
issued : total=r=727/w=714/d=0, short=r=0/w=0/d=0
latency : target=0, window=0, percentile=100.00%, depth=1
Run status group 0 (all jobs):
READ: io=11632KB, aggrb=1156KB/s, minb=1156KB/s, maxb=1156KB/s, mint=10061msec, maxt=10061msec
WRITE: io=11424KB, aggrb=1135KB/s, minb=1135KB/s, maxb=1135KB/s, mint=10061msec, maxt=10061msec
Disk stats (read/write):
dm-2: ios=719/1496, merge=0/0, ticks=60164/81876, in_queue=142913, util=100.00%, aggrios=727/1144, aggrmerge=0/370, aggrticks=61291/63730, aggrin_queue=125075, aggrutil=100.00%
sda: ios=727/1144, merge=0/370, ticks=61291/63730, in_queue=125075, util=100.00%
- 可以看到讀寫的iops基本在70左右隅要,根據(jù)以上基本參數(shù)解釋,現(xiàn)有服務(wù)器(Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz 4核8線程翼闽,24G內(nèi)存拾徙,疊瓦式機(jī)械硬盤)進(jìn)行以下參數(shù)設(shè)置洲炊,可以明顯降低甚至消除接口性能測(cè)試過(guò)程中的TPS抖動(dòng)(MySQL數(shù)據(jù)庫(kù)刷盤導(dǎo)致感局,可以從error.log日志看到刷盤信息)
innodb_buffer_pool_size =6G
innodb_buffer_pool_instances = 1
innodb_lru_scan_depth = 200
innodb_io_capacity = 100
innodb_io_capacity_max = 200
innodb_log_file_size = 4G
innodb_page_cleaners= 1
- 以上參數(shù)只是針對(duì)特定機(jī)器匹配服務(wù)相對(duì)最優(yōu)的參數(shù)
其他一些概念
刷臟頁(yè)機(jī)制
https://www.ywnds.com/?p=11039&viewuser=489
https://www.cnblogs.com/JiangLe/p/7419835.htmlredolog與binlog的區(qū)別
https://blog.csdn.net/wanbin6470398/article/details/81941586InnoDB臟頁(yè)刷新機(jī)制Checkpoint
https://www.cnblogs.com/olinux/p/5196139.html
http://www.reibang.com/p/0b19e1cd5e8c
https://blog.csdn.net/qq_18312025/article/details/78597681
問(wèn)題匯總
- 驗(yàn)證一個(gè)項(xiàng)目接口性能測(cè)試時(shí)持續(xù)出現(xiàn)數(shù)據(jù)庫(kù)死鎖,本項(xiàng)目基于arm架構(gòu)麒麟系統(tǒng)暂衡,mysql為源碼編譯询微,transaction_isolation = REPEATABLE-READ
Deadlock found when trying to get lock; try restarting transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
- 嘗試查看項(xiàng)目之前的mysql服務(wù),默認(rèn)為transaction_isolation = READ-COMMITTED狂巢,調(diào)整之后再次驗(yàn)證未再出現(xiàn)死鎖問(wèn)題
- 參考文檔