mongodbインストール構成の実践
9864 ワード
[root@mongodb ~]# tar zxvf mongodb-linux-x86_64-rhel55-3.0.3.tgz -C /opt/
mongodb-linux-x86_64-rhel55-3.0.3/README
mongodb-linux-x86_64-rhel55-3.0.3/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel55-3.0.3/GNU-AGPL-3.0
mongodb-linux-x86_64-rhel55-3.0.3/bin/mongodump
mongodb-linux-x86_64-rhel55-3.0.3/bin/mongorestore
mongodb-linux-x86_64-rhel55-3.0.3/bin/mongoexport
mongodb-linux-x86_64-rhel55-3.0.3/bin/mongoimport
mongodb-linux-x86_64-rhel55-3.0.3/bin/mongostat
mongodb-linux-x86_64-rhel55-3.0.3/bin/mongotop
mongodb-linux-x86_64-rhel55-3.0.3/bin/bsondump
mongodb-linux-x86_64-rhel55-3.0.3/bin/mongofiles
mongodb-linux-x86_64-rhel55-3.0.3/bin/mongooplog
mongodb-linux-x86_64-rhel55-3.0.3/bin/mongoperf
mongodb-linux-x86_64-rhel55-3.0.3/bin/mongod
mongodb-linux-x86_64-rhel55-3.0.3/bin/mongos
mongodb-linux-x86_64-rhel55-3.0.3/bin/mongo
[root@mongodb opt]# ln -sv mongodb-linux-x86_64-rhel55-3.0.3/ mongodb
create symbolic link `mongodb' to `mongodb-linux-x86_64-rhel55-3.0.3/'
3.mongodbユーザーの作成
[root@mongodb /]# useradd mongodb
4.適切なファイルディレクトリを作成し、権限を変更する
[root@mongodb /]# mkdir /data/db -p
[root@mongodb /]# chown mongodb:mongodb /data/ -R
[root@mongodb opt]# chown -R mongodb:mongodb /opt/*
5.PATH環境変数を追加shellの下でmongodbに正常にアクセスするアプリケーションツール
[root@mongodb bin]# vim /etc/profile.d/mongodb.sh
export PATH=$PATH:/opt/mongodb/bin
[root@mongodb bin]# source /etc/profile.d/mongodb.sh
[root@mongodb bin]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/opt/mongodb/bin
6.mongodbのPID格納ディレクトリの作成
[root@mongodb run]# mkdir /var/run/mongodb
[root@mongodb run]# chown mongodb:mongodb /var/run/mongodb/ -R
[root@mongodb opt]# mongod --dbpath /data/db/ --logpath /var/log/mongodb.log --logappend --port 27017 --pidfilepath /var/run/mongodb/mongodb.pid --maxConns 250 --rest --httpinterface --fork
about to fork child process, waiting until server is ready for connections.
forked process: 4060
child process started successfully, parent exiting
--maxConns 250 : 250
--rest --httpinterface : web , 28017
--fork :
7.インストール中に発生した問題
インストール後に次の警告が表示されます.
(1).[root@mongodb ~]# mongo
MongoDB shell version: 3.0.3
connecting to: test
Server has startup warnings:
2015-06-15T20:27:49.892+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-06-15T20:27:49.892+0800 I CONTROL [initandlisten]
この問題が発生したらmongodbに関するファイル権限を表示します.主に/opt/data/db//var/run/mongodb//var/log/mongodb.logの権限は、初期化開始時にrootユーザーの権限で実行されるため、これらのディレクトリの下の権限がroot rootになる可能性があります.
(2).[mongodb@mongodb db]$ mongod --dbpath /data/db/ --logpath /var/log/mongodb.log --logappend --port 27017 --pidfilepath /var/run/mongodb/mongodb.pid --maxConns 250 --rest --httpinterface --fork
2015-06-15T17:41:25.721+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is
'always'.
2015-06-15T17:41:25.721+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2015-06-15T17:41:25.721+0800 I CONTROL [initandlisten]
2015-06-15T17:41:25.721+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is
'always'.
2015-06-15T17:41:25.721+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2つのパラメータを変更すればよい
echo "never" > /sys/kernel/mm/transparent_hugepage/enabled
echo "never" > /sys/kernel/mm/transparent_hugepage/defrag
8.インストールに成功してログイン
[root@mongodb ~]# ss -tanlp | grep mongod
0 0 *:27017 *:* users:(("mongod",4095,6))
0 0 *:28017 *:* users:(("mongod",4095,8))
[root@mongodb ~]# mongo
MongoDB shell version: 3.0.3
connecting to: test
> 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
Webインタフェース28017ポートへのログイン
9.サーバーのシャットダウン
(1).[root@mongodb ~]# mongod --shutdown
killing process with pid: 3936
(2).[root@mongodb ~]# killall mongod
10.mongodbにプロファイル/etc/mongodb.confを作成する
[root@mongodb ~]# grep -v ^# /etc/mongodb.conf
logpath=/var/log/mongodb.log
logappend=true
fork = true
dbpath=/data/db
pidfilepath = /var/run/mongodb/mongodb.pid
rest = true
httpinterface = true
開始
[root@mongodb ~]# mongod -f /etc/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 4169
child process started successfully, parent exiting
[root@mongodb ~]# ss -tanlp | grep mongod
0 0 *:27017 *:* users:(("mongod",4169,6))
0 0 *:28017 *:* users:(("mongod",4169,8))
11.サービスmongodb*でmongodb起動スクリプトを起動する:/etc/rc.d/init.d/mongod
#!/bin/bash
# mongod - Startup script for mongod
# chkconfig: 35 85 15
# description: Mongo is a scalable, document-oriented database.
# processname: mongod
# config: /etc/mongod.conf
# pidfile: /var/run/mongo/mongod.pid
. /etc/rc.d/init.d/functions
# things from mongod.conf get there by mongod reading it
CONFIGFILE="/etc/mongodb.conf"
OPTIONS=" -f $CONFIGFILE"
SYSCONFIG="/etc/sysconfig/mongod"
DBPATH=`awk -F= '/^dbpath=/{print $2}' "$CONFIGFILE"`
PIDFILE=`awk -F= '/^dbpath\s=\s/{print $2}' "$CONFIGFILE"`
mongod=${MONGOD-/opt/mongodb/bin/mongod}
MONGO_USER=mongodb
MONGO_GROUP=mongodb
if [ -f "$SYSCONFIG" ]; then
. "$SYSCONFIG"
fi
# Handle NUMA access to CPUs (SERVER-3574)
# This verifies the existence of numactl as well as testing that the command works
NUMACTL_ARGS="--interleave=all"
if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/null
then
NUMACTL="numactl $NUMACTL_ARGS"
else
NUMACTL=""
fi
start()
{
echo -n $"Starting mongod: "
daemon --user "$MONGO_USER" $NUMACTL $mongod $OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/mongod
}
stop()
{
echo -n $"Stopping mongod: "
killproc -p "$PIDFILE" -d 300 /opt/mongodb/bin/mongod
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mongod
}
restart () {
stop
start
}
ulimit -n 12000
RETVAL=0
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload|force-reload)
restart
;;
condrestart)
[ -f /var/lock/subsys/mongod ] && restart || :
;;
status)
status $mongod
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
RETVAL=1
esac
exit $RETVAL
テスト開始
[root@mongodb ~]# service mongod start
Starting mongod: about to fork child process, waiting until server is ready for connections.
forked process: 4426
child process started successfully, parent exiting
[ OK ]
[root@mongodb init.d]# ss -tanlp | grep mongod
0 0 *:27017 *:* users:(("mongod",4426,6))
0 0 *:28017 *:* users:(("mongod",4426,8))
[root@mongodb ~]# mongo
MongoDB shell version: 3.0.3
connecting to: test
>
[root@mongodb ~]# service mongod start
Starting mongod: about to fork child process, waiting until server is ready for connections.
forked process: 4426
child process started successfully, parent exiting
[ OK ]
[root@mongodb ~]# service mongod stop
Stopping mongod: [ OK ]