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)

[root@hadoop100 ~]# 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等命令)

[root@hadoop100 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下的目录:

[root@hadoop100 bin]# cd /usr/sbin/
[root@hadoop100 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(命令及配置文件)

[root@hadoop100 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)

[root@hadoop100 etc]# ls | grep my.cnf
my.cnf
my.cnf.d
my.cnf.rpmsave

数据库和文件系统的关系


InnoDBMyISAM这样的存储引擎都是把表存储在磁盘上,然后操作系统用来管理磁盘的结构称为文件系统,即: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_schemaperformace_schema结合起来,帮助系统管理员和开发人员监控MySQL性能。

2 数据库在文件系统中的表示

InnoDB

在/var/lib/mysql数据目录下,每个数据库都对应着一个文件夹,查看文件夹的内容如下:

image-20230116153439539

  • 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文件查看工具:

[root@hadoop100 dbtest1]# ibd2sdi --dump-file=emp1.txt emp1.ibd
[root@hadoop100 dbtest1]# ls
emp1.ibd  emp1.txt
[root@hadoop100 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;


然后在文件目录中查看表对应的文件:

[root@hadoop100 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证书和密钥文件