User management:
Add Users and give permissions
Db.createuser ({User: "Hy", pwd: "123", Roles:[{role: "Useradmin", DB: "Test"}]})
The 3.4 version of the MongoDB configuration file /etc/mongod.conf
is, in the format Yaml. Click to view official documents
Turn on user authentication
Click to view the official guide
1. Add Users
Start MongoDB:
mongod
Start the MongoDB shell
mongo
Add Users:
use admindb.createUser( { user: "myUserAdmin", pwd: "abc123", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })
2. Change the configuration file
In the configuration file, you will
#security:
In the #
minus, and on its next line, empty two spaces, plus authorization: enabled
, as follows
security: authorization: enabled
Note: authorization: enabled
Add a space after the colon, which is required by the YAML syntax
3. Restart the MongoDB database
sudo service mongod restart
Note: The command is mongod
, notmongodb
Setting up remote connections
Note: The old version of the GUI management software may not support the new version of the database, please ensure that the software is available before testing
In the configuration file, you will
bindIp: 127.0.0.1
Comment out (that is, the first addition to the sentence #
)
#bindIp: 127.0.0.1
Restart database
sudo service mongod restart
Change Data Catalog
The default data store directory is /var/lib/mongodb
, assuming our destination path is/home/mongodb
Close the database
sudo service mongod stop
Set the item to the destination path in the configuration file storage.dbPath
, as modified below
storage: dbPath: /home/mongodb
Copy the files from the original directory to the new directory:
/var/bin/mongodb/* /home/mongodb/
To mongod.lock
start a database after deleting a file
sudo rm /home/mongodb/mongod.locksudo service mongod start
Testing remote connections
Attention:
When your mongo
command error occurs, when the connection fails, try deleting the file in the Datastore directory and restart the mongod.lock
MongoDB database
Management of MongoDB