Mongo management and mongo management tools

Source: Internet
Author: User
Tags mongodump mongorestore

Mongo management and mongo management tools
Mongo Management

Based on ipv3.0, and 2.x versions may not be the same in some places.

Install source (ubuntu)
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.listapt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10apt-get updateapt-get install -y mongodb-org
Log on to mongo on mac

The default environment variables under mac will cause login failure and the following error is reported:

Root @ iZ28ywqw7nhZ :~ # Mongo
Failed global initialization: BadValue Invalid or no user locale set. Please ensure LANG and/or LC _ * environment variables are set correctly.

Set the environment variables:

export LC_ALL=Cmongo 
Start Stop start

Script method:

/etc/init.d/mongod start/etc/init.d/mongod stop

Command line:

Start from the configuration file: nohup mongod-f/etc/mongod. conf & stop: mongo use admin db. shutdownServer ()
Stop

Script:

Service producer D stop

Command line:

Use admin
Db. shutdownServer ()

If none of the above are valid, use

Killall-15 mongod

Note that you do not need to use kill-9.-9 will cause database corruption.

Mongodb repair

It's easy to add the-repair parameter at startup.
Mongod-dbpath/data/db-repair
Abnormal restart or kill-9 can cause mongodb database damage.

Install and install the php driver
pecl install mongo
Install the python driver
Apt-get install python-pippip install pydeskroot @ iZ28ywqw7nhZ :~ # PythonPython 2.7.3 (default, Dec 18 2014, 19:10:20) [GCC 4.6.3] on linux2Type "help", "copyright", "credits" or "license" for more information. import pymongo correctly indicates that pymongo has been installed successfully.
Mongo Index

Mongo indexes are similar to those of other databases. The data structure uses the B tree.

Create index ensureInde
db.inventory.ensureIndex({"item":1}){    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1}
View Indexes
db.inventory.getIndexes()[    {        "v" : 1,        "key" : {            "_id" : 1        },        "name" : "_id_",        "ns" : "test.inventory"    },    {        "v" : 1,        "key" : {            "item" : 1        },        "name" : "item_1",        "ns" : "test.inventory"    }]
Delete Index
db.inventory.dropIndex({"item":1}){ "nIndexesWas" : 2, "ok" : 1 }
Unique Index
db.test.ensureIndex({"item":1},{"unique":true}){    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1}
Explain query execution plan
db.inventory.find().explain(){    "queryPlanner" : {        "plannerVersion" : 1,        "namespace" : "test.inventory",        "indexFilterSet" : false,        "parsedQuery" : {            "$and" : [ ]        },        "winningPlan" : {            "stage" : "COLLSCAN",            "filter" : {                "$and" : [ ]            },            "direction" : "forward"        },        "rejectedPlans" : [ ]    },    "serverInfo" : {        "host" : "iZ28ywqw7nhZ",        "port" : 27017,        "version" : "3.0.2",        "gitVersion" : "6201872043ecbbc0a4cc169b5482dcf385fc464f"    },    "ok" : 1}

If the stage is COLLSCAN, the index is not used. If the index is used, IXSCAN is displayed.

Mongo Backup Recovery Backup mongodump
root@iZ28ywqw7nhZ:~/backup# mongodump 2015-04-26T21:31:34.095+0800    writing admin.system.indexes to dump/admin/system.indexes.bson2015-04-26T21:31:34.096+0800    writing admin.system.users to dump/admin/system.users.bson2015-04-26T21:31:34.113+0800    writing admin.system.users metadata to dump/admin/system.users.metadata.json2015-04-26T21:31:34.115+0800    done dumping admin.system.users2015-04-26T21:31:34.115+0800    writing admin.system.version to dump/admin/system.version.bson2015-04-26T21:31:34.116+0800    writing admin.system.version metadata to dump/admin/system.version.metadata.json2015-04-26T21:31:34.117+0800    done dumping admin.system.version2015-04-26T21:31:34.117+0800    writing test.inventory to dump/test/inventory.bson2015-04-26T21:31:34.118+0800    writing test.inventory metadata to dump/test/inventory.metadata.json2015-04-26T21:31:34.119+0800    done dumping test.inventory2015-04-26T21:31:34.119+0800    writing test.system.indexes to dump/test/system.indexes.bson
Restore mongorestore-drop
root@iZ28ywqw7nhZ:~/backup# mongorestore --drop2015-04-27T09:03:22.391+0800    using default 'dump' directory2015-04-27T09:03:22.394+0800    building a list of dbs and collections to restore from dump dir2015-04-27T09:03:22.397+0800    reading metadata file from dump/test/inventory.metadata.json2015-04-27T09:03:22.397+0800    restoring test.inventory from file dump/test/inventory.bson2015-04-27T09:03:22.398+0800    restoring indexes for collection test.inventory from metadata2015-04-27T09:03:22.398+0800    finished restoring test.inventory2015-04-27T09:03:22.399+0800    restoring users from dump/admin/system.users.bson2015-04-27T09:03:22.402+0800    done
Restore a single database

Cd ~ /Testmongobackup

Mongorestore-d blog-drop

Recovery ticket set

Cd ~ /Testmongobackup

Mongorestore-d blog-c posts-drop

Import, export, and import large backup Databases

Currently, three formats are supported.

  • CSV
  • TSV
  • JSON

For example:

$ Export Import-d blog-c tagcloud-type csv-headerline <csvimportfile.csv

Export export Export

Three formats are also supported, which is consistent with the import format.

Export export-d blog-c posts-q {}-f _ id, Title, Message, Author-csv> blogposts.csv

Add an admin user for authentication
use admindb.createUser({user : "admin", pwd: "pass", roles: [ "readWrite", "dbAdmin" ] })db.system.users.find()
Common permissions are:
  • Read
  • ReadWrite
  • DbAdmin
  • DbOwner
  • UserAdmin
  • Backup
  • Restore
  • ReadAnyDatabase
  • ReadWriteAnyDatabase
  • UserAdminAnyDatabase
  • DbAdminAnyDatabase
  • Root (highest permission)
  • ...

For more information, see http://docs.mongodb.org/v3.0/reference/built-in-roles/.

Enable authentication

Add it to the mongo configuration file, and then restart it to take effect:

auth=true

Service mongodb restart

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.