In the above article, we learned how to use MySQL to learn how to install MongoDB. In this article, we will continue our learning journey and learn its data storage structure.
1. MySQL Data Storage Structure
Each MySQL database is stored in a folder with the same name as the database. the MySQL database files include the database files created by MySQL and the database files created by the storage engine used by MySQL.
If MySQL uses the MyISAM storage engine, the database file types include. frm,. MYD,. MYI
If MySQL uses the InnoDB Storage engine, the database file types include. frm, ibdata1, And. ibd.
(1) database files:
. Frm file: Framework Structure of the data table. The file name of the MySQL database is the same as that of the table. Each table corresponds to an frm file of the same name, which is irrelevant to the operating system and storage engine, that is, no matter what operating system MySQL runs on and what storage engine is used, this file is available. Apart from the necessary. frm files, the storage engine creates different database files based on the storage engine used by MySQL (two storage engines commonly used by MySQL are MyISAM and InnoDB.
(2) MyISAM database table file:
(3) InnoDB uses tablespaces:
Ibdata1 and ibdata2: System tablespace MySQL database files, which store InnoDB system information and user database table data and indexes, and are shared by all tables;
. Ibd file: a single table space file. Each table uses a tablespace file per table to store user database table data and indexes;
Log Files: ib_logfile1 and ib_logfile2.
2. MongoDB Data Storage Structure
MongoDB is a new product for domestic users. It is like a black box, but if you know more about its internal data storage, you will soon understand and control MongoDB, let it play a greater role.
The default data directory of MongoDB is/data/db, which stores all MongoDB data files. In MongoDB, each database contains a. ns file and some data files, and these data files will become more and more as the data volume increases. Therefore, if there is a database named foo in the system, the file that makes up the foo database will be composed of foo. ns, foo.0, foo.1, foo.2, and so on. The details are as follows:
- [Root @ localhost db] # ll/data/db/
- Total 196844
- -Rw ------- 1 root 16777216 04-15 16:33 admin.0
- -Rw ------- 1 root 33554432 04-15 16:33 admin.1
- -Rw ------- 1 root 16777216 04-15 16:33 admin. ns
- -Rw ------- 1 root 16777216 04-21 :30 foo.0
- -Rw ------- 1 root 33554432 04-21 :30 foo.1
- -Rw ------- 1 root 67108864 04-21 :30 foo.2
- -Rw ------- 1 root 16777216 04-21 :30 foo. ns
- -Rwxr-xr-x 1 root 6 04-21 17:16 mongod. lock
- -Rw ------- 1 root 16777216 04-15 16:30 test.0
- -Rw ------- 1 root 33554432 04-15 16:30 test.1
- -Rw ------- 1 root 16777216 04-15 16: 30 test. ns
- Drwxr-xr-x 2 root 4096 04-21 17:30 _ tmp
- [Root @ localhost db] #
MongoDB has a table space pre-allocated mechanism. Each pre-allocated file is filled with 0. Because of this mechanism, MongoDB always maintains extra space and spare data files, this effectively avoids the disk pressure caused by the Data surge.
As the amount of data in the table increases, the size of each data file is twice the size of the previous data file, and the maximum size of each data file is 2 GB. This mechanism can prevent small databases from wasting too much disk space and ensure that large databases have reserved space.
Each table in the database corresponds to a namespace, and each index also has a corresponding namespace. The metadata of these namespaces is concentrated in the *. ns file.
In, the foo database contains three files used to store table and index data. The foo.2 file is a pre-allocated empty file. Foo.0 and foo.1 data files are divided into different namespaces for the corresponding disk areas.
Shows the relationship between the namespace and the disk area. Each namespace can contain multiple different disk areas, which are not consecutive. As data files grow, the size of the Disk Area corresponding to each namespace increases with the number of times allocated. The purpose is to balance the space wasted by the namespace and maintain data continuity in a namespace. There is also a namespace to note: $ freelist, which is used to record the disk areas that are no longer in use (the deleted Collection or index ). Whenever the namespace needs to be allocated a new disk area, you will first check whether $ freelist has a suitable Disk Area to use.