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、鎖、等待等方面的信息,這個對象的內存需要從額外內存池中申請