First, install mongdb
1. Create an installation directory
# mkdir /data/local # mkdir /data/local/mongodbdata
2, extract the installation package
# tar -xvf /software/mongodb-linux-x86_64-2.4.10.tar -C /usr/local # cd /usr/local # mv mongodb-linux-x86_64-2.4.10 mongdb
Second, the configuration mongdb
1, create a profile
# cd /usr/local/mongodb/ # vi mongodb.conf
Configuration is as follows:
port=27017 dbpath=/data/local/mongodbdata logpath=/data/local/mongodbdata/mongodb.log logappend=true fork=true
2, set the boot
# vim /etc/rc.local
New reads as follows:
# add for mongodb /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf
Third, start mongdb
1, run mongdb
# cd /usr/local/mongodb/ # ./bin/mongod --config ./mongodb.conf
2, view the service port, confirm that normal service starts
# netstat -anltp | grep mongod tcp 0 0 0.0.0.0:28017 0.0.0.0:* LISTEN 2011/./bin/mongod tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 2011/./bin/mongod
Fourth, new subscribers
1, start Client
#cd /usr/local/mongdb/bin # ./mongo MongoDB shell version: 3.0.6 connecting to: test Server has startup warnings: 2019-11-27T20:47:21.338+0800 I STORAGE [initandlisten] 2019-11-27T20:47:21.338+0800 I STORAGE [initandlisten] ** WARNING: Readahead for /data/local/mongodbdata is set to 4096KB 2019-11-27T20:47:21.338+0800 I STORAGE [initandlisten] ** We suggest setting it to 256KB (512 sectors) or less 2019-11-27T20:47:21.338+0800 I STORAGE [initandlisten] ** http://dochub.mongodb.org/core/readahead 2019-11-27T20:47:21.507+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2019-11-27T20:47:21.507+0800 I CONTROL [initandlisten] 2019-11-27T20:47:21.507+0800 I CONTROL [initandlisten] 2019-11-27T20:47:21.507+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2019-11-27T20:47:21.507+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2019-11-27T20:47:21.508+0800 I CONTROL [initandlisten] 2019-11-27T20:47:21.508+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2019-11-27T20:47:21.508+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2019-11-27T20:47:21.508+0800 I CONTROL [initandlisten] >
2, the new user
#新增admin读写权限 > use admin; switched to db admin > db.createUser({user:"admin",pwd:"test.1234",roles:[{role: "readWrite", db: "admin"}]}) Successfully added user: { "user" : "admin", "roles" : [ { "role" : "readWrite", "db" : "admin" } ] } #新增超级管理员用户 >use admin; >db.createUser({user:"admin",pwd:"test.1234",roles:[{role: "root", db: "admin"}]}) Successfully added user: { "user" : "admin", "roles" : [ { "role" : "root", "db" : "admin" } ] }
3, delete users
>db.system.users.remove({user:"admin"})
Five, mongdb database role
1, built-in character
database user roles: read, readWrite;
database administration roles: dbAdmin, dbOwner, userAdmin;
cluster management roles: clusterAdmin, clusterManager, clusterMonitor, hostManager
backup and recovery role: backup, restore;
all database roles: readAnyDatabase, readWriteAnyDatabase, userAdminAnyDatabase, dbAdminAnyDatabase
superuser roles: root // there are several direct or indirect role provides system super user access (dbOwner, userAdmin, userAdminAnyDatabase)
internal roles: system
role Description:
the read: allows users to read the specified database
readWrite: allows the user to specify the database to read and write
dbAdmin: allows users to perform management functions specified in the database, such as index creation, delete, view statistics or access system.profile
useradmin: allowing a user to write to system.users, you can find the specified database create, delete, and manage user
clusterAdmin: admin is only available in the database, giving users administrative rights to all fragments and replication set-correlation function.
readAnyDatabase: admin is only available in the database, giving all users read access to the database
readWriteAnyDatabase: only available in the database admin, read and write access gives the user all databases
userAdminAnyDatabase: admin is only available in the database, the user gives permission userAdmin all databases
dbAdminAnyDatabase: admin is only available in the database, the user gives permission dbAdmin all databases.
root: admin is only available in the database. Super accounts, super powers
2, the user operates
1, the new administrator user
>use admin > db.createUser( { user: "adminUser", pwd: “admin123”, roles: [{role: ”userAdminAnyDatabase”,db:”admin”}] } ) #结果 > Successfully added user: { "user" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
2. Create a regular user
> use foo > Show View Roles can Roles # > db.createUser ( { User: "simpleUser", pwd: "123456", Roles: [ "readWrite", "dbadmin", "useradmin"] } )
3, view the user already exists
>db.system.users.find()
4, delete users
>db.system.users.remove({user:”simpleUser”})
Note: When a user operation, when the service starts mongod try not authorized to open
3, open verification
--Auth specified at startup that is required authorization to operate
# Enable Services > --dbpath the mongod --auth / Home / user1 / MongoDB / --logpath the Data / Home / user1 / MongoDB / log / logs --fork # client connection and authentication > mongo > use foo > db.auth ( "simpleUser", "123456" )
After that only certified to operate data