本次环境 RHEL6
学着用一下MongoDB,做个笔记:
下载&安装
-
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.10.tgz
-
tar zxvf mongodb-linux-x86_64-rhel62-3.2.10.tgz
-
cd mongodb-linux-x86_64-rhel62-3.2.10
-
cp bin/* /usr/bin/
预创建datadir和logdir
-
mkdir -p /data/mongodb/data
-
mkdir -p /data/mongodb/log
编辑配置文件
可以最简化的写一下配置,其他的默认
-
port = 27017
-
dbpath = /data/mongodb/data
-
logpath = /data/mongodb/log/mongod.log
-
fork = true
保存退出
启动mongodb
-
shell> mongod -f /etc/mongodb.conf
进入mongodb shell
-
shell> mongo 127.0.0.1:27017/test
或者不加选项直接:
-
MongoDB shell version: 3.2.10
-
connecting to: test
-
Server has startup warnings:
-
2016-10-03T12:38:14.769+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
-
2016-10-03T12:38:14.769+0800 I CONTROL [initandlisten]
-
2016-10-03T12:38:14.769+0800 I CONTROL [initandlisten]
-
2016-10-03T12:38:14.769+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
-
2016-10-03T12:38:14.769+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
-
2016-10-03T12:38:14.774+0800 I CONTROL [initandlisten]
-
2016-10-03T12:38:14.774+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
-
2016-10-03T12:38:14.774+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
-
2016-10-03T12:38:14.774+0800 I CONTROL [initandlisten]
-
>
这三个wanring,后两个可以通过修改如下文件,来关闭transparent_hugepage
-
echo never > /sys/kernel/mm/transparent_hugepage/enabled
-
echo never > /sys/kernel/mm/transparent_hugepage/defrag
但这样重启会被重置成always,可以写个脚本或者放入/etc/rc.local中
关闭hugepage之后,重启mongd,进入mongo shell则该warning消失:
-
shell> mongo
-
MongoDB shell version: 3.2.10
-
connecting to: test
-
Server has startup warnings:
-
2016-10-12T05:31:46.933+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
-
2016-10-12T05:31:46.933+0800 I CONTROL [initandlisten]
-
>
-
>
还剩一个warning则很明显,不建议通过root用户来启动mongod。
如果不想让warning再显示,可以打开quiet选项,比如添加到配置文件中:quiet=true
或者
通过help便可获取常用mongo shell语法:
-
> 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
比如查怎么关闭mongod:
先查db相关的帮助:
-
> db.help()
-
………………
-
db.shutdownServer()
-
………………
-
-
> db.shutdownServer()
-
server should be down...
-
2016-10-13T17:58:49.544+0800 I NETWORK [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
-
2016-10-13T17:58:49.547+0800 I NETWORK [thread1] Socket recv() errno:104 Connection reset by peer 127.0.0.1:27017
-
2016-10-13T17:58:49.547+0800 I NETWORK [thread1] SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:27017]
-
2016-10-13T17:58:49.547+0800 I NETWORK [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) failed failed
-
>
这样,mongod就已经被关闭了。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29773961/viewspace-2126245/
,如需转载,请注明出处,否则将追究法律责任。