Installation using MongoDB

Source: Internet
Author: User

Installation

Download the installation package
Curl-o https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.5.tgz
Extract
Tar axf mongodb-linux-x86_64-2.6.5.tgz-c/opt/

Ln-s/opt/mongodb-linux-x86_64-2.6.5//opt/mongodb
Create a log, data storage directory
Mkdir-pv/opt/mongodb/{data,etc,logs}

Creating a configuration file

Vim/opt/mongodb/etc/mongodb.conf

# log file location logpath=/opt/mongodb/logs/mongodb.log# write to log logappend=true# as a daemon run fork = true# default 27017port = 27017# Database file location dbpath=/opt/mongodb/data/# Enable periodic logging of CPU utilization and I/O waits #cpu = true# is not authenticated by default, non-secure way #noauth = Trueauth = true# verbose Record output #verbose = true# Inspect All client data for validity on receipt (useful for# developing drivers) to validate client requests when developing drivers #objch Eck = true# Enable DB quota management# enabling database quota management #quota = true# setting Oplog record rank # set oplogging level where n is# 0=off (DEFA ult) # 1=w# 3=both# 7=w+some reads#diaglog=0# diagnostic/debugging option Dynamic Debug Item #nocursors = true# Ignore query hints Ignore query hint #nohints = true# Disable HTTP interface, default to Localhost:28017#nohttpinterface = true# Close Server-side script, this will greatly limit function # Turns off Server-side  Scripting.  This would result in greatly limited# functionality#noscripting = true# Close the scan table, any query will be scan failed # Turns off table scans. Any query this would do a table scan fails. #notablescan = true# Close the data file pre-allocate # Disable. #noprealloc = t rue# Specify the size of the. ns file for the new database, in mb# SpeCify. ns file size for new databases.# nssize = # Replication options copy options # in replicated MONGO databases, specify the rep Lica Set name here#replset=setname# maximum size in megabytes for replication operation log#oplogsize=1024# Path to a key File storing authentication info for connections# between replica set members# specifies the path to the key file that stores the authentication information #keyfile=/path/to/ KeyFile

Add a MongoDB user
Useradd MongoDB
Echo ' 123456! ' |PASSWD MongoDB--stdin

Writing Administrative Scripts

Vim/etc/init.d/mongodb

#!/bin/sh## mongodb-this script starts and stops the MongoDB daemon## chkconfig:-15# Description:mongodb is a non- relational database storage system.# processname:mongodb# config:/opt/mongodb/etc/mongodb.conf# pidfile:/opt/mongodb /mongodb.pidpath=/opt/mongodb/bin:/sbin:/bin:/usr/sbin:/usr/binname=mongodbtest-x $DAEMON | | Exit 0set-ecase "in Start" echo-n "Starting MongoDB ... "#/opt/mongodb/bin/mongod--port 27017--fork--dbpath=/opt/mongodb/data/--config=/opt/mongodb/etc/mongodb.conf- -logpath=/opt/mongodb/logs/mongodb.log--logappend su-mongodb-c "/opt/mongodb/bin/mongod--config=/opt/mongodb/e  Tc/mongodb.conf ";; Stop) echo-n "stopping MongoDB ...  "/opt/mongodb/bin/mongod--shutdown--dbpath=/opt/mongodb/data/;;      Restart) $ stop $ start;    *) n=/etc/init.d/$NAME echo "Usage: $N {start|stop}" >&2 exit 1;; ESAC Exit 0

Modify Permissions
Chown-r mongodb.mongodb/opt/mongodb-linux-x86_64-2.6.5
Start
chmod A+x/etc/init.d/mongodb
/etc/init.d/mongodb start
modifying environment variables
Vim/etc/profile
#mongodb
Export path= $PATH:/opt/mongodb/bin

MongoDB common commands to connect MongoDB

MONGO 127.0.0.1:27017/admin
This connection is connected to the Admin library in 27017-Port MongoDB on 127.0.0.1

Help information

Help
\help
Db.mycoll.help ()

Library Table Basic Operations

View all databases
Show DBS
Switch database
Use DBNAME
View all Tables
Show tables
View the database currently in use
Db.getname ()
Db
Show Current DB status
Db.stats ()
View the link machine address of the current DB
Db.getmongo ()

User Management

View all users in the current library
Show Users
View a list of permissions
Show roles
Add Admin user

db.createUser({user:"python",pwd:"python",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})

Create read-write permissions for ordinary users
Use library name

db.createUser({user:"name",pwd:"password",roles:[{ role:" readWrite",db:”库名”}]})

User authentication, password settings
Db.auth ("UserName", "123123")
Delete User
Db.removeuser ("UserName")

Start off

With a script
Service MongoDB Start
Service MongoDB Stop
Using commands
/opt/mongodb/bin/mongod--config=/opt/mongodb/etc/mongodb.conf
/opt/mongodb/bin/mongod--shutdown--dbpath=/opt/mongodb/data/

Aggregation (table) management view Help information

Db.help
Get all the clustered collections of the current DB
Db.getcollectionnames ()
Create a Clustered collection (table)
Show tables;
Db.createcollection (' Test ', {size:20,capped:5,max:100})
Gets the Clustered collection (table) of the specified name
Db.getcollection ("test");
Displays the status of all clustered indexes in the current DB
Db.printcollectionstats ()

Querying data

db. aggregation name (table name). Find ()
Db.tb.find ()
db. aggregation name (table name). FIND ({condition},{key})
Db.tb.find ({},{_id:0,_class:1})
_id:0 in _id: is the key, 0 means not display, non-zero indicates the display
Query by criteria
Comparator: $GT----greater than, $GTE----greater than or equal, $lt----less than, $lte----less than or equal, $eq----equal to, $ne----not equal to
Db.tb.find ({num:{$eq: 4}},{_id:0,_class:1,num:4})
Db.tb.find ({num:{$gt: 3, $lt: 7}},{_id:0,_class:1,num:4})
Regular expressions
Test failed ...

Open firewall

Vim/etc/sysconfig/iptables
-A input-m state--state new-m tcp-p TCP--dport 27017-j ACCEPT
Service Iptables Reload

Installation using 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.