MongoDB database maintenance operations: Connection & amp; User Management & amp; backup, mongodb Database

Source: Internet
Author: User
Tags mongodb server mongodump mongo shell mongorestore

MongoDB database maintenance operations: Connection & amp; User Management & amp; backup, mongodb Database
Start/Close the mongodb ServiceStart the serviceThe mongodb service can be started directly by specifying the startup parameter or by using the configuration file ;?

# Specify the startup parameter to start mongodb
$ mongod --dbpath=/usr/bin/mongodb-3.6.2/data --logpath=/usr/bin/mongodb-3.6.2/logs/mongo.log --appendlog ? 
?
# Specify the configuration file to start mongodb
$ mongod -f /etc/mongod.conf
?
# Hybrid mongodb startup
$ mongod -f /etc/momgod.conf --auth 
The configuration is similar to the following, similar to the format of the properties file. You can obtain these configuration parameters and descriptions through mongod -- help :?
dbpath=/usr/bin/mongodb-3.6.2/data
journal=true
logpath=/usr/bin/mongodb-3.6.2/logs/mongod.log
logappend=true
bind_ip=127.0.0.1
port=27017
auth=true
Close serviceIf the -- fork parameter is not used when starting the service, you can exit the terminal directly at the front end. If the -- fork command is used to run the service in the background, you need to send shutdownServer () to the server () to close the database:
?
> use admin
> db.shutdownServer()

The command for connecting to mongodb on the terminal is as follows: Use the specified user password to connect to the specified database; mongo-u user_name-p password file_name?
# Directly log on to mongo
$ mongo ? 
?
# Use the administrator password to log on to mongodb on the local default port (27017) as the administrator.
$ mongo -u assad -p mongo123 localhost/admin
?
# Use the user password (user = vancy, passowrd = 4252233) to log on to the mongodb database at the specified address (127.233.22.33: 2233/article)
$ mongo -u vancy -p mongo2233 127.233.22.33:2233/article
? Is the same as mysql when I exit the database connection terminal? Exit command;
Mongo shell actually uses JavaScript as the interpretation engine, so it can run simple js Code, or even create methods and define variables ;?
> 1 + 1
2
> var num = 2333
> num + 1000
3333
After the user manages mongodb by default, there is no administrator user, and you need to manually add it. mongodb users are associated with the database and are authorized in a certain database, it must be verified in a database. Except for the admin database, authorization can be used in any database. authorization and verification between other common databases are independent of each other. Db. createUser ()Create a user in the current database. This function contains the following key-value pairs: User: User name. The value is a string;
Pwd: User password. The value is a string;
Roles: User permission rule. The value is an array. Each array item contains the following key-value pairs: Role: Permission rules; Db: Specifies the database for which permission rules are used. This parameter can be omitted. The default role is used for the current database;
The role option is as follows: Database User role
Read: Read-Only permission; ReadWrite: Read and write permissions; database management role DbAdmin: Database Management permissions (including executing management functions (such as creating and deleting an index set), viewing statistics, and accessing system. profile) UserAdmin: User Management permissions (allow reading and writing the system. users set, creating, deleting, and managing users ); DbOwner: ReadWrite, dbAdmin, and userAdmin permissions;
All Database roles ReadAnyDatabase, readWriteAnyDatabase, userAdminAnyDatabasem, dbAdminAnyDatabaseCluster Management Role ClusterAdmin, ClusterManager, clusterMonitor, and hostManager;Backup and recovery role Backup and restoreSuperuser: Root: Super permission, with all permissions;
Where? ReadAnyDatabase, readWriteAnyDatabase, userAdminAnyDatabasem, dbAdminAnyDatabase, clusterAdmin, and root permissions can only be authorized in the admin database;
A simple example is as follows :?
# Connecting to the test Database
> use test ? ? 
?
# Create an administrator account under the test database and have management permissions on the test Database
> db.createUser({
...  user:"admin1",
...  pwd:"ad123",
... Roles: [{role: "dbAdmin", db: "test"}]? # Replace it with roles: ["dbAdmin"]
})
?
?
# Create a common user under the test database and grant only the read and write permissions to the test Database
> db.createUser({ 
... ?  user:"user1", 
... ?  pwd:"1234",
...? Roles: [{role: "readWrite", db: "test"}]? # Replace it with roles: ["readWrite"]
... })
Users who want to manage non-current databases can only do so in the admin database, but the user created in the admin database is saved in the system. users set of the admin database, as follows :?
> use admin
# Create user3 in the admin database and have the read and write permissions of the testdb database and the management permissions of the testdb2 database.
> db.createUser({
... user:"user3",
... pwd:"123",
... roles:[ { role:"readWrite", db:"testdb"},
 ? ? ? ? ?  { role:"dbAdmin", db:"testdb2" } ]
... })

