Objective
MongoDB is a distributed file store-based database written in the C + + language and is a popular one in today's NoSQL databases, designed to provide scalable, high-performance data storage solutions for Web applications.
Mongodb
Brief introduction
MongoDB is a product between a relational database and a non-relational database, and is the most versatile and most like relational database in a non-relational database. The supported data structures are very loose, so you can store more complex data types. The most important feature is that its supported query language is very powerful, its syntax is somewhat similar to object-oriented query language, almost can achieve similar relational database single-table query most of the functions, but also support the indexing of data.
Features and functional characteristics
Features: High performance, easy to deploy, easy to use, easy to store data
The main features are:
Collection-oriented storage for easy storage of object-type data
Mode freedom
Support Dynamic Query
Full index support, including internal objects
Support Query
Support for replication and failure recovery
Use efficient binary data storage, including large objects (such as video, etc.)
Automatically process fragmentation to support scalability at the cloud level
Supports multiple languages such as ruby,python,java,c++,php
File storage format is Bson (an extension of JSON)
Accessible over the network
Advantages and Disadvantages
The advantages of MongoDB compared to non-relational databases are:
Weak consistency (final and consistent), to ensure the user's access speed
The document structure is stored in a way that makes it easier to get data
Built-in Gridfs to support high-capacity storage
Built-in sharding
Third party support Rich (this is the advantage of MongoDB compared to other NoSQL)
Superior performance
The disadvantages of MongoDB compared to non-relational databases:
Transactional operations are not supported
Too much space occupied
No Mature maintenance tools
MongoDB Installation
1. Download MongoDB
wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-3.2.1.tgz
2. Unzip the tar file
tar zxf mongodb-linux-x86_64-3.2. 1 . tgz MV mongodb-linux-x86_64-3.2. 1//usr/local/mongodb-3.2
3. Creating data files and log files
mkdir -p/usr/local/mongodb-3.2/datamkdir /usr/local/mongodb-3.2/ logs Touch /usr/local/mongodb-3.2/logs/mongod.log
4. Under the user who installed MongoDB, add the following environment variables to directly use the commands in the MongoDB bin directory
Export path= $PATH:/usr/local/mongodb-3.2/bin/
5. Start MongoDB
Mongod--dbpath=/usr/local/mongodb-3.2/data--logpath=/usr/local/mongodb-3.2/logs/mongod.log --logappend --port=27017&
6. Check if the port is up, port: 27017
grep 27017 TCP 0 0 0.0. 0.0:27017 0.0. 0.0:* LISTEN 1897/mongod Unix 2 [ACC] STREAM LISTENING 11098 1897/mongod /tmp/mongodb-27017. Sock
Started successfully.
7. Connect to the database
# MONGO> Use test
8. Set up MongoDB Auto-start (no permissions)
Add the following command to the/etc/rc.local
Mongod--dbpath=/usr/local/mongodb-3.2/data--logpath=/usr/local/mongodb-3.2/logs/mongod.log-- Logappend --port=27017&
9. Set up MongoDB Auto-start (with permissions)
Add the following command to the/etc/rc.local
Mongod--dbpath=/usr/local/mongodb-3.2/data--logpath=/usr/local/mongodb-3.2/logs/mongod.log-- Logappend --port=27017&-auth
10. Add Users
> Use test // does not exist automatically created > Db.createuser ({User:"admin",pwd :"123456", Roles:[{role:'dbowner', DB:' userdb'}]})> Use admin // switch to admin library under > Db.system.users. Find () // see which users
MongoDB Connectivity and user management
1) Connect MongoDB:
1: You can run the command MONGO directly into the MongoDB shell in this machine
2: If the MongoDB listener port is not the default 27017, you need to add--port option when connecting, for example
27018
3: Connect remote MongoDB, need to add--host, for example
127.0. 0.1
4: If authentication is set, the user name and password are required when connecting
MONGO-UUSERNAME-PPASSWD // This is quite like MySQL .
2) MongoDB User Management
1: First MongoDB user is for the library, the establishment of the user must first enter into the corresponding library
2:use test//Switch to test library
3:db.createuser ({User: "admin", pwd: "123456", Roles:[{role: ' Dbowner ', db: ' UserDB '}]})
4:use Admin//Switch to Admin library
5:db.system.users.find ()//list all users, need to switch to admin library
6:show users//View all user under current library
7:db.dropuser (' admin ')//delete user
8: About user roles, refer to document Http://bbs.51cto.com/thread-1146654-1.html
3) database Management
#查看版本
Db.version ()
#显示当前的库 DB
# Toggle/Create Library
Use USERDB//If inventory is switched on, it does not exist to create
#此时show DBS does not see the USERDB, we need to create a collection
Db.createcollection (' Clo1 ')
Again show DBS there is UserDB.
#查看所有数据库
Show DBS
#删除数据库 MyDB
Use MyDB//switch to the library first, and then delete
Db.dropdatabase ()
4) Data Management
Create a Collection
Db.createcollection ("mycol"truetrue614280010000 } )
Syntax: Db.createcollection (name,options)
Name is the name of the collection, options are optional, to configure the parameters of the collection, the parameters are as follows
Capped True/false (optional) If true, the CAP collection is enabled. A capped collection is a fixed-size collection that automatically overwrites the oldest entry when it reaches its maximum size. If True is specified, the dimension parameter needs to be specified as well.
Autoindexid True/false (optional) If true, the default value for automatically creating an index _id field is false.
A size (optional) specifies a maximum size byte cap set. If the cap is true, then you also need to specify this field. Unit B
Max (optional) specifies the maximum number of files allowed in the capping collection.
View Collections
Show collections
Add a document to the collection
Db. Account.insert ({AccountID:2, UserName:"123", Password:"123456 "}) // If the collection does not exist and the data is inserted directly, MongoDB will automatically create the collection to modify the db. Account.update ({AccountID:2},{"$set": {"age ":+}})
View
Db. Account. Find () // View all document db. Account. Find ({AccountID:2}) // Query by Condition Delete the db. Account.remove ({AccountID:2}) // Delete the entire document DB according to the conditions. Account.drop () View the status of the collection use dbname// enter the corresponding library first, then view the collection status db.printcollectionstats ()
5) Database Performance
Db.stats () // View information about the current library db.serverstatus () // View the status of the MongoDB server
MongoDB installation Deployment (i)