Reference Links:
https://docs.mongodb.com/manual/reference/method/db.createUser/#create-administrative-user-with-roles
Installing MongoDB under Linux
Download:
Curl-o https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz
Extract:
TAR-ZXVF mongodb-linux-x86_64-3.0.6.tgz
Copy the unpacked package to the specified directory:
MV mongodb-linux-x86_64-3.0.6//usr/local/mongodb
MongoDB's executable file is located in the bin directory, so you can add it to your path path:
Export Path=<mongodb-install-directory>/bin: $PATH
Create a database directory
Mkdir-p/data/db
MongoDB provides a simple HTTP user interface. If you want to enable this feature, you need to specify the parameter--rest at boot time.
Mongod--dbpath=/data/db--rest
2. Create an administrator user
Db.createuser (
{
User: "AppAdmin",
PWD: "Password",
Roles
[
{role: "ReadWrite", db: "Config"},
"Clusteradmin"
]
}
)
3. Create a user with a role
Db.createuser (
{
User: "Accurme",
PWD: "Accurme",
roles:["ReadWrite", "DbAdmin"]
})
4. Connection database-uri connection syntax:
Mongodb://[username:[email protected]]host1[:p ort1][,host2[:p Ort2],... [, hostn[:p Ortn]] [/[database][?options]]
mongodb://this is a fixed format that must be specified.
Username:[email protected] option, if set, after connecting to the database server, the driver will attempt to log into this database
Host1 must specify at least one host, and Host1 is the only URI to be filled in. It specifies the address to connect to the server. If you are connecting to a replica set, specify multiple host addresses.
Portx Optional specified port, if not, default is 27017
/database If you specify Username:[email protected], connect and verify the login to the specified database. If not specified, the test database is opened by default.
The options is the connection option. If you do not use/database, you need to add/. All connection options are key-value pairs Name=value, and key-value pairs are separated by & or; (semicolon)
Mongodb://example:[email Protected]:27017/accurme
5. Create a database
Use database_name;
6. View all databases
Show DBS;
7. Deleting a database
Use test
Db.dropdatabase ()
8. View collections in the Database (table)
Show tables;
9. Deleting a collection in a database (table)
Deleted the collection from the Runoob database site
Use Runoob
Db.site.drop ()
MongoDB Note 1