1. 下载二进制包,解压,安装

 
  
  1. wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-1.8.2.tgz -P /usr/local/src  
  2. tar zxvf /usr/local/src/mongodb-linux-x86_64-1.8.2.tgz -C /usr/local/src  
  3. cd /usr/local/src  
  4. mv mongodb-linux-x86_64-1.8.2 /usr/local/mongodb 

2. 添加mongod用户

 
  
  1. useradd -r mongod -d /usr/local/mongodb -s /bin/false 

3. 建立相关目录

 
  
  1. # 配置文件目录  
  2. mkdir /usr/local/mongodb/etc  
  3. # 修改目录的拥有者为mongod  
  4. chown -R mongod:mongod /usr/local/mongod  
  5. # 建立数据目录和日志目录  
  6. mkdir -p /data/mongodb/{db,logs}  
  7. # 修改目录的拥有者为mongod  
  8. chown -R mongod:mongod /data/mongodb 

4. 配置文件

 
  
  1. vi /usr/local/mongodb/etc/mongod.conf  
  2. # mongo.conf  
  3.  
  4. # log file to send write to instead of stdout – has to be a file, not directory  
  5. logpath=/data/mongodb/logs/mongod.log  
  6. # append to logpath instead of over-writing  
  7. logappend=true 
  8.  
  9. # fork and run in background  
  10. fork = true 
  11.  
  12. # specify port number  
  13. port = 27017 
  14. # comma separated list of ip addresses to listen on – all local ips by default  
  15. bind_ip = 192.168.1.10  
  16.  
  17. # directory for datafiles  
  18. dbpath = /data/mongodb/db  
  19.  
  20. # full path to pidfile (if not set, no pidfile is created)  
  21. pidfilepath = /usr/local/mongodb/mongod.pid  
  22.  
  23. # Enables periodic logging of CPU utilization and I/O wait  
  24. #cpu = true 
  25.  
  26. # Turn on/off security. Off is currently the default  
  27. #noauth = true 
  28. #auth = true 
  29.  
  30. # Verbose logging output.  
  31. #verbose = true 
  32.  
  33. # Inspect all client data for validity on receipt (useful for  
  34. # developing drivers)  
  35. #objcheck = true 
  36.  
  37. # Enable db quota management  
  38. #quota = true 
  39.  
  40. # Set oplogging level where n is  
  41. 0=off (default)  
  42. 1=W 
  43. 2=R 
  44. 3=both 
  45. 7=W+some reads  
  46. #oplog = 0 
  47.  
  48. # Diagnostic/debugging option  
  49. #nocursors = true 
  50.  
  51. # Ignore query hints  
  52. #nohints = true 
  53.  
  54. # Disable the HTTP interface (Defaults to localhost:27018).  
  55. nohttpinterface = true 
  56.  
  57. # Turns off server-side scripting. This will result in greatly limited  
  58. # functionality  
  59. #noscripting = true 
  60.  
  61. # Turns off table scans. Any query that would do a table scan fails.  
  62. #notablescan = true 
  63.  
  64. # Disable data file preallocation.  
  65. #noprealloc = true 
  66.  
  67. # Specify .ns file size for new databases.  
  68. nssize = <size> 
  69.  
  70. # Accout token for Mongo monitoring server.  
  71. #mms-token = <token> 
  72.  
  73. # Server name for Mongo monitoring server.  
  74. #mms-name = <server-name> 
  75.  
  76. # Ping interval for Mongo monitoring server.  
  77. #mms-interval = <seconds> 
  78.  
  79. # Replication Options  
  80.  
  81. # in replicated mongo databases, specify here whether this is a slave or master  
  82. #slave = true 
  83. #source = master.example.com  
  84. # Slave only: specify a single database to replicate  
  85. #only = master.example.com  
  86. # or  
  87. #master = true 
  88. #source = slave.example.com 

指定了日志的存放目录,日志以追加方式进行,后台进程运行,端口号,监听IP,数据目录,pid文件,不启动http显示页面

5. 添加为Service

 
  
  1. # 添加选项文件,可以将要启动的选项写入这个文件中,我的为空  
  2. vi /etc/sysconfig/mongod  
  3. # 启动文件  
  4. vi /etc/rc.d/init.d/mongod  
  5. #!/bin/bash  
  6.  
  7. # mongod – Startup script for mongod  
  8.  
  9. # chkconfig: 35 85 15  
  10. # description: Mongo is a scalable, document-oriented database.  
  11. # processname: mongod  
  12. # config: /etc/mongod.conf  
  13. # pidfile: /var/run/mongo/mongo.pid  
  14.  
  15. . /etc/rc.d/init.d/functions  
  16.  
  17. # things from mongod.conf get there by mongod reading it  
  18.  
  19. OPTIONS=" -f /usr/local/mongodb/etc/mongod.conf" 
  20. SYSCONFIG="/etc/sysconfig/mongod" 
  21.  
  22. mongod=${MONGOD-/usr/local/mongodb/bin/mongod}  
  23.  
  24. MONGO_USER=mongod 
  25. MONGO_GROUP=mongod 
  26.  
  27. . "$SYSCONFIG" || true  
  28.  
  29. start()  
  30. {  
  31. echo -n $"Starting mongod: "  
  32. daemon –user "$MONGO_USER" $mongod $OPTIONS  
  33. RETVAL=$?  
  34. echo  
  35. [ $RETVAL -eq 0 ] && touch /usr/local/mongodb/mongod  
  36. }  
  37.  
  38. stop()  
  39. {  
  40. echo -n $"Stopping mongod: "  
  41. killproc -p /data/mongodb/db/mongod.lock -t30 -TERM /usr/local/mongodb/bin/mongod  
  42. RETVAL=$?  
  43. echo  
  44. [ $RETVAL -eq 0 ] && rm -f /usr/local/mongodb/mongod  
  45. }  
  46.  
  47. restart () {  
  48. stop  
  49. start  
  50. }  
  51.  
  52. ulimit -n 12000  
  53. RETVAL=0 
  54.  
  55. case "$1" in  
  56. start)  
  57. start  
  58. ;;  
  59. stop)  
  60. stop  
  61. ;;  
  62. restart|reload|force-reload)  
  63. restart  
  64. ;;  
  65. condrestart)  
  66. [ -f /usr/local/mongodb/mongod ] && restart || :  
  67. ;;  
  68. status)  
  69. status $mongod  
  70. RETVAL=$?  
  71. ;;  
  72. *)  
  73. echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"  
  74. RETVAL=1 
  75.  
  76. exit $RETVAL 

添加为服务

 
  
  1. chkconfig –add mongod  
  2. chkconfig mongod on 


6. 启动、停止

 
  
  1. service mongod start  
  2. service mongod stop