User verification has enabled the -- auth startup parameter (run in Authentication Mode) in the mongodb service. users need to perform verification when accessing a database. There are two login methods for users, one is to directly log on to a database using the user identity, and the other is to use the user identity authentication during use ;?
# Directly log on to the local database testdb using the ueer3 identity
$ mongo -u user3 -p 123 localhost/testdb
> ....
?
# User authentication during use
$ mongo
> use testdb
> show collections
2012-2-13t21: 30: 11.107 + 0800 e query? [Thread1] Error: listDatabases failed :....? # The current identity does not have the permission to view testdb
> Db. auth ('user3', '123 ')? ? # Verify the identity of the user3 user
> use testdb
> show collection
.....

View user information usage ? Db. system. users. find ()The user information that can be viewed by the function. Note that the user who uses this function must have the user management information. The user information returned by this function is in the compressed format. If you want to view the complete format, can you use it? Db. system. users. find (). pretty ()??
> use test
# View all user information of the test Database
> db.system.users.find().pretty()
....
# View the assad user information in the test Database
> db.system.users.find({user:'assad'}).pretty()
....
User information modification is implemented by updating the db. systems. users set. Note that users using this function must have user-managed information ;? 
# Update the password of user1 in the test Database
> use test
> db.system.users.update({user:'user1'},{$set:{'pwd':'mongo123'}})

You can delete a user by removing the db. systems. users set. Note that the user who uses this function must have the information managed by the user;
?
# Deleting user1 users in the test Database
> use test
> db.system.users.remove({user:'user1'})



Backup and recovery data backup for mongodb data backup can use the built-in MongodumpTool, and the prototype of the tool call is as follows :?
$ mongodump -h dbhost -d dbname -o dbdirectory 
-H: the address of the mongodb server. You can specify an ip address or ip Address: port, for example, 127.0.0.1 or 127.0.0.1: 27017;
-D: name of the database to be backed up, for example, test;
-O: path for storing backups;
Note that the mongodump tool can be used only after the mongo service is started. If the mongo Service sets the auth Authentication mode, the-u-p parameter is also required to specify users with relevant permissions. Example:
?
# Backing up the test database
$ mongodump -h localhost:27017 -d /usr/local/bin/mongodb/data/test/ -o /usr/backup/mongodb/
# Back up all databases
$ mongodump -h localhost:27017 -d /usr/local/bin/mongodb/data/ -o /usr/backup/mongodb/
Data Recovery for mongodb data backup can use the built-in MongorestoreTool, and the prototype of the tool call is as follows:
?
$ mongodump -h dbhost -d dbname [--drop ] [--dir ] backpath 
-H, -- host: the address of the mongodb server. You can specify an ip address or ip: port, for example, 127.0.0.1 or 127.0.0.1: 27017;
-D, -- db: name of the database to be backed up, for example, test. The name can be different from the name used in the original backup;
-- Drop: Delete the current data and restore the backup data.
-- Dir: the location of the backup data, for example,/user/share/backup/test. -- Dir can be omitted, but the path must be placed at the end of the command;Note that the mongodump tool can be used only after the mongo service is started. If the mongo Service sets the auth Authentication mode, the-u-p parameter must be used to specify users with relevant permissions;
?
# Recovering the test database from the backup
$ mongorestore -h lcoalhsot:27017 -d /usr/local/bin/mongodb/data/test/ /usr/backup/mongodb/
# Recovering all databases from the backup
$ mongorestore -h lcoalhsot:27017 -d /usr/local/bin/mongodb/data/ /usr/backup/mongodb/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.