The 1th chapter of MongoDB installation
(Dawn Hello original works, reproduced please specify)
1.1 MongoDB Introduction
MongoDB is a database open source project based on distributed file storage. Written in the C + + language, it is designed to provide a high-performance, sustainable data storage solution for Web applications.
It is characterized by scalability, high performance, ease of use, freedom of mode, and easy storage of data.
1.1.1 Main functional characteristics
A) document-oriented storage: (class JSON data schema simple and powerful).
b) Efficient traditional storage: supports binary data and large objects such as photos and videos.
c) replication and automatic failover: The MONGO database supports data replication between servers, supporting master-slave mode and inter-server replication.
d) auto-sharding Auto-Shard supports cloud-scale scalability (in the early alpha phase): Automatic sharding supports a level of database clustering, adding additional machines dynamically.
e) Dynamic query: It supports rich query expressions. Query directives use a JSON-style tag to easily query objects and arrays embedded in the document.
f) Full index support: Includes embedded objects and arrays in the document. The query optimizer of MONGO parses the query expression and generates an efficient query plan.
g) Support multiple languages such as ruby,python,java,c++,php.
1.1.2 Applicable Scenarios
A) suitable for real-time insert, update and query, and with the application of real-time data storage required for replication and high scalability.
b) A persistent caching layer that is suitable as an information infrastructure.
c) suitable for databases consisting of dozens of or hundreds of servers. Because MONGO already contains built-in support for the MapReduce engine.
d) MONGO's Bson data format is ideal for storing and querying in a document format.
1.1.3 Not applicable scene
A) highly transactional system.
b) Traditional business intelligence applications.
c) level for complex SQL queries.
1.2 Windows Environment Installation 1.2.1 Download TAR package
mongodb-win32-x86_64-2.4.6.
1.2.2 Configuration file
Create a log folder
Java code
- MD D:\mongodb-win32-x86_64-2.4. 6 \log
Create a configuration file for the MongoDB logpath option:
Java code
- echo logpath=d:\mongodb-win32-x86_64-2.4. 6\log\mongo.log > d:\mongodb-win32-x86_64-2.4. 6\mongod.cfg
1.2.3 Manual Start
Start the service
Java code
- Mongod--dbpath D:\mongodb_data
- Or
- Mongod--config d:\mongodb-win32-x86_64-2.4. 6\mongod.cfg
1.2.4 Service startup
Install the MongoDB program as a Windows service.
Java code
- Installing MongoDB Services
- Mongod--config d:\mongodb-win32-x86_64-2.4. 6\mongod.cfg--install
- Running MongoDB Service
- net start MongoDB
- Removal service
- Mongod--remove
1.2.5 REST is not enabled
When the following error occurs
REST is not enabled. Use--rest to turn on. Check that port 28017 was secured for the network too.
Workaround:
When you start with the command line:
Java code
- Mongod--dbpath d:\mongodb_data--rest--port 27017
Reinstall System Services:
Java code
- Mongod--dbpath d:\mongodb_data--config d:\mongodb-win32-x86_64-2.4. 6\mongod.cfg--rest--port 27017--install
1.3 Linux Environment Installation 1.3.1 Download package tgz
Mongodb-linux-i686-2.4.6.tgz
1.3.2 Creating a user
Create user
Java code
- AddUser MongoDB
- passwd MongoDB
1.3.3 Creating a database file
Create a database file
Java code
- Mkdir-p/data/mongodb_data
- Chown Mongodb/data/mongodb_data–r
- Chgrp Mongodb/data/mongodb_data-r
1.3.4 configuration file
Create a log folder
Java code
- mkdir/opt/mongodb/mongodb-linux-i686-2.4. 6/logs
Create a configuration file for the MongoDB logpath option:
Java code
- echo/opt/mongodb/mongodb-linux-i686-2.4. 6/bin/mongod--dbpath/data/mongodb_data/--logpath/opt/mongodb/mongodb-linux-i686-2.4. 6/logs/mongodb.log--rest--port 27017
1.3.5 Manual Start
Start the service
Java code
- ./mongod--dbpath/data/mongodb_data
1.3.6 service startup
Install the MongoDB program as a Linux service.
Installing MongoDB Services
Java code
- echo/opt/mongodb/mongodb-linux-i686-2.4. 6/bin/mongod--dbpath/data/mongodb_data/--logpath/opt/mongodb/mongodb-linux-i686-2.4. 6/logs/mongodb.log--rest--port 27017–fork >>/etc/rc.local
1.4 Linked databases
Use the command line method and create the student database
Command line link disease Create a database
Java code
- >mongo
- >use Student
In the student database, create a user collection, insert a document
Insert a piece of data into the student database and query
Java code
- Db.user.save ({username: "Limingnihao", nickname: "Dawn Hello", Password: "123456"})
1.5 Operation commands
Function |
Command |
Entering and creating a database |
Use test |
Add or modify user passwords |
Db.adduser (' name ', ' pwd ') |
View the list of users |
Db.system.users.find () |
User authentication |
Db.auth (' name ', ' pwd ') |
Delete User |
Db.removeuser (' name ') |
View Master-slave replication status |
Db.printreplicationinfo () |
Repairing the database |
Db.repairdatabase () |
Copy Database |
Db.copydatabase ("MyDB", "temp", 127.0.0.1 "); |
Cloning a Database |
Db.clonedatabase ("127.0.0.1"); |
Delete the current database |
Db.dropdatabase () |
View the database currently in use |
Db.status () |
Database version |
Db.version () |
View All Users |
Show Users |
View all databases |
Show DBS |
See all Collection |
Show collections |
View Profiling |
Show profile |
Error message prior to query |
Db.getpreverror () |
Clear Error logging |
Db.reseterror () |
Create a Clustered collection (table) |
Db.createcollection ("Collname", {size:20, Capped:5, max:100}) |
Gets the Clustered collection (table) of the specified name |
Db.getcollection ("account") |
Get all the clustered collections of the current DB |
Db.getcollectionnames () |
Displays the status of all clustered indexes in the current DB |
Db.printcollectionstats () |
1.6 Data types
Data type |
Describe |
Distance |
Boolean value |
True or False |
True or false: TRUE or False |
32-bit integer |
A 32-bit integer. The shell does not support this type, and the shell is converted to a 64-bit floating-point number by default |
|
64-bit integer |
A 64-bit integer. The shell does not support this type, and the shell is converted to a 64-bit floating-point number by default |
|
64-bit floating point number |
64-bit floating-point number. The number in the shell is this type |
{"X": 3.14, "Y": 3} |
String |
UTF-8 string |
{"foo": "Bar"} |
Object ID |
12-byte Unique ID of the document |
{"id": ObjectId ()} |
Date |
The number of milliseconds starting from the standard era |
{"Date": New Date ()} |
Regular expressions |
A document can contain regular expressions that follow the syntax of JavaScript |
{"foo":/foobar/i} |
JS Code |
The document can contain JavaScript code |
{"X": Function () {}} |
Binary data |
Any byte binary string, Shell does not support |
|
Not defined |
Undefined |
{"X": undefined} |
Array |
A collection or list of values |
{"Arr": ["a", "B"]} |
Inline document |
A document can be used as the value of a key in a document |
{"X": {"foo": "Bar"}} |
Null |
Represents an empty value or undefined object |
{"X": null} |
OBJECTID type structure:
MongoDB installation to go