MySQL(三)資料目錄
Mysql的主要目錄結構
find / -name mysql
1 資料庫檔案的存放路徑 /var/lib/mysql/
MySQL伺服器程式會在啟動的時候去檔案系統下的某個目錄載入一些檔案,之後在執行過程中產生的資料也會儲存到這個目錄下的某個檔案中,這個目錄就稱為資料目錄。
可以通過命令檢視當前資料庫的資料目錄:
mysql> show variables like 'datadir';
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| datadir | /var/lib/mysql/ |
+---------------+-----------------+
1 row in set (0.01 sec)
[[email protected] ~]# ll /var/lib/mysql
總用量 90568
-rw-r-----. 1 mysql mysql 56 1月 15 19:38 auto.cnf
-rw-r-----. 1 mysql mysql 4094 1月 15 21:55 binlog.000001
-rw-r-----. 1 mysql mysql 16 1月 15 19:39 binlog.index
-rw-------. 1 mysql mysql 1676 1月 15 19:38 ca-key.pem
-rw-r--r--. 1 mysql mysql 1112 1月 15 19:38 ca.pem
-rw-r--r--. 1 mysql mysql 1112 1月 15 19:38 client-cert.pem
-rw-------. 1 mysql mysql 1676 1月 15 19:38 client-key.pem
-rw-r-----. 1 mysql mysql 196608 1月 15 21:54 #ib_16384_0.dblwr
-rw-r-----. 1 mysql mysql 8585216 1月 15 19:38 #ib_16384_1.dblwr
-rw-r-----. 1 mysql mysql 5985 1月 15 19:38 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 1月 15 21:54 ibdata1
-rw-r-----. 1 mysql mysql 12582912 1月 15 19:39 ibtmp1
drwxr-x---. 2 mysql mysql 4096 1月 15 19:39 #innodb_redo
drwxr-x---. 2 mysql mysql 187 1月 15 19:39 #innodb_temp
drwxr-x---. 2 mysql mysql 143 1月 15 19:38 mysql
-rw-r-----. 1 mysql mysql 25165824 1月 15 21:54 mysql.ibd
srwxrwxrwx. 1 mysql mysql 0 1月 15 19:39 mysql.sock
-rw-------. 1 mysql mysql 6 1月 15 19:39 mysql.sock.lock
drwxr-x---. 2 mysql mysql 8192 1月 15 19:38 performance_schema
-rw-------. 1 mysql mysql 1680 1月 15 19:38 private_key.pem
-rw-r--r--. 1 mysql mysql 452 1月 15 19:38 public_key.pem
-rw-r--r--. 1 mysql mysql 1112 1月 15 19:38 server-cert.pem
-rw-------. 1 mysql mysql 1676 1月 15 19:38 server-key.pem
drwxr-x---. 2 mysql mysql 28 1月 15 19:38 sys
-rw-r-----. 1 mysql mysql 16777216 1月 15 21:54 undo_001
-rw-r-----. 1 mysql mysql 16777216 1月 15 21:54 undo_002
2 相關命令目錄 /usr/bin/mysql /usr/sbin/mysql
bin下的命令(mysqladmin、mysqlbinlog、mysqldump等命令)
[[email protected] bin]# ls | grep mysql
mysql
mysqladmin
mysqlbinlog
mysqlcheck
mysql_config_editor
mysqld_pre_systemd
mysqldump
mysqldumpslow
mysqlimport
mysql_migrate_keyring
mysqlpump
mysql_secure_installation
mysqlshow
mysqlslap
mysql_ssl_rsa_setup
mysql_tzinfo_to_sql
mysql_upgrade
sbin下的目錄:
[[email protected] bin]# cd /usr/sbin/
[[email protected] sbin]# ls | grep mysql
mysqld
安裝目錄下非常中要的bin命令目錄,它裡面儲存了許多關於控制客戶端程式和伺服器程式的命令(許多可執行檔案,如mysql、mysqld、mysqld_safe等)。而資料目錄是用來儲存mysql執行過程中產生的資料。
3 配置檔案目錄 /usr/share/mysql-8.0(命令及配置檔案) /etc(如my.conf)
/usr/share/mysql-8.0(命令及配置檔案)
[[email protected] mysql-8.0]# ll
總用量 1032
drwxr-xr-x. 2 root root 24 1月 15 19:34 bulgarian
drwxr-xr-x. 2 root root 4096 1月 15 19:34 charsets
drwxr-xr-x. 2 root root 24 1月 15 19:34 czech
drwxr-xr-x. 2 root root 24 1月 15 19:34 danish
-rw-r--r--. 1 root root 25575 9月 13 09:15 dictionary.txt
drwxr-xr-x. 2 root root 24 1月 15 19:34 dutch
drwxr-xr-x. 2 root root 24 1月 15 19:34 english
drwxr-xr-x. 2 root root 24 1月 15 19:34 estonian
drwxr-xr-x. 2 root root 24 1月 15 19:34 french
drwxr-xr-x. 2 root root 24 1月 15 19:34 german
drwxr-xr-x. 2 root root 24 1月 15 19:34 greek
drwxr-xr-x. 2 root root 24 1月 15 19:34 hungarian
-rw-r--r--. 1 root root 3999 9月 13 09:15 innodb_memcached_config.sql
-rw-r--r--. 1 root root 2216 9月 13 11:45 install_rewriter.sql
drwxr-xr-x. 2 root root 24 1月 15 19:34 italian
drwxr-xr-x. 2 root root 24 1月 15 19:34 japanese
drwxr-xr-x. 2 root root 24 1月 15 19:34 korean
-rw-r--r--. 1 root root 624277 9月 13 09:15 messages_to_clients.txt
-rw-r--r--. 1 root root 378839 9月 13 09:15 messages_to_error_log.txt
-rw-r--r--. 1 root root 1977 9月 13 11:45 mysql-log-rotate
drwxr-xr-x. 2 root root 24 1月 15 19:34 norwegian
drwxr-xr-x. 2 root root 24 1月 15 19:34 norwegian-ny
drwxr-xr-x. 2 root root 24 1月 15 19:34 polish
drwxr-xr-x. 2 root root 24 1月 15 19:34 portuguese
drwxr-xr-x. 2 root root 24 1月 15 19:34 romanian
drwxr-xr-x. 2 root root 24 1月 15 19:34 russian
drwxr-xr-x. 2 root root 24 1月 15 19:34 serbian
drwxr-xr-x. 2 root root 24 1月 15 19:34 slovak
drwxr-xr-x. 2 root root 24 1月 15 19:34 spanish
drwxr-xr-x. 2 root root 24 1月 15 19:34 swedish
drwxr-xr-x. 2 root root 24 1月 15 19:34 ukrainian
-rw-r--r--. 1 root root 1248 9月 13 11:45 uninstall_rewriter.sql
/etc(如my.conf)
[[email protected] etc]# ls | grep my.cnf
my.cnf
my.cnf.d
my.cnf.rpmsave
資料庫和檔案系統的關係
像InnoDB、MyISAM這樣的儲存引擎都是把表儲存在磁碟上,然後作業系統用來管理磁碟的結構稱為檔案系統,即:InnoDB、MyISAM這樣的儲存引擎都是把表存放在檔案系統上,當我們需要讀資料,這些儲存引擎就會從檔案系統中獲取資料返回給我們,當我們想寫入資料,這些儲存引擎又會把這些資料寫入檔案系統。
1 mysql預設的資料庫
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
-
mysql:MySQL系統自帶的核心資料庫,儲存mysql使用者及其許可權等資訊
-
information_schema:這個資料庫儲存著MySQL伺服器
維護的所有其他資料庫的資訊,比如有哪些表、哪些檢視、哪些觸發器、哪些列、哪些索引等資訊。 -
performace_schema:MySQl自帶的資料庫,主要儲存MySQL最近執行的一些狀態資訊,可以用來
監控MySQL服務的各類效能指標,包括最近執行了哪些語句,在執行過程中的每個階段花費了多長時間,記憶體的使用情況等資訊 -
sys:MySQl自帶的資料庫,主要是通過檢視的形式,把
information_schema和performace_schema結合起來,幫助系統管理員和開發人員監控MySQL效能。
2 資料庫在檔案系統中的表示
InnoDB
在/var/lib/mysql資料目錄下,每個資料庫都對應著一個資料夾,檢視資料夾的內容如下:
- opt檔案:記錄資料庫的字符集、比較規則等資訊
- frm:記錄資料庫表中的結構資訊
- ibd:
獨立表空間,每個資料庫中的表對應一個檔案,用於儲存表中的資料
5.7版本及之前的版本,資料還會存放到/var/lib/mysql/idata1檔案中,這個檔案獨立於資料庫,預設大小12M,是系統表空間
8.0版本後就沒有frm了,和ibd合併成一個,表結構和表資料統一存放在ibd檔案裡面了
ibd檔案的檢視 ibd2sdi --dump-file=emp1.txt emp1.ibd
ibd2sdi 是8.0版本後提供的一個ibd檔案檢視工具:
[[email protected] dbtest1]# ibd2sdi --dump-file=emp1.txt emp1.ibd
[[email protected] dbtest1]# ls
emp1.ibd emp1.txt
[[email protected] dbtest1]# cat emp1.txt
MyISAM
首先使用MYISAM引擎建立一張表:
CREATE TABLE `student_myisam` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`name` varchar(64) DEFAULT NULL,
`age` int DEFAULT NULL,
`sex` varchar(2) DEFAULT NULL,
PRIMARY KEY(`id`)
) ENGINE=MYISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
然後在檔案目錄中查看錶對應的檔案:
[[email protected] dbtest1]# ll
總用量 136
-rw-r-----. 1 mysql mysql 114688 1月 15 23:57 emp1.ibd
-rw-------. 1 root root 11112 1月 15 23:58 emp1.txt
-rw-r-----. 1 mysql mysql 4335 1月 16 00:31 student_myisam_366.sdi
-rw-r-----. 1 mysql mysql 0 1月 16 00:31 student_myisam.MYD
-rw-r-----. 1 mysql mysql 1024 1月 16 00:31 student_myisam.MYI
看到這裡MYISAM引擎建立的表有三個檔案,5.7中為frm、MYD和MYI,8.0為sdi、MYD、MYI,其中frm和sdi儲存表結構,MYD和MYI分別儲存資料和索引。
3 檢視在檔案系統中的表示
會儲存一個檢視名.frm的檔案,不會存在.ibd檔案,側面印證了檢視只是一個虛擬的表,也就是某個查詢語句的一個別名而已
4 資料目錄下的其他檔案
- 伺服器程序檔案
- 伺服器日誌檔案
- 預設/自動生成的SSL和RSA證書和金鑰檔案
