Hardening MongoDB Recommendation: Modify the database default port to add Database access permissions:
- Startup database (Bare ben): C:\mongodb\bin>mongod--dbpath C:\MongoDB\data (also specify data storage location as "DB" folder with--dbpath. )
- Database management: Mongo.exe
- The new version of MongoDB has not supported the AddUser method, changed to CreateUser.
Considerations for Starting a database:
- Specify port to start the database (no Authentication required): E:\mongodb\bin>mongod--dbpath E:\MongoDB\data--port=27017
- Specify port to start the database (requires authentication): E:\mongodb\bin>mongod --auth --dbpath E:\MongoDB\data--port=27017
Login database: (name:root;pwd:root)
Log on Locally:
- Specify Port Logon database: C:\mongodb\bin>mongo--port=27017
- Username Password Login: c:\mongodb\bin>mongo-u root-p root--port=27017
- Login to DB1 database: C:\mongodb\bin>mongo db1-u root-p Root--port=27017
Telnet
- Connecting to a remote database: E:\mongodb\bin>mongo ip:27017/db-u root-p Root
Some commands:
- Show DBS (or use Show databases to view the current database situation, by default, test)
- Use test if test does not exist create test database, show DBS cannot see, need to insert data
- Db.test.insert ({"AA": "11"})
- DB view which database is currently connected (Db.test2.insert () creates test2 data table and inserts data under the current database)
- Db.test.find () View the test data table data for the current database
Create a user name and password as root administrator (created under current db)
Roles role, the role is assigned after the appropriate permissions, generally defined in the admin role in other places with
- Db.createuser ({User: "root", pwd: "Root", Customdata:{name: "Root"},roles:[{role: "Useradminanydatabase", DB: "Admin"} ]})
- Db.createuser ({User: "Root4", pwd: "Root", Customdata:{name: "Root"},roles:[]})
Simple version:
- Db.createuser ({User: "Root5", pwd: "Root", customdata:{},roles:[]})
Login after creation
1. direct command login;
2.mongo suffix plus username password login;
3.mongo remote login;
4.robomongo Visual management software login.
- Db.auth (' root ', ' root ')
Modify User Password
- Use admin
- Db.changeuserpassword ("username", "xxx")
View user Information
- Db.runcommand ({usersinfo: "UserName"})
Modify password and user information
- Db.runcommand ({updateUser: "username", pwd: "xxx", Customdata:{title: "XXX"})
To add access rights to a database: (auth)
Steps to resolve:
1) Start the database without the--auth parameter, so you don't need an account to connect to MongoDB.
2) to create a new role, such as sysadmin, you need to switch to the Admin library to do the following:
> Use admin
Switched to DB admin
> db.createrole({role: ' Syadmin ', roles:[],
privileges:[
{resource:{anyresource:true},actions:[' anyaction '}
]})
3) Then, create a new user, use this role, note that the role of the DB is admin, the operation is as follows:
> Use woplus
Switched to DB Woplus
> db.createuser({
User: ' Root ',
PWD: ' Root ',
roles:[
{role: ' Syadmin ', db: ' admin '}
]})
Okay, now restart the startup database, and with--auth, you can do it properly.
Node server-side configuration:
var mongoose = require (' Mongoose ')
var opts = {server: {socketoptions: {keepalive:1}}}
Connection Address
Mongoose.connect ('mongodb://uname:[email protected]:27017/db', opts);
var db = mongoose.connection;
Output on the console
Db.on (' Error ', () =>{console.error (' Connection Database error ')})
Db.once (' Open ', () = {Console.log (' connection succeeded! ‘)})
Db.help ()
DB methods:
1) Db.admincommand (nameordocument)-Switches to ' admin ' db, and runs command [just calls Db.runcommand (...)]
2) Db.auth (username, password)
3) db.clonedatabase (fromhost)
4) db.commandhelp (name) returns the Help for the command
5) Db.copydatabase (Fromdb, Todb, Fromhost)
6) db.createcollection (name, {size: ..., capped: ..., max: ...})
7) Db.createview (name, Viewon, [{$operator: {...}}, ...], {viewoptions})
8) Db.createuser (userdocument)
9) Db.currentop () displays currently executing operations in the DB
Ten) Db.dropdatabase ()
One) db.eval ()-Deprecated
Db.fsynclock () flush data to disk and lock server for backups
Db.fsyncunlock () unlocks server following a Db.fsynclock ()
Db.getcollection (CNAME) Same as db[' cname ') or db.cname
Db.getcollectioninfos ([filter])-Returns a list that contains the names and options of the DB ' s collections
() Db.getcollectionnames ()
Db.getlasterror ()-Just returns the Err Msg string
Db.getlasterrorobj ()-Return Full status object
) db.getlogcomponents ()
Db.getmongo () Get the Server connection object
Db.getmongo (). Setslaveok () Allow queries on a replication slave server
Db.getname ()
) Db.getpreverror ()
) db.getprofilinglevel ()-Deprecated
Db.getprofilingstatus ()-Returns if profiling is on and slow threshold
() Db.getreplicationinfo ()
DB.GETSIBLINGDB (name) get the db at the same server as this one
Db.getwriteconcern ()-Returns the write concern used for any operations on this db, inherited from server object if Set
Db.hostinfo () Get details about the server ' s host
Db.ismaster () Check replica primary status
Db.killop (Opid) kills the current operation in the DB
Db.listcommands () lists all the DB commands
Db.loadserverscripts () loads all the scripts in Db.system.js
Db.logout ()
) Db.printcollectionstats ()
Db.printreplicationinfo ()
Notoginseng) Db.printshardingstatus ()
) Db.printslavereplicationinfo ()
Db.dropuser (username)
Db.repairdatabase ()
) Db.reseterror ()
Db.runcommand (cmdobj) run a database command. If Cmdobj is a string, turns it into {cmdobj:1}
Db.serverstatus ()
Db.setloglevel (level,<component>)
Db.setprofilinglevel (level,<slowms>) 0=off 1=slow 2=all
Db.setwriteconcern (<write concern doc>)-sets the write concern for writes to the DB
Db.unsetwriteconcern (<write concern doc>)-Unsets The write concern for writes to the DB
Db.setverboseshell (flag) display extra information in shell output
) Db.shutdownserver ()
() Db.stats ()
Db.version () Current version of the server
Original reprint of Spring Thunder Please specify: http://www.cnblogs.com/chunlei36
MongoDB database Add permissions and simple database commands operation notes