WeChat public account: [Front end cook in a pot]
A little technique, a little thinking.
For questions or suggestions, please leave a message on the official account.
Related commands
Check the native mongodb version: mongo -version
Check the mongodb running on this machine: ps aux | grep mongodb
Connect to the local database: mongo localhost:27017
Use account password to connect to the local database: mongo -port 27017 -u 'admin' -p 'admin_root_test'
Display the database list: show dbs
Switch/create database: use mytest
Delete the current database: db.dropDatabase()
Create collection: db.createCollection('book')
Display all current users: show users
Delete user: db.dropUser('myread')
Kill the running mongodb: kill pid
Read-only permission settings
- Start the mongodb service
mac:mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log --fork
linux: systemctl start mongod
or service mongod start
- Entry command
mongo localhost:27017
- Create an administrator account
use admin
db.createUser({user:'admin', pwd:'admin_root_test',roles:[{ role: 'root', db: 'admin' }]})
- Close mongodb
db.adminCommand( { shutdown: 1 } )
or
ps -ef | grep mongodb // 查看 momgodb pid
kill pid
- Reopen with authorization
mongod --auth --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log --fork
or
vi /etc/mongod.conf
security:
authorization: enabled
- Enter the command again
mongo -port 27017 -u 'admin' -p 'admin_root_test'
- Create a read-only user
use mytest // 创建数据库
db.createCollection('book') // 创建集合,以方便 show dbs 能显示数据库
db.createUser({ user: 'myread', pwd: 'myread_pwd', roles: [{ role: 'read', db: 'mytest' }] })
Interpretation of role permissions
Built-In Roles:
- Database user roles: read, readWrite.
- Database management roles: dbAdmin, dbOwner, userAdmin.
- Cluster management roles: clusterAdmin, clusterManager, clusterMonitor, hostManager.
- Backup and restore roles: backup, restore.
- All database roles: readAnyDatabase, readWriteAnyDatabase, userAdminAnyDatabase, dbAdminAnyDatabase.
- Super user role: root has several roles indirectly or directly providing system super user access (dbOwner, userAdmin, userAdminAnyDatabase).
- Internal role: __system.
Specific role:
- Read: Allow users to read the specified database.
- readWrite: Allow users to read and write the specified database.
- dbAdmin: Allows users to perform management functions in the specified database, such as index creation, deletion, viewing statistics or access.
- userAdmin: Allow users to write to the system.users collection, and create, delete and manage users in the specified database.
- clusterAdmin: It is only available in the admin database. It gives the user the management authority of all shards and replication set related functions.
- readAnyDatabase: Only available in the admin database, grants users read permissions for all databases.
- readWriteAnyDatabase: Only available in the admin database, giving the user read and write permissions for all databases.
- userAdminAnyDatabase: Only available in the admin database, giving the user the userAdmin authority for all databases.
- dbAdminAnyDatabase: It is only available in the admin database and gives the user dbAdmin permissions for all databases.
- root: Only available in admin database. Super account, super authority.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。