MongoDB-linux-2.4.10-X86_64(centos/linux下安装,启动服务脚本)
admin
2023-04-13 11:21:32
0

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,解压并移动到相应位置

[root@localhost ~]# tar -zxvf mongodb-linux-x86_64-2.4.10.tgz 

[root@localhost ~]# cd mongodb-linux-x86_64-2.4.10

[root@localhost mongodb-linux-x86_64-2.4.10]# ls

bin  GNU-AGPL-3.0  README  THIRD-PARTY-NOTICES

[root@localhost mongodb-linux-x86_64-2.4.10]# cd bin/

[root@localhost bin]# ls

bsondump  mongo  mongod  mongodump  mongoexport  mongofiles  mongoimport  mongooplog  mongoperf  mongorestore  mongos  mongosniff  mongostat  mongotop

[root@localhost ~]# 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

[root@localhost 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目录下使用如下命令查看启动参数:

[root@localhost bin]# pwd

/mongodb/bin

[root@localhost 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

  --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 [/]

  --replIndexPrefetch arg specify index prefetching behavior (if secondary) 

                          [none|_id_only|all]


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,建立启动服务文件

[root@localhost conf]# 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,启动服务

[root@localhost logs]# 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

                                                           [确定]

[root@localhost logs]# ps -ef | grep mongod

root      1407  1346  0 17:08 pts/1    00:00:00 vim /etc/init.d/mongod

mongod    1564     1  1 17:13 ?        00:00:00 /mongodb/bin/mongod -f /mongodb/conf/mongod.conf

root      1577  1410  0 17:14 pts/2    00:00:00 grep mongod

[root@localhost logs]# lsof -i:8908

-bash: lsof: command not found

[root@localhost logs]# 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端口号,故登录不成功

[root@localhost bin]# ./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


由于改变了端口号,故使用时需要指定端口号

[root@localhost bin]# ./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目录,自动生成如下文件

[root@localhost mongodb]# cd data/

[root@localhost data]# ll

总用量 507936

-rw------- 1 mongod mongod  67108864 5月  21 17:37 admin.0

-rw------- 1 mongod mongod 134217728 5月  21 17:37 admin.1

-rw------- 1 mongod mongod  16777216 5月  21 17:37 admin.ns

drwxr-xr-x 2 mongod mongod      4096 5月  21 18:01 journal

-rw------- 1 mongod mongod  67108864 5月  21 18:01 local.0

-rw------- 1 mongod mongod  16777216 5月  21 18:01 local.ns

-rwxr-xr-x 1 mongod mongod         5 5月  21 18:01 mongod.lock

-rw------- 1 mongod mongod  67108864 5月  21 18:02 test.0

-rw------- 1 mongod mongod 134217728 5月  21 18:00 test.1

-rw------- 1 mongod mongod  16777216 5月  21 18:02 test.ns

OK,以上情况视为安装成功,服务正常运行


8,基本用户管理--命令区分大小写

http://blog.51yip.com/nosql/1575.html 请参考该博文


9,更多shell操作管理命令

请查看http://quanzhong.iteye.com/blog/916237 博文


相关内容

热门资讯

徐巧芯剖析赖清德:最怕自己丢掉... 海峡导报综合报道 大罢免投票一周年纪念活动26日举行,台北市长蒋万安出席并怒轰,民进党一年过后仍没有...
中国红十字会总会紧急向广东调拨... 记者从中国红十字会总会了解到,针对台风“红霞”给广东造成的灾情,中国红十字会总会启动四级应急响应,紧...
这些少年,正被“毒蛋”围猎 李梦抽了几口电子烟,吐出了白色的烟雾,有一股浓烈的水果香味。随后,她看着自己握电子烟的手,跟身边人说...
蒋万安号召“倒阁”,卢秀燕认为... 海峡导报综合报道 台北市长蒋万安27日抛出震撼议题,表示多名民代向他提议推动“倒阁”,要求台行政机构...
特朗普将在白宫会见两位重要客人 俄乌战争仍未结束,美伊冲突又有升级风险。在此背景下,美国总统特朗普将在白宫接见乌克兰和以色列领导人。...
新租客打扫卧室柜子上掉落4万元... 近日,莲前西路某小区内,张倩签下一套房子的租赁合同。当天上午,她带着12岁的大女儿周馨怡和11岁的小...
六部门通告:禁止涉军队退役报废... 关于禁止涉军队退役报废装备销售活动的通告近年来,部分经营者在线上线下公开销售涉及我军退役报废装备(指...
九寨沟景区发生泥石流,部分道路... 7月26日,九寨沟风景名胜区管理局发布泥石流灾害通报:2026年7月26日15时50分,九寨沟景区局...
空调26度不凉是什么原因 从空调内部元器件来说,不凉的原因有可能是温度传感器出现故障了,也有可能是空调压缩机或者铜管出现问题,...
鲸鸿动能携手香港国际机场、鸿蒙...   2026 年 7 月 25 日,十余位鸿蒙车主组成的车队从珠海出发,经港珠澳大桥通关抵达香港国际...