MongoDB Boot configuration, etc.

Source: Internet
Author: User
Tags deprecated mongodump mongorestore

Directory:

One . MongoDB Boot configuration

Two . Export , import , run-time backup

Three . Fsync lock , data repair

Iv. . User Management , Security certification

First, the startup item Mongod--help

C:\windows\system32>mongod--help

1. Common Configuration Items

--dbpath

Specifies the directory for the database , which is c:\data\db\ by default under Window

--port

Specifies the port number on which the server listens , by default, 27017

--fork

Start MongoDB in a daemon way

--logpath

Specifies the output path of the log , which is the default console

--config

Specify the path to the file used for the startup item

--auth

Start the database with secure authentication (MongoDB is not enabled by default)

2. Use config configuration file to start database change port to 8888

① creating a mongodb.config file

Specify the directory for the database file and the port number to use

DBPath = D:\MongoDBData

Port = 8888

② input mongod--config D:\MongoDB\mongodb.config enable configuration

③ Start MongoDB

CMD input MONGO 127.0.0.1:8888/admin

3. Stop mongoDB service

①Ctrl + C key combination to close the database

②Admin Database command to close data

Second, export , import , run-time backup

1. Export Data ( interrupts other operations )

The Mongoexport tool in MongoDB can export a collection to a file in JSON or CSV format. You can specify the exported data item by parameter, or you can export the data based on the specified criteria.

Description of Export command options:

    • -H: Indicates the IP of the database host
    • -U: Indicates the user name of the database
    • -P: Indicates the password for the database
    • -D: Indicates the name of the database
    • -C: Indicates the name of the collection
    • -F: Indicates which columns to export
    • -O: Indicates the file name to be exported
    • -Q: Indicates the filtering criteria for the exported data

Example :

① export the Students in the database Mongodbtest

Use the mongoexport command to export data, note that the host and port are not added , the default is host=127.0.0.1,port=27017

Finally, see if the exported path has a file that has just been manipulated.

The default is to export the JSON, but you can also specify the export csv file, which will be combined using the --csv parameter and the- F parameter

The first reason for failure is that you do not specify the columns you want to export, so you use- f to specify them.

2. Import data (interrupt other operations)

The Mongoimport tool in MongoDB can import content from a particular format file into the specified collection. The tool can import JSON-formatted data or import CSV-formatted data

Description of the Import command options:

    • -H: Indicates the IP of the database host
    • -U: Indicates the user name of the database
    • -P: Indicates the password for the database
    • -D: Indicates the name of the database
    • -C: Indicates the name of the collection
    • -F: Indicates which columns to import

Example:

① Importing document Students into the mongodbtest database

Remove the Students from the mongodbtest first

> Db. Students.drop ()

Import the students.cvs file just now

Parameter description:

--Type: indicates the file format to import

--Headerline: indicates that the first row is a column name and does not need to be imported

--file: Indicates the files to be imported

View results of Import completion

3.MongoDB of Backup

Use Mongodump to make a library or table-level backup of MongoDB

    • -H: Indicates the IP of the database host
    • -U: Indicates the user name of the database
    • -P: Indicates the password for the database
    • -D: Indicates the name of the database
    • -C: Indicates the name of the collection
    • -O: Output directory
    • -q:json query (JSON query)

Cases:

Backing Up the Mongodbtest database

c:\windows\system32>mongodump-d Mongodbtest-o D:\mongoDBTest

D:\mongoDBTest is the directory where backup data is stored

Recovery of 4.MongoDB

Use Mongorestore to do mongodb library or table-level data recovery

    • -U: Indicates the user name of the database
    • -P: Indicates the password for the database
    • -D: Indicates the name of the database
    • -C: Indicates the name of the collection

Cases:

Delete the mongodbtest and then restore

> Db.dropdatabase ()

{"Dropped": "Mongodbtest", "OK": 1}

Recovery:

C:\windows\system32>mongorestore-d mongodbtest D:\mongoDBTest\mongoDBTest

Third ,Fsync Lock and data repair

Why there is the concept of lock, this and MongoDB is the implementation mechanism, in the database and user read and write directly have a buffer pool, when the user to back up the database only the content of the backup, buffer pool content is not backed up, then the role of the lock is reflected, After locking, you can put the buffer pool data into the database first, thus guaranteeing the integrity of the data.

2. Locking and unlocking

Note: Lock and unlock to switch to the admin database

locked

Db.runcommand ({fsync:1,lock:1});

Unlock

Db.currentop ()

3. Data Repair

When an irreversible disaster such as a power outage arrives , because the storage structure of MongoDB generates garbage data , the garbage data still exists after the data recovery, which is the ability of the database to provide a self-healing . It's easy to use.

Db.repairdatabase ()

Iv. User Management and security certification

1. Add a user

Db.adduser ("name"," password ");

2. Enable users

Db.auth ("name","password")

3. security check --auth

Cases:

① Create a user that belongs to the admin database

> Db.adduser ("Li", "123");

Warning:the ' AddUser ' shell Helper is DEPRECATED. Please use ' createUser ' instead

Successfully added User: {"user": "Li", "Roles": ["Root"]}

② Create a database mongodbtest user

> Use Mongodbtest

Switched to DB Mongodbtest

> Db.adduser ("Licheng", "123");

Warning:the ' AddUser ' shell Helper is DEPRECATED. Please use ' createUser ' instead

Successfully added User: {"user": "Licheng", "Roles": ["Dbowner"]}

③ first enable security check

Modify the Mongodb.config as follows:

DBPath = D:\MongoDBData

auth=true           

Start MongoDB service

C:\windows\system32>mongod--config D:\MongoDB\mongodb.config

④Test user non-mongodbtest is not able to manipulate the database

Log in with the test user and switch to the mongodbtest database to view the contents of the Students document

C:\windows\system32>mongo 127.0.0.1:27017/test

MongoDB Shell version:2.6.5

Connecting To:127.0.0.1:27017/test

> Use Mongodbtest

Switched to DB Mongodbtest

> DB. Students.find ()

Error: {"$err": "Not authorized for query on Mongodbtest.students", "Code": 13}

>

⑤ switch Users ( enable their own users to access )

> Db.auth ("Licheng", "123")

1

> DB. Students.find ()

{"_id": 1, "name": "Zhao", "age": +, "country": "USA", "Sex": "M"}

{"_id": 2, "name": "Qian", "Age": $, "country": "USA", "Sex": "M"}

{"_id": 3, "name": "Sun", "age": +, "country": "USA", "Sex": "M"}

{"_id": 4, "name": "Li", "age": +, "country": "China", "Sex": "}

{"_id": 5, "name": "Zhou", "age": +, "country": "China", "Sex": "}

{"_id": 6, "name": "Wu", "Age": "Country": "Japan", "Sex": "}

{"_id": 7, "name": "Zheng", "Age":, "Country": "UK", "Sex": "}

{"_id": 8, "name": "Wang", "age": +, "country": "Korea", "Sex": "}

{"_id": 9, "name": "Xu", "age": +, "country": "Japan", "Sex": "}

>

⑥ database commands are not available to users of non-admin databases

Continue to use Licheng users

> Show DBS

2016-03-28t11:47:21.698+0800 listdatabases failed:{

"OK": 0,

"ErrMsg": "Not authorized in Admin to execute command {listdatabases:1.0}",

"Code": 13

} at src/mongo/shell/mongo.js:47

>

The data in the ⑦ Admin database is certified as an administrator user

Switch to admin User Login, and then enable Li user

C:\windows\system32>mongo 127.0.0.1:27017/admin

MongoDB Shell version:2.6.5

Connecting To:127.0.0.1:27017/admin

> Db.auth ("Li", "123")

1

> Show DBS

Admin 0.078GB

Local 0.078GB

Mongodbtest 0.078GB

>

⑧. User Delete action

Db.system.users.remove ({User: "Licheng"});

MongoDB Boot configuration, etc.

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.