MongoDB is a database based on distributed file storage. It is a product between relational database and non-relational database. It is the most functional and most like relational database among non-relational databases. The data structure it supports is very loose, and it can store more complex Data Types. The most important feature of MongoDB is that it supports a very powerful query language. Its syntax is similar to an object-oriented query language. It can almost realize most of the functions of a single table query similar to a relational database, and it also supports the establishment of indexes on data.
At present Supports MongoDB databases above 2.4. MongoDB 2.0 and MongoDB 3.0 are started in the same way when the service is started in the validation mode and non-validation mode, but the way is different when the database is created. The following is a Detail Description.
When using the MongoDB database, you need to start the MongoDB service to be connected. The following describes the MongoDB service based on the mongodb-win32-i386-2.6.6 version.
Start MongoDB Service
- When the service is started, there is no permission verification by default. After logging in, you can operate the database arbitrarily. If the mongod. Exe is in the "C: \ MongoDB \ bin" folder, the start command is:
mongod.exe --dbpath C:\MongoDB\data --logpath C:\MongoDB\log\log.txt。
- When the service is started with the authentication parameter, it indicates that the database is started with user authentication. If an ordinary user is created in the database and verified, any operation can only be performed on this database, and no operation can be performed on other databases; if a user is verified on the admin database, any operation can be performed on all databases in the system. The startup command is:
mongod. Exe --dbpath C: \ MongoDB \ data --logpath C: \ MongoDB \ log \ log. Txt -auth.
- Users can be added to or deleted from the database only in the following two cases. The first case is to start the service in a non-authentication mode. The second case is to start the service in a user authentication mode and add users to the admin database and perform user authentication.
- When the Start Service reports an error related to Unclean shutdown detected ", it may be caused by abnormal shutdown of the service, which can be repaired and restarted. The command is as follows:
mongod. Exe --repair --dbpath C: \ MongoDB \ data.
MongoDB can be started in two ways: non-user authentication and user authentication. The Connection Info Parameter Settings of the database are different in different ways. The specific description is as follows:
Create Database
- When the MongoDB service is started in the non-user authentication mode and the Database Name to be created is entered in the "Database Name", the Username, Password, and confirmation password must be blank before the database can be created successfully; If the user name and password of New Database are set, the database creation fails.
- When the MongoDB service is started in the user authentication mode, enter the Database Name in the "Database Name" field. The database must be created through the command in advance. If the entered user name and password are incorrect, or the database name does not exist, the database Connection failed. There are the following ways to create the test database through the command Create Database User:
- Starts the service
- in unauthenticated mode, executed on the Mongo. Exe command links
use test
db.addUser("map","map")
Restart MongDB service - with authentication:
mongod. Exe --dbpath C: \ MongoDB \ data --logpath C: \ MongoDB \ log \ log. Txt -auth.
After - starting the MongoDB service, the user map can operate the test of the database.
- in unauthenticated mode, executed on the Mongo. Exe command links
The above is the method of creating database for MongoDB 2.0, while the method of creating data for MongDB 3.0 by user authentication is different. Take mongodb-win32-v3.4.0 as an example:
- Start the service
- in non-authentication mode, and execute it on the command line of the Mongo. Exe:
use admin
db.createUser({user:'admin',pwd:'admin',roles:{role:"userAdminAnyDatabase", db:"admin"}})
db.system.version.update({'_id':'authSchema'},{$set:{'currentVersion':3}})
db.system.version.find()
use test
db.createUser({user:'map',pwd:'map',roles:{role:"dbOwner",db:"test"}});
Restart MongDB service - with authentication:
mongod. Exe -- setParameter authentication Mechanisms = MONGODB-CR -- auth
- This completes the creation of the test database and creates a map user for the database with the password map.
To sum up, if the MongDB database is used in the authentication mode, the Database Name in the database Connection Info is test, and the user name and password are map; map respectively.
Open Data Library
The connection Set Parameters of Open Datasource are inconsistent after the service is started in the user authentication mode and the non-user authentication mode, as shown in the following table:
MongoDB startup mode | Does the admin database have user | Does the normal database have users? | Whether to enter the user name and password | Is connectable |
No user authentication | × |
× |
× |
√ |
× |
× |
√ |
× |
|
× |
√ |
× |
√ |
|
× |
√ |
√ |
√ |
|
√ |
× |
× |
√ |
|
√ |
× |
√ |
× |
|
√ |
√ |
× |
√ |
|
√ |
√ |
√ |
√ |
|
User authentication | × |
× |
× |
× |
× |
× |
√ |
× |
|
× |
√ |
× |
× |
|
× |
√ |
√ |
√ |
|
√ |
× |
× |
× |
|
√ |
× |
√ |
× |
|
√ |
√ |
× |
× |
|
√ |
√ |
√ |
√ |