Introduction to MongoDB:
MongoDB is a database based on distributed file storage. Written by C++ language. It aims to provide scalable high-performance data storage solutions for WEB applications.
Related Links:
Official website: http://www.mongodb.org/
Baidu Encyclopedia: http://baike.baidu.com/view/3385614.htm
PHP plug-in: http://cn2.php.net/manual/zh/book.mongo.php
NoSQL: http://baike.baidu.com/view/2677528.htm
Features:
High performance, easy to deploy, easy to use, and very convenient to store data. The main features are:
Collection-oriented storage, easy to store data of object type.
Pattern free.
Support dynamic query.
Full indexing is supported, including internal objects.
Support inquiries.
Supports replication and failover.
Use efficient binary data storage, including large objects (such as video, etc.).
Automatically handle fragmentation to support cloud-level scalability
Drivers for Python, PHP, Ruby, Java, C, C#, Javascript, Perl and C++ are supported, and drivers for Erlang and .NET platforms are also provided in the community.
The file storage format is BSON (an extension of JSON).
Accessible via the web.
Function:
Collection-oriented storage: suitable for storing data in the form of objects and JSON.
Dynamic query: Mongo supports rich query expressions. The query command uses JSON format tags, which can easily query the embedded objects and arrays in the document.
Full indexing support: including document embedded objects and arrays. Mongo’s query optimizer analyzes query expressions and generates an efficient query plan.
Query Monitoring: Mongo includes a monitoring tool for analyzing the performance of database operations.
Replication and automatic failover: Mongo database supports data replication between servers, master-slave mode and mutual replication between servers. The main goal of replication is to provide redundancy and automatic failover.
Efficient traditional storage: supports binary data and large objects (such as photos or pictures)
Auto-sharding to support cloud-level scalability: Auto-sharding supports horizontal database clusters where additional machines can be added dynamically.
Applicable occasions:
Website data: Mongo is very suitable for real-time insertion, update and query, and has the replication and high scalability required for real-time data storage on the website.
Caching: Due to its high performance, Mongo is also suitable as a caching layer for information infrastructure. After the system restarts, the persistent cache layer built by Mongo can avoid the underlying data source
overload.
Large size, low-value data: It may be expensive to store some data using traditional relational databases. Before that, programmers often choose traditional files for storage.
Highly scalable scenarios: Mongo is ideal for databases consisting of tens or hundreds of servers. Mongo’s roadmap already includes built-in support for the MapReduce engine.
Used for object and JSON data storage: Mongo’s BSON data format is very suitable for storage and query in document format.
Environment construction:
Install the MongoDB database:
Step 1: Download the installation package: Official download point: http://www.mongodb.org/downloads, pay attention to select the corresponding operating system to download
Step 2: Create a new directory: “D:\MongoDB”, unzip the downloaded installation package, find all *.exe files in the bin directory, and copy them to the newly created directory
Step 3: Create a new folder “data” in the “D:\MongoDB” directory, which will be the root directory for data storage
Configure MongoDB server:
Open the CMD window and enter the following command:
> d:
> cd D:\MongoDB
> mongod –dbpath D:\MongoDB\data
After the configuration is successful, enter http://localhost:27017/ in the browser to see the following prompt, which means that the MongoDB database service has been successfully started:
You are trying to access MongoDB on the native driver port. For
http diagnostic access, add 1000 to the port number
* This CMD window cannot be closed. After closing, the database service will also be closed. Every time you use it in the future, you need to restart it
* Open http://127.0.0.1:28017/ to see the running status of mongodb
* For the convenience of not having to start mongod every time in the future, you can register mongod as a windows service
MongoDB register windows service:
Step 1: Open the CMD window and enter the following command
> d:
> cd D:\MongoDB
> mongod -dbpath D:\MongoDB\data -logpath D:\MongoDB\log -install
** -logpath must exist in versions above 1.4, otherwise the registration cannot be successful, the location of the log cannot be stored in the same directory as the data file, and the data directory and LOG directory need to be created manually, this command cannot create the corresponding file by itself Folder, otherwise the registration will not be successful
Step 2: At this time mongod.exe cannot be started randomly, we need to modify the registry, the command to enter the registry is
regedit, search after entering the registrymongodb, find an item named ImagePath, change install to service, and mongod.exe can start normally
** ImagePath may be slightly different in different versions, it may be imgstc, etc.
** Mongo Shell: Open the CMD window, enter the directory where mongodb is located, and manage it:
> d:
> cd D:\MongoDB
> mongo // will output Mongo Shell version information
PHP driver installation:
Driver download address: https://github.com/mongodb/mongo-php-driver/downloads
Step 1: Download the corresponding driver, decompress it, select the correct “php_mongo.dll” file and put it into the “ext” folder of PHP
Step 2: Modify php.ini, add extension=php_mongo.dll, and restart the MongoDB service.
The above content is excerpted from the book and collected from the Internet