recently the company has a project to use MongoDB, not before, today's temporary sharpening, spent half a day to study, in the middle have encountered several pits written to share to everyone.
TAR-ZXF mongodb-linux-x86_64-v3.6.3.tgz
CP-PR Mongodb-linux-x86_64-v3.6.3/usr/local/mongodb
Mkdir-p/data/mongodb/logs
Touch/data/mongodb/logs/mongodb.log
Vi/data/mongodb/bin/mongodb.conf
Dbpath=/data/mongodb/data
Logpath=/data/mongodb/logs/mongodb.log
port=27017
Fork=true
Auth=true
Logappend=true
Nojournal=true
Nohttpinterface=true
/usr/local/mongodb/bin/mongod--bind_ip 172.31.31.63-f/usr/local/mongodb/conf/mongodb.conf
echo "Export path=/usr/local/mongodb/bin: $PATH" >>/etc/profile
Source/etc/profile
Connect to MongoDB:
MONGO 172.31.31.63:27017
Show DBS //View database
Use admin //Switch to Admin library and create admin if admin Library does not exist
Show Users //view user
Db.createuser ({User: "admin", pwd: "123456", roles:["root"]}); //Create Super Admin user
Db.createuser ({User: "Usradm", pwd: "123456", roles:["Useradminanydatabase"]}); //Create user admin account
Db.changeuserpassword (' admin ', ' 147258 '); //Change Password
Db.dropuser (' admin ') //delete user
Db.auth ("admin", "147258"); //admin login Authentication
Db.auth ("Usradm", "123456"); //usradm login Authentication
#############################################################################################
Built-in roles
1. Database User Role
READ: Allows the user to read the specified database
ReadWrite: Allows the user to read and write to the specified database
2. Database Administrator Role
DbAdmin: Allows users to create, delete, view statistics or access system.profile without roles and user-managed permissions
Useradmin: Provides the ability to create and modify roles and users in the current database
Dbowner: Provides the ability to perform any administrative operation on the database. This role combines the privileges granted by the ReadWrite, Dbadmin, and useradmin roles
3. Cluster Management Role
Clusteradmin: Provides the most powerful access to cluster management. The ability to combine Clustermanager, Clustermonitor, and hostmanager roles. The dropdatabase operation is also available
Clustermanager: Provides management and monitoring operations on the cluster. Access to the configuration and local databases, respectively, for sharding and replication
Clustermonitor: Provides read-only access to monitoring tools, such as MongoDB Cloud Manager and OPS manager Monitoring agent
Hostmanager: Provides the ability to monitor and manage servers.
4. Backup Recovery Role
Backup: Provides the capabilities required to back up data using the MongoDB Cloud manager back up agent, OPS Manager backup agent, or using Mongodump
Restore: Provides the capabilities needed to recover data using Mongorestore
5. All database Roles
Readanydatabase: Only available in the Admin database, giving users read access to all databases
Readwriteanydatabase: Only available in the Admin database, giving users read and write access to all databases
Useradminanydatabase: Only available in the Admin database, giving the user useradmin permissions for all databases
Dbadminanydatabase: Only available in the Admin database, giving the user dbadmin permissions for all databases
6. Super User Role
Root: Provides access to all resources for Readwriteanydatabase, Dbadminanydatabase, Useradminanydatabase, clusteradmin, restore, and backup
7. Internal role
__system: Provides privileges for any operation of any object in the database
#############################################################################################
Create a user backupuser and give the backup recovery role
Db.createuser ({User: "Backupuser", pwd: "123456", Roles: [
{role: ' Backup ', DB: ' admin '},
{role: ' Restore ', db: ' admin '}
]});
Use students //CREATE DATABASE
Db.createuser ({User: "user1", pwd: "user123456", roles: [{role: "ReadWrite", DB: "Students"}]}); //Create students library read/write permissions user
Db.auth ("user1", "user123456");
Db.clas.insert ({"Name": "Wang"}); Insert 3 data into Clas collection (table)
Db.clas.insert ({"Name": "Zhang"});
Db.clas.insert ({"name": "Li"});
Db.clas.remove ({}); //Delete all data records in Clas collection
Db.clas.find (); //Query Clas all data records in the collection
Db.clas.drop (); //Delete Clas collection (table)
Show collections; //View collection (table)
Db.dropdatabase (); //Delete database
To export the data for the students Library:
Mongodump-h 172.31.31.63:27017--authenticationdatabase admin-d students-u backupuser-p-o/root/test/
-H Host IP: Port
-D libraries to back up
-u user with Backup role
-p Backup User's password
-O Backup directory path
--authenticationdatabase backing up the database that the user Backupuser belongs to
Import data for the students Library:
Mongorestore-h 172.31.31.63:27017--authenticationdatabase admin-d students-u backupuser-p--directoryperdb/root/tes T/students
-H Host IP: Port
-D Recovery Target Library
-u user with restore role
-P Recover User's password
--directoryperdb Recovering directory paths
--authenticationdatabase recovering the database that the user Backupuser belongs to
Pits to note:
1. Authentication issues, such as above I have an admin user, only use admin switch to the admin library below to authenticate successfully.
2. For example, I have created a USRADM user and assigned the role, the current role it has to give the user all the database useradmin permissions. Now I want to create a user and configure the Useradmin role for the students library. At this point, if I execute under the admin library, it will fail and you must switch to the students library to succeed.
3. The user who backs up the recovery role can only be created under the Admin library, and will be able to perform a backup to recover all other libraries after creation.
!!! Remember that you must switch to the corresponding library when you perform any library operation.
MongoDB installation configuration and other underlying operations