hyilk 发表于 2014-5-22 09:19:02

MongoDB-linux-2.4.10-X86_64(centos/linux下安装,启动服务脚本)

1,下载

从http://www.mongodb.org/downloads获取下载版本mongodb-linux-x86_64-2.4.10.tgz

2,添加用户
groupadd mongod
useradd -s /sbin/nologin -M -g mongod mongod

3,解压并移动到相应位置
# tar -zxvf mongodb-linux-x86_64-2.4.10.tgz
# cd mongodb-linux-x86_64-2.4.10
# ls
binGNU-AGPL-3.0READMETHIRD-PARTY-NOTICES
# cd bin/
# ls
bsondumpmongomongodmongodumpmongoexportmongofilesmongoimportmongooplogmongoperfmongorestoremongosmongosniffmongostatmongotop
# mv mongodb-linux-x86_64-2.4.10 /mongodb

如有必要可设置其目录权限
chmod 755 /mongodb-linux-x86_64-2.4.10/ -R
如有必要可建立系统目录命令软链接
ln -s /mongodb/bin/mongo /usr/bin/mongo
ln -s /mongodb/bin/mongod /usr/bin/mongod

3,手工建立运行目录并授权
mkdir -p date
mkdir -p logs
mkdir -p conf
mkdir -p run
chmod u+w data -R
chmod u+w logs -R
chmod u+w run -R
chown -R mongod:mongod date logs run

4,建立配置文件
vim etc/mongod.conf
# cat mongod.conf
# Start MongoDB as a daemon on port 8908

port = 8908
fork = true # daemonize it !
journal = true #
rest = true
logappend = true
auth = true

dbpath = /mongodb/data/
logpath = /mongodb/logs/mongod.log
pidfilepath = /mongodb/run/mongod.pid

=======================================
注:如果更多的启动参数,请在你的mongodb主目录bin目录下使用如下命令查看启动参数:
# pwd
/mongodb/bin
# ./mongod --help
Allowed options:

General options:
-h [ --help ]               show this usage information
--version                   show version information
-f [ --config ] arg         configuration file specifying additional options
-v [ --verbose ]            be more verbose (include multiple times for more
                              verbosity e.g. -vvvvv)
--quiet                     quieter output
--port arg                  specify port number - 27017 by default
--bind_ip arg               comma separated list of ip addresses to listen on
                              - all local ips by default
--maxConns arg            max number of simultaneous connections - 20000 by
                              default
--logpath arg               log file to send write to instead of stdout - has
                              to be a file, not directory
--logappend               append to logpath instead of over-writing
--pidfilepath arg         full path to pidfile (if not set, no pidfile is
                              created)
--keyFile arg               private key for cluster authentication
--setParameter arg          Set a configurable parameter
--nounixsocket            disable listening on unix sockets
--unixSocketPrefix arg      alternative directory for UNIX domain sockets
                              (defaults to /tmp)
--fork                      fork server process
--syslog                  log to system's syslog facility instead of file
                              or stdout
--auth                      run with security
--cpu                     periodically show cpu and iowait utilization
--dbpath arg                directory for datafiles - defaults to /data/db/
--diaglog arg               0=off 1=W 2=R 3=both 7=W+some reads
--directoryperdb            each database will be stored in a separate
                              directory
--ipv6                      enable IPv6 support (disabled by default)
--journal                   enable journaling
--journalCommitInterval arg how often to group/batch commit (ms)
--journalOptions arg      journal diagnostic options
--jsonp                     allow JSONP access via http (has security
                              implications)
--noauth                  run without security
--nohttpinterface         disable http interface
--nojournal               disable journaling (journaling is on by default
                              for 64 bit)
--noprealloc                disable data file preallocation - will often hurt
                              performance
--noscripting               disable scripting engine
--notablescan               do not allow table scans
--nssize arg (=16)          .ns file size (in MB) for new databases
--profile arg               0=off 1=slow, 2=all
--quota                     limits each database to a certain number of files
                              (8 default)
--quotaFiles arg            number of files allowed per db, requires --quota
--repair                  run repair on all dbs
--repairpath arg            root directory for repair files - defaults to
                              dbpath
--rest                      turn on simple rest api
--shutdown                  kill a running server (for init scripts)
--slowms arg (=100)         value of slow for profile and console log
--smallfiles                use a smaller default file size
--syncdelay arg (=60)       seconds between disk syncs (0=never, but not
                              recommended)
--sysinfo                   print some diagnostic system information
--upgrade                   upgrade db if needed

Replication options:
--oplogSize arg       size to use (in MB) for replication op log. default is
                        5% of disk space (i.e. large is good)

Master/slave options (old; use replica sets instead):
--master            master mode
--slave               slave mode
--source arg          when slave: specify master as <server:port>
--only arg            when slave: specify a single database to replicate
--slavedelay arg      specify delay (in seconds) to be used when applying
                        master ops to slave
--autoresync          automatically resync if slave data is stale

Replica set options:
--replSet arg         arg is <setname>[/<optionalseedhostlist>]
--replIndexPrefetch arg specify index prefetching behavior (if secondary)
                        

Sharding options:
--configsvr         declare this is a config db of a cluster; default port
                        27019; default dir /data/configdb
--shardsvr            declare this is a shard db of a cluster; default port
                        27018
5,建立启动服务文件
# cat /etc/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

# NOTE: if you change any OPTIONS here, you get what you pay for:
# this script assumes all options are in the config file.
BASEDIR="/mongodb"
CONFIGFILE="$BASEDIR/conf/mongod.conf"
OPTIONS=" -f $CONFIGFILE"
SYSCONFIG="/etc/sysconfig/mongod"

# FIXME: 1.9.x has a --shutdown flag that parses the config file and
# shuts down the correct running pid, but that's unavailable in 1.8
# for now.This can go away when this script stops supporting 1.8.
DBPATH=`awk -F= '/^dbpath=/{print $2}' "$CONFIGFILE"`
PIDFILE=`awk -F= '/^dbpath\s=\s/{print $2}' "$CONFIGFILE"`
mongod=${MONGOD-$BASEDIR/bin/mongod}

MONGO_USER=mongod
MONGO_GROUP=mongod

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 /usr/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


6,启动服务
# service mongod restart
Stopping mongod:                                           [失败]
Starting mongod: about to fork child process, waiting until server is ready for connections.
forked process: 1605
all output going to: /mongodb/logs/mongod.log
child process started successfully, parent exiting
                                                         [确定]
# ps -ef | grep mongod
root      140713460 17:08 pts/1    00:00:00 vim /etc/init.d/mongod
mongod    1564   11 17:13 ?      00:00:00 /mongodb/bin/mongod -f /mongodb/conf/mongod.conf
root      157714100 17:14 pts/2    00:00:00 grep mongod
# lsof -i:8908
-bash: lsof: command not found
# netstat -anpt
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp      0      0 0.0.0.0:8908                0.0.0.0:*                   LISTEN      1564/mongod         
tcp      0      0 0.0.0.0:9908                0.0.0.0:*                   LISTEN      1564/mongod         

7,shell进入服务命令行查看
系统默认使用270117端口号,故登录不成功
# ./mongo 
MongoDB shell version: 2.4.10
connecting to: test
Wed May 21 17:28:09.809 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145
exception: connect failed

由于改变了端口号,故使用时需要指定端口号
# ./mongo -port 8908  
MongoDB shell version: 2.4.10
connecting to: 127.0.0.1:8908/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
>

查看data目录,自动生成如下文件
# cd data/
# ll
总用量 507936
-rw------- 1 mongod mongod67108864 5月21 17:37 admin.0
-rw------- 1 mongod mongod 134217728 5月21 17:37 admin.1
-rw------- 1 mongod mongod16777216 5月21 17:37 admin.ns
drwxr-xr-x 2 mongod mongod      4096 5月21 18:01 journal
-rw------- 1 mongod mongod67108864 5月21 18:01 local.0
-rw------- 1 mongod mongod16777216 5月21 18:01 local.ns
-rwxr-xr-x 1 mongod mongod         5 5月21 18:01 mongod.lock
-rw------- 1 mongod mongod67108864 5月21 18:02 test.0
-rw------- 1 mongod mongod 134217728 5月21 18:00 test.1
-rw------- 1 mongod mongod16777216 5月21 18:02 test.ns
OK,以上情况视为安装成功,服务正常运行

页: [1]
查看完整版本: MongoDB-linux-2.4.10-X86_64(centos/linux下安装,启动服务脚本)