标签:size cti run nstat alias serve items download war
MongoDB 是由 C++语言编写一个基于分布式文件存储的开源 NoSQL 数据库系统。在高负 载的情况下,可添加更多的节点,以保证服务性能。在许多场景下用于代替传统的关系型 数据库或键/值存储方式。旨在为 Web 应用提供可扩展的高性能数据存储解决方案。
MongoDB 提供了一个面向文档存储,操作起来比较简单和容易,可以存储比较复杂的 数据类型。最大的特点是支持的查询语言非常强大,语法优点类似于面向对象的查询语 言。几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索 引。是一个面向集合的,模式自由的文档型数据库。
l 网站数据
l 分布式场景
l 缓存层
l 文档格式存储
(1)文档(document):是 mongodb 的核心概念,是 mongodb 逻辑存储的最小单元
(2)集合(collection):多个文档组成集合
(3)数据库(database):多个集合组成数据库
l 命名空间文件(ns)
l 数据文件(0,1,2)
l 系统日志文件 logpath
l journal 日志文件
l oplog 复制操作日志文件
l 慢查询日志
BSON 是 Binary JSON,是二进制的格式,能将 mongdb 的所有文档表示为字节字符串。
JSON 是一种轻量级的数据交换格式。它基于 JavaScript 的一个子集
l null,代表空或者不存在
l布尔,只有 true 和 false
l 数字,64 位浮点数
l 字符串,utf8 字符串
l 数组,值或者列表可表示为数组
l 对象,对象的数据
简单,简洁,容易理解、解析、记忆
l 文档的键命名几乎所有 utf8 字符,只有少数例外:$开头;\0(空字符);_下划线开头。
l 集合的命名几乎所有 utf8 字符,只有少数例外:$开头;\0(空字符);system.开头;”” 空字符串。
l 数据库的命名几乎所有 utf8 字符,只有少数例外:””空字符串;\0;空格;. 点;\ ;
/。
CentOS-6.8 192.168.25.32/24 MongoDB
[root@MongodDB tmp]# wget https://www.mongodb.com/dr/fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62- 3.2.7.tgz/download
[root@MongodDB tmp]# tar zxvf mongodb-linux-x86_64-rhel62-3.2.7.tgz
?
[root@MongodDB tmp]# mv mongodb-linux-x86_64-rhel62-3.2.7/ /usr/local/mongodb
[root@MongodDB ~]# ulimit -n
?
1024
?
[root@MongodDB ~]# ulimit -n 65535
?
[root@MongodDB ~]# ulimit -n
?
65535
[root@MongodDB ~]# ulimit -u
?
31994
?
[root@MongodDB ~]# ulimit -u 65535
?
[root@MongodDB ~]# ulimit -u
?
65535
[root@MongodDB ~]# mkdir -p /data/mongodb1
?
[root@MongodDB ~]# mkdir -p /data/logs/mongodb
?
[root@MongodDB ~]# touch /data/logs/mongodb/mongodb1.log
?
[root@MongodDB ~]# cd /usr/local/mongodb/
?
[root@MongodDB mongodb]# ls
?
bin GNU-AGPL-3.0 MPL-2 README THIRD-PARTY-NOTICES
?
[root@MongodDB mongodb]# mkdir conf
?
[root@MongodDB mongodb]# cat conf/mongodb1.conf
?
port=27017
?
dbpath=/data/mongodb1
?
logpath=/data/logs/mongodb/mongodb1.log
?
logappend=true
?
fork=true
?
maxConns=5000
?
storageEngine=mmapv1
[root@MongodDB mongodb]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf
?
[root@MongodDB mongodb]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf
?
about to fork child process, waiting until server is ready for connections.
?
forked process: 2016
?
child process started successfully, parent exiting
?
[root@MongodDB mongodb]# netstat -anpt | grep mongod
?
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 2016/mongod
?
[root@MongodDB mongodb]# ps aux | grep mongdb
?
root 2044 0.0 0.0 103324 892 pts/0 S+ 20:04 0:00 grep mongdb
[root@MongodDB mongodb]# cat /etc/rc.local
?
#!/bin/sh
?
#
?
# This script will be executed *after* all the other init scripts.
?
# You can put your own initialization stuff in here if you don‘t
?
# want to do the full Sys V style init stuff.
?
?
touch /var/lock/subsys/local
?
rm -f /data/mongodb1/mongod.lock
?
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf
[root@MongodDB mongodb]# /usr/local/mongodb/bin/mongo
?
MongoDB shell version: 3.2.7
?
connecting to: test
?
Welcome to the MongoDB shell.
?
For interactive help, type "help".
?
For more comprehensive documentation, see
?
• http://docs.mongodb.org/
?
Questions? Try the support group
?
• http://groups.google.com/group/mongodb-user
?
Server has startup warnings:
?
2019-09-02T19:50:12.394+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
?
2019-09-02T19:50:12.394+0800 I CONTROL [initandlisten]
?
2019-09-02T19:50:12.395+0800 I CONTROL [initandlisten]
?
2019-09-02T19:50:12.395+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always‘.
?
2019-09-02T19:50:12.395+0800 I CONTROL [initandlisten] ** We suggest setting it to ‘never‘
?
2019-09-02T19:50:12.395+0800 I CONTROL [initandlisten]
?
2019-09-02T19:50:12.395+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always‘.
?
2019-09-02T19:50:12.395+0800 I CONTROL [initandlisten] ** We suggest setting it to ‘never‘
?
2019-09-02T19:50:12.395+0800 I CONTROL [initandlisten]
?
\> show dbs
?
local 0.078GB
?
\> exit
?
Bye
[root@MongodDB mongodb]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
?
[root@MongodDB mongodb]# echo never > /sys/kernel/mm/transparent_hugepage/defrag
?
[root@MongodDB mongodb]# vim .bash_profile
?
[root@MongodDB mongodb]# cat .bash_profile
?
alias mongo=/usr/local/mongodb/bin/mongo
?
[root@MongodDB mongodb]# source .bash_profile
方法一:
[root@MongodDB ~]# mongo
?
> use admin
?
switched to db admin
?
> db.shutdownServer();
?
server should be down...
?
2019-09-02T20:19:49.361+0800 I NETWORK [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
?
2019-09-02T20:19:49.361+0800 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
?
2019-09-02T20:19:49.361+0800 I NETWORK [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) failed failed
?
> exit
?
bye
?
[root@MongodDB ~]# netstat -anpt |grep mongod
方法二:
[root@MongodDB ~]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf
?
about to fork child process, waiting until server is ready for connections.
?
forked process: 2081
?
child process started successfully, parent exiting
?
[root@MongodDB ~]# netstat -anpt | grep mongod
?
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 2081/mongod
?
[root@MongodDB ~]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf --shutdown
?
killing process with pid: 2081
?
[root@MongodDB ~]# netstat -anpt | grep mongod
方法三:
[root@MongodDB ~]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf
?
about to fork child process, waiting until server is ready for connections.
?
forked process: 2102
?
child process started successfully, parent exiting
?
[root@MongodDB ~]# ps -ef |grep mongod
?
root 2102 1 0 20:22 ? 00:00:00 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf
?
root 2118 1857 0 20:22 pts/0 00:00:00 grep mongod
?
[root@MongodDB ~]# kill 2102
?
[root@MongodDB ~]# netstat -anpt | grep mongod
?
[root@MongodDB ~]#
[root@MongodDB ~]# cd /usr/local/mongodb/conf/
?
[root@MongodDB conf]# ls
?
mongodb1.conf
?
[root@MongodDB conf]# cp mongodb{1,2}.conf
?
[root@MongodDB conf]# ls
?
mongodb1.conf mongodb2.conf
?
[root@MongodDB conf]# cat mongodb2.conf
?
port=27018
?
dbpath=/data/mongodb2
?
logpath=/data/logs/mongodb/mongodb2.log
?
logappend=true
?
fork=true
?
maxConns=5000
?
storageEngine=mmapv1
?
[root@MongodDB conf]# mkdir /data/mongodb2
?
[root@MongodDB conf]# touch /data/logs/mongodb/mongodb2.log
?
[root@MongodDB conf]# chmod 777 /data/logs/mongodb/mongodb2.log
[root@MongodDB conf]# cd /etc/init.d/
?
[root@MongodDB init.d]# cat mongodb
?
#!/bin/bash
?
INSTANCE=$1
?
ACTION=$2
?
case "$ACTION" in
?
‘start‘)
?
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf;;
?
‘stop‘)
?
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf --shutdown;;
?
‘restart‘)
?
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf --shutdown
?
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf;;
?
esac
?
[root@MongodDB init.d]# chmod +x mongodb
?
[root@MongodDB init.d]# /etc/init.d/mongodb mongodb1 start
?
about to fork child process, waiting until server is ready for connections.
?
forked process: 2139
?
child process started successfully, parent exiting
?
[root@MongodDB init.d]# /etc/init.d/mongodb mongodb2 start
?
about to fork child process, waiting until server is ready for connections.
?
forked process: 2155
?
child process started successfully, parent exiting
?
[root@MongodDB init.d]# netstat -anpt |grep mongod
?
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 2139/mongod
?
tcp 0 0 0.0.0.0:27018 0.0.0.0:* LISTEN 2155/mongod
到此mongodb已经搭建完毕了
操作 | 作用 |
---|---|
show dbs | 查看当前示例下的数据库列表,等同于 show database |
show users | 显示用户 |
use <db_name> | 切换当前数据库 |
db.help() | 显示数据库操作命令 |
show collections | 显示当前数据库中的集合,等同于 show tables |
db.mycoll.help() | 显示集合操作命令,mycoll 是当前下叫做 mycoll 的集合 |
db.foo.find() | 对当前数据库中 foo 集合进行数据查找 |
[root@MongodDB ~]# mongo
?
> show dbs
?
local 0.078GB
> show databases
?
local 0.078GB
?
> use benet
?
switched to db benet
?
> show dbs
?
local 0.078GB
?
> show tables
?
> use local
?
switched to db local
?
> show tables
?
startup_log
?
system.indexes
?
> use benet
?
switched to db benet
?
> db.user.insert({"id":1,"name":"Tom"});
?
WriteResult({ "nInserted" : 1 })
?
> show dbs
?
benet 0.078GB
?
local 0.078GB
?
> show collections
?
system.indexes
?
user
?
> db.user.find()
?
{ "_id" : ObjectId("5d6d0d5bea5345188da4699e"), "id" : 1, "name" : "Tom" }
?
>
?
bye
查看帮助
[root@MongodDB ~]# /usr/local/mongodb/bin/mongod --help
?
[root@MongodDB ~]# mongo
?
MongoDB shell version: 3.2.7
?
connecting to: test
?
Server has startup warnings:
?
2019-09-02T20:31:36.534+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
?
2019-09-02T20:31:36.534+0800 I CONTROL [initandlisten]
?
> help
?
• db.help() help on db methods
?
• db.mycoll.help() help on collection methods
?
• sh.help() sharding helpers
?
• rs.help() replica set helpers
?
• help admin administrative help
?
• help connect connecting to a db help
?
• help keys key shortcuts
?
• help misc misc things to know
?
• help mr mapreduce
?
?
• show dbs show database names
?
• show collections show collections in current database
?
• show users show users in current database
?
• show profile show most recent system.profile entries with time >= 1ms
?
• show logs show the accessible logger names
?
• show log [name] prints out the last segment of log in memory, ‘global‘ is default
?
• use <db_name> set current database
?
• db.foo.find() list objects in collection foo
?
• db.foo.find( { a : 1 } ) list objects in foo where a == 1
?
• it result of the last line evaluated; use to further iterate
?
• DBQuery.shellBatchSize = x set default number of items to display on shell
?
• exit quit the mongo shell
?
>
?
?
> db.help()
?
DB methods:
?
• db.adminCommand(nameOrDocument) - switches to ‘admin‘ db, and runs command [ just calls db.runCommand(...) ]
?
• db.auth(username, password)
?
• db.cloneDatabase(fromhost)
?
• db.commandHelp(name) returns the help for the command
?
• db.copyDatabase(fromdb, todb, fromhost)
?
• db.createCollection(name, { size : ..., capped : ..., max : ... } )
?
• db.createUser(userDocument)
?
• db.currentOp() displays currently executing operations in the db
?
• db.dropDatabase()
?
• db.eval() - deprecated
?
• db.fsyncLock() flush data to disk and lock server for backups
?
• db.fsyncUnlock() unlocks server following a db.fsyncLock()
?
• db.getCollection(cname) same as db[‘cname‘] or db.cname
?
• db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db‘s collections
?
• db.getCollectionNames()
?
• db.getLastError() - just returns the err msg string
?
• db.getLastErrorObj() - return full status object
?
• db.getLogComponents()
?
• db.getMongo() get the server connection object
?
• db.getMongo().setSlaveOk() allow queries on a replication slave server
?
• db.getName()