MongoDB Service Configuration Series Problems

Posted by nikkio3000 on Sun, 16 Jun 2019 20:07:18 +0200

MongoDB Service Configuration Series Problems

Configure services through configuration files:

dbpath=F:\Program\DATA\Mongo\db #Data Storage Path
logpath=F:\Program\DATA\Mongo\logs\mongo.log #Journal Storage Strength
logappend=true #Log Writing: Addition
port=27017 #port
#auth=true #Certification or not
bind_ip=0.0.0.0 
serviceName = MongoDB #Service Name
serviceDisplayName = MongoDB

1. Installation Profile:

In the bin directory of mongdb, execute the following commands as administrators in cmd:

mongod --install -f F:\Program\DATA\Mongo\mongodb.conf 

After executing this command, you can see the service name MongoDB in'This Computer - Management - Services and Applications - Services'.

2. Close, Start and Delete Services:

net stop MongoDB #Stop a service
net start MongoDB #Open a service
sc delete MongoDB #Delete a service sc delete "service name"  (If there is a space in the service name, you need to quote it back and forth.

Open or close login with username and password:

Auh = true or auth = false means whether password authentication is required to connect to the database, corresponding to the user name and password.

Here's how to turn on password authentication:

admin database is an administrator database, recording user information, we need to create an administrator user in this database. This administrator user can then add, modify or delete users of that database under other databases.

use admin
db.createUser(
... ...   {
... ...     user: "****",
... ...     pwd: "****",
... ...     roles: [ { role: "root", db: "admin" } ] #'root'has the highest privileges.
... ...   }
... ... )

Shut down service:

net stop MongoDB 

Modify the configuration file:

dbpath=F:\Program\DATA\Mongo\db #Data Storage Path
logpath=F:\Program\DATA\Mongo\logs\mongo.log #Journal Storage Strength
logappend=true #Log Writing: Addition
port=27017 #port
auth=true #holdfalse Change to true
bind_ip=0.0.0.0 
serviceName = MongoDB #Service Name
serviceDisplayName = MongoDB

Reassemble the resettlement file:

mongod --install -f F:\Program\DATA\Mongo\mongodb.conf

At this time, when entering the database for operation, found that there is no corresponding permission, the next step to login.

use admin
db.auth('username','password')
use test1 #Switch to database test1
#Create a user for test1
db.createUser(
...     {
...       user: "testuser",
...       pwd: "testpassword",
...       roles: [ { role: "read", db: "test1" }]
...     }

Connect mongodb in pymongo:

import pymongo
client = pymongo.MongoClient('localhost', connect=True)
client['test1'].authenticate('testuser','testpassword',mechanism="DEFAULT"). #The purpose of this step is landing. Landing successfully returned to TRUE.
db = client['test1'] #After successful login, connect to the database.
db.test.find_one() #query
db.test.insert_one({'b':2}) #Insert.#role determines the user's rights. If you are a read-only user, you cannot do this step.
for i in db.test.find():
    print(i)
client['test'].logout() #Exit the database.

After mongodb opened authentication, I found that I could not create a new database through pymongo.

#Give an example:
client = pymongo.MongoClient('localhost', connect=False)
db = client['jianshu']
db.table1.insert({'a':1}) #This step reported an error, indicating that there was no authenticated landing. But this is a new database, it has not been written to mongodb at all, how to authenticate.

There are two ways at this time:

  • One is to establish a database in the mongo terminal. After setting up the user, it connects with the user name and password in pymongo.
  • The second is to close the certification.

How to Close Authentication:

#Close the MongoDB service
net stop MongoDB
#Modify configuration files
auth = false
#Installation Profile
mongod --install -f F:\Program\DATA\Mongo\mongodb.conf
#Open Services:
net start MongoDB

User Authorization Related Operations

In which library to create a user, the user belongs to which library, and only has access to the library.

Mongodb's authorization uses the role authorization method, each role includes a set of privileges.

Database user roles: read, read Write;

Database management roles: dbAdmin, dbOwner, userAdmin;

Super User Roles: root // There are several other roles that indirectly or directly provide system superuser access (dbOwner, userAdmin, userAdminAnyDatabase)

1. Creating Users

db.createUser(
...     {
...       user: "test1",
...       pwd: "test1",
...       roles: [ { role: "dbOwner", db: "test1" }]
...     }

2. Modify password

db.changeUserPassword('testuser','1234') #Change the password of database test to 1234

3. Adding User Rights

db.grantRolesToUser(  "testuser",  [    { role: "read",db:"admin"}  ] )

4. Recovery of user privileges

db.revokeRolesFromUser("testuser",[    { role: "read",db:"admin"}  ] )

5. Delete Users

db.dropUser("testuser")

Topics: MongoDB Database