How to use MySQL to learn the data storage structure of MongoDB

Source: Internet
Author: User
Tags install mongodb

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:

 
 
  1. [Root @ localhost db] # ll/data/db/
  2. Total 196844
  3. -Rw ------- 1 root 16777216 04-15 16:33 admin.0
  4. -Rw ------- 1 root 33554432 04-15 16:33 admin.1
  5. -Rw ------- 1 root 16777216 04-15 16:33 admin. ns
  6. -Rw ------- 1 root 16777216 04-21 :30 foo.0
  7. -Rw ------- 1 root 33554432 04-21 :30 foo.1
  8. -Rw ------- 1 root 67108864 04-21 :30 foo.2
  9. -Rw ------- 1 root 16777216 04-21 :30 foo. ns
  10. -Rwxr-xr-x 1 root 6 04-21 17:16 mongod. lock
  11. -Rw ------- 1 root 16777216 04-15 16:30 test.0
  12. -Rw ------- 1 root 33554432 04-15 16:30 test.1
  13. -Rw ------- 1 root 16777216 04-15 16: 30 test. ns
  14. Drwxr-xr-x 2 root 4096 04-21 17:30 _ tmp
  15. [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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.