MySql Study案例之--MySql體系和存儲引擎 1、數(shù)據(jù)庫和實例 數(shù)據(jù)庫 :物理操作系統(tǒng)文件或其他形式文件類型的集合。在MySQL中,數(shù)據(jù)庫文件可以是frm、myd、myi、ibd結尾的文件。當使用NDB引擎時,數(shù)據(jù)庫文件可能不是操作系統(tǒng)上的文件,而是存放與內存之中的文
MySql Study案例之--MySql體系和存儲引擎
1、數(shù)據(jù)庫和實例
數(shù)據(jù)庫:物理操作系統(tǒng)文件或其他形式文件類型的集合。在MySQL中,數(shù)據(jù)庫文件可以是frm、myd、myi、ibd結尾的文件。當使用NDB引擎時,數(shù)據(jù)庫文件可能不是操作系統(tǒng)上的文件,而是存放與內存之中的文件,但是定義仍然不變。
數(shù)據(jù)庫實例:由數(shù)據(jù)庫后臺進程/線程以及一個共享內存區(qū)組成。共享內存可以被運行的后臺進程/線程所共享。需要牢記的是,數(shù)據(jù)庫實例才是真正用來操作數(shù)據(jù)庫文件的。
在MySQL中,實例和數(shù)據(jù)庫通常關系是一一對應,即一個實例對應一個數(shù)據(jù)庫,一個數(shù)據(jù)庫對應一個實例。但是,在集群情況下可能存在一個數(shù)據(jù)庫可被多個實例訪問的情況。
MySQL被設計為一個單進程多線程架構的數(shù)據(jù)庫。
2、MySQL進程
[root@rh6 ~]#mysqld_safe & [root@rh6 ~]# ps -ef|grep mysql |grep -v grep root 4168 4130 0 14:48 pts/1 00:00:00 /bin/sh ./mysqld_safe mysql 4294 4168 0 14:48 pts/1 00:00:14 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/var/lib/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock root 4643 4130 0 15:26 pts/1 00:00:00 mysql -h localhost -u root -p MySQL配置文件: [root@rh6 ~]# mysql --help |grep my.cnf order of preference, my.cnf, $MYSQL_TCP_PORT, /etc/mysql/my.cnf /etc/my.cnf ~/.my.cnf 數(shù)據(jù)文件路徑: mysql> show variables like 'datadir' \G *************************** 1. row *************************** Variable_name: datadir Value: /var/lib/mysql/ 1 row in set (0.00 sec) [root@rh6 bin]# ls -l /var/lib/mysql/ total 28688 -rw-rw---- 1 mysql mysql 56 Jan 28 17:25 auto.cnf -rw-rw---- 1 mysql mysql 18874368 Feb 2 14:48 ibdata1 -rw-rw---- 1 mysql mysql 5242880 Feb 2 14:48 ib_logfile0 -rw-rw---- 1 mysql mysql 5242880 Jan 28 17:21 ib_logfile1 drwx------ 2 mysql root 4096 Jan 28 17:21 mysql srwxrwxrwx 1 mysql mysql 0 Feb 2 14:48 mysql.sock drwx------ 2 mysql mysql 4096 Jan 28 17:21 performance_schema drwx------ 2 mysql root 4096 Jan 28 17:21 test
3、MySQL的結構
在具體介紹MySQL的存儲引擎之前,先來介紹一下MySQL的結構。
從圖中可以看到MySQL由以下幾個部分組成:
連接池
管理服務和工具組件
SQL接口
查詢分析器
優(yōu)化器
緩存
插入式存儲引擎
物理文件
4、InnoDB存儲引擎:
InnoDB是Mysql數(shù)據(jù)庫的一種存儲引擎:
InnoDB給Mysql的表提供了 事務、回滾、崩潰修復能力、多版本并發(fā)控制的事務安全、間隙鎖(可以有效的防止幻讀的出現(xiàn))、支持輔助索引、聚簇索引、自適應hash索引、支持熱備、行級鎖。還有InnoDB是Mysql上唯一一個提供了外鍵約束的引擎。
InnoDB存儲引擎中,創(chuàng)建的表的表結構是單獨存儲的并且存儲在.frm文件中。數(shù)據(jù)和索引存儲在一起的并且存儲在表空間中。但是默認情況下mysql會將數(shù)據(jù)庫的所有InnoDB表存儲在一個表空間中的。其實這種方式管理起來非常的不方便而且還不支持高級功能所以建議每個表存儲為一個表空間實現(xiàn)方式為:使用服務器變量innodb_file_per_table = 1。
如果需要頻繁的進行更新、刪除操作的數(shù)據(jù)庫也可選擇InnoDB存儲引擎。因為該存儲引擎可以實現(xiàn)事務提交和回滾。
InnoDB存儲引擎內存由以下幾個部分組成:緩沖池(buffer pool)、重做日志緩沖池(redo log buffer)以及額外的內存池(additional memory pool),分別由配置文件中的參數(shù)innodb_buffer_pool_size和innodb_log_buffer_size的大小決定。
InnoDB體系結構:
后臺線程的主要作用是負責刷新內存池中的數(shù)據(jù),保證緩沖池中的內存緩存的是最近的數(shù)據(jù)。此外,將已修改的數(shù)據(jù)文件刷新到磁盤文件,同時保證在數(shù)據(jù)庫發(fā)生異常情況下InnoDB能恢復到正常運行狀態(tài)。
后臺線程
由于Oracle是多進程的架構(Windows下除外),因此可以通過一些很簡單的命令來得知Oracle當前運行的后臺進程,如ipcs命令。一般來說,Oracle的核心后臺進程有CKPT、DBWn、LGWR、ARCn、PMON、SMON等。
很多DBA問我,InnoDB存儲引擎是否也是這樣的架構,只不過是多線程版本的實現(xiàn)后,我決定去看InnoDB的源代碼,發(fā)現(xiàn)InnoDB并不是這樣對數(shù)據(jù)庫進程進行操作的。InnoDB存儲引擎是在一個被稱做master thread的線程上幾乎實現(xiàn)了所有的功能。
默認情況下,InnoDB存儲引擎的后臺線程有7個—4個IO thread,1個master thread,1個鎖(lock)監(jiān)控線程,1個錯誤監(jiān)控線程。IO thread的數(shù)量由配置文件中的innodb_file_ io_threads參數(shù)控制,默認為4,如下所示。
mysql> show engine innodb status \G *************************** 1. row *************************** Type: InnoDB Name: Status: ===================================== 150202 17:15:33 INNODB MONITOR OUTPUT ===================================== Per second averages calculated from the last 19 seconds ----------------- BACKGROUND THREAD ----------------- srv_master_thread loops: 0 srv_active, 0 srv_shutdown, 8787 srv_idle srv_master_thread log flush and writes: 8787 ---------- SEMAPHORES ---------- OS WAIT ARRAY INFO: reservation count 3 OS WAIT ARRAY INFO: signal count 3 Mutex spin waits 2, rounds 60, OS waits 1 RW-shared spins 2, rounds 60, OS waits 2 RW-excl spins 0, rounds 0, OS waits 0 Spin rounds per wait: 30.00 mutex, 30.00 RW-shared, 0.00 RW-excl ------------ TRANSACTIONS ------------ Trx id counter 2305 Purge done for trx's n:o < 0 undo n:o < 0 History list length 0 LIST OF TRANSACTIONS FOR EACH SESSION: ---TRANSACTION 0, not started MySQL thread id 3, OS thread handle 0x7fe2a80c2700, query id 45 localhost root init show engine innodb status -------- 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 161 OS file reads, 5 OS file writes, 5 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 276707, node heap has 0 buffer(s) 0.00 hash searches/s, 0.00 non-hash searches/s --- LOG --- Log sequence number 1602871 Log flushed up to 1602871 Pages flushed up to 1602871 Last checkpoint at 1602871 0 pending log writes, 0 pending chkp writes 8 log i/o's done, 0.00 log i/o's/second ---------------------- BUFFER POOL AND MEMORY ---------------------- Total memory allocated 137363456; in additional pool allocated 0 Dictionary memory allocated 43148 Buffer pool size 8192 Free buffers 8042 Database pages 150 Old database pages 0 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 150, created 0, written 1 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: 150, 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 0 read views open inside InnoDB Main thread process no. 4294, id 140611110070016, state: sleeping Number of rows inserted 0, updated 0, deleted 0, read 0 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)
附:
Oracle 體系結構圖
聲明:本網(wǎng)頁內容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com