InnoDB存储引擎

MySQL数据库与实例:

数据库:物理操作系统文件或其他形式文件类型的集合。
数据库实例:由数据库后台进程/线程以及一个共享内存区组成,用来操作数据库文件
一个数据库对用一个数据库实例,集群对应多个实例。

启动实例时,MySQL会读取配置文件,根据配置文件参数启动数据库实例
[root@localhost ~]# mysql --help |grep my.cnf
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf 

MySQL在Linux下按照/etc/my.cnf  -->  /etc/mysql/my.cnf  -->  /usr/local/mysql/etc/my.cnf  -->  ~/.my.cnf 的顺序读取配置文件

Windows环境下后缀名可能是.cnf 也可以是.ini
Default options are read from the following files in the given order:
C:\Windows\my.ini C:\Windows\my.cnf C:\my.ini C:\my.cnf C:\Program Files\MySQL\M
ySQL Server 5.5\my.ini C:\Program Files\MySQL\MySQL Server 5.5\my.cnf


根据datadir参数找到数据库文件所在路径
mysql> show variables like 'datadir';
+---------------+---------------------+
| Variable_name | Value               |
+---------------+---------------------+
| datadir       | /backup/mysql_data/ |
+---------------+---------------------+
1 row in set (0.00 sec)

mysql> system ls /backup/mysql_data/local*
/backup/mysql_data/localhost.localdomain.err  /backup/mysql_data/localhost.localdomain.pid  /backup/mysql_data/localhost-slow.log

InnoDB体系架构

1. 后台进程

  • 负责刷新内存池的数据,保证缓存池中的内存缓存是最近的数据
  • 将已修改的数据刷新到磁盘文件
  • 保证在数据库发生异常时恢复到正常运行状态

InnoDB存储引擎是在一个master thread的线程上几乎完成所有工作。
默认情况下,InnoDB存储引擎的后台进程有7个:4个IO thread (insert buffer thread、log thread、
read thread、write thread),1个master thread,一个锁(lock)监控线程,一个错误监控线程。

InnoDB增加了默认的IO thread的数量,read thread和write thread分别增加到4个
mysql> show variables like '%io_threads%';
+-------------------------+-------+
| Variable_name           | Value |
+-------------------------+-------+
| innodb_read_io_threads  | 4     |
| innodb_write_io_threads | 4     |
+-------------------------+-------+
2 rows in set (0.00 sec)


mysql> show engine innodb status \G;
*************************** 1. row ***************************
  Type: InnoDB
  Name: 
Status: 
=====================================
140223 16:18:14 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 47 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 8117 1_second, 8117 sleeps, 726 10_second, 875 background, 875 flush
srv_master_thread log flush and writes: 8150
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 519, signal count 515
Mutex spin waits 258, rounds 2224, OS waits 34
RW-shared spins 485, rounds 14523, OS waits 475
RW-excl spins 9, rounds 336, OS waits 10
Spin rounds per wait: 8.62 mutex, 29.94 RW-shared, 37.33 RW-excl
------------
TRANSACTIONS
------------
Trx id counter 2FACE
Purge done for trx's n:o < 2FAAF undo n:o < 0
History list length 1552
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0, not started
MySQL thread id 6626, OS thread handle 0x2adb6275a940, query id 417450 localhost root
show engine innodb status
---TRANSACTION 0, not started
MySQL thread id 6661, OS thread handle 0x2adb6c305940, query id 417400 192.168.193.217 test217
---TRANSACTION 2FACD, not started
MySQL thread id 6662, OS thread handle 0x2adb6cf1d940, query id 417448 192.168.193.217 test217
---TRANSACTION 2F9DC, not started
MySQL thread id 6630, OS thread handle 0x2adb62352940, query id 417108 192.168.1.175 root
---TRANSACTION 2FA83, not started
MySQL thread id 6627, OS thread handle 0x2adb6ee5b940, query id 416123 192.168.1.106 root
--------
FILE I/O
--------
I/O thread 0 state: waiting for completed aio requests (insert buffer thread)
I/O thread 1 state: waiting for completed aio requests (log thread)
I/O thread 2 state: waiting for completed aio requests (read thread)
I/O thread 3 state: waiting for completed aio requests ( read thread )
I/O thread 4 state: waiting for completed aio requests ( read thread )
I/O thread 5 state: waiting for completed aio requests ( read thread )
I/O thread 6 state: waiting for completed aio requests (write thread)
I/O thread 7 state: waiting for completed aio requests ( write thread )
I/O thread 8 state: waiting for completed aio requests ( write thread )
I/O thread 9 state: waiting for completed aio requests ( write thread )
Pending normal aio reads: 0 [0, 0, 0, 0] , aio writes: 0 [0, 0, 0, 0] ,
 ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 0; buffer pool: 0
5288 OS file reads, 14403 OS file writes, 2085 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 0 merges
merged operations:
 insert 0, delete mark 0, delete 0
discarded operations:
 insert 0, delete mark 0, delete 0
Hash table size 8850461, node heap has 5 buffer(s)
0.00 hash searches/s, 0.00 non-hash searches/s
---
LOG
---
Log sequence number 4008081363
Log flushed up to   4008081363
Last checkpoint at  4008081363
0 pending log writes, 0 pending chkp writes
7955 log i/o's done, 0.00 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 4395630592; in additional pool allocated 0
Dictionary memory allocated 1667822
Buffer pool size   262143
Free buffers       256521
Database pages     5617
Old database pages 2053
Modified db pages  0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 5277, created 340, written 5929
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 5617, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
1 read views open inside InnoDB
Main thread process no. 4950, id 47121635174720, state: waiting for server activity
Number of rows inserted 302915, updated 822, deleted 41, read 13071916
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================

1 row in set (0.00 sec)



2. InnoDB内存池

  • 缓冲池(buffer pool) 由innodb_buffer_pool_size  控制
  • 重做日志缓冲池 (redo log buffer) 由innodb_log_buffer_size   控制
  • 额外的内存池 (additional memory pool)  由innodb_additional_mem_pool_size   控制

mysql> show variables like '%innodb%size';
+---------------------------------+------------+
| Variable_name                   | Value      |
+---------------------------------+------------+
| innodb_additional_mem_pool_size | 67108864   |
| innodb_buffer_pool_size         | 4294967296 |
| innodb_log_buffer_size          | 16777216   |
| innodb_log_file_size            | 536870912  |
| innodb_purge_batch_size         | 20         |
+---------------------------------+------------+
5 rows in set (0.00 sec)

2.1 缓冲池

缓冲池是占最大块内存的部分,用来存放各种数据的缓存。InnoDB的工作方式时间数据库文件按页(16K)读取到缓存池,然后按最近最少使用(LRU)的算法来保留在缓存池中的缓存数据,如果数据库文件需要修改,会首先修改在缓冲池中的页(发生修改后,该页变成脏页),然后按照一定的频率将缓冲池中的脏页刷新(flush)到文件。
Buffer pool size 表示有多少缓冲帧(buffer frame),每个buffer frame是16K,所以一共分配了262143*16/1024/1024=4G
Free buffers 表示当前空闲的缓冲帧
Database pages    表示已经使用的缓冲帧
Modified db pages  表示脏页的数量

缓冲池中缓存的数据页类型有:索引页、数据页、undo页、插入缓冲(insert buffer)、自适应哈希索引(adaptive hash index)、InnoDB存储的锁数据(lock info)、数据字典信息(data dictionary)等。

2.2 重做日志缓冲池

存储重做日志信息,然后按照一定频率将其刷新到重做日志文件中。该值一般不需设置太大,一般每秒都会刷新,只需要保证每秒产生的事务量在缓冲池之内即可。

2.3 额外的内存池

在InnoDB中对内存的管理是通过内存堆(heap)的方式进行,InnoDB实例会申请缓冲池(innodb_buffer_pool)的空间,每个缓冲池的帧缓冲(frame buffer)有对应的缓冲控制对象(buffer control block),这些对象记录了LRU、锁、等待等方面的信息,这个对象的内存需要从额外内存池中申请  

                                                             

发布了54 篇原创文章 · 获赞 5 · 访问量 3万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章