当前位置:Gxlcms > mysql > mongodb服务脚本编写

mongodb服务脚本编写

时间:2021-07-01 10:21:17 帮助过:40人阅读

1.新建配置文件 mongodb安装时缺少配置文件 在 /etc 目录下新建 mongod.conf 配置文件 内容如下 # mongo.conf#where to loglogpath=/data/logs/db.loglogappend=true# fork and run in backgroundfork = true#port = 27017dbpath=/data/db# Enables periodic

1.新建配置文件

mongodb安装时缺少配置文件

在 /etc 目录下新建 mongod.conf 配置文件

内容如下

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

mongodb/bin/mongod -f /etc/mongod.conf
这种启动我感到很不爽,那我就做成service

3.service(服务)脚本

在 /etc/init.d 目录下新建 mongod文件

内容如下

  1. #!/bin/sh
  2. #
  3. #mongod - Startup script for mongod
  4. #
  5. # chkconfig: - 85 15
  6. # description: Mongodb database.
  7. # processname: mongod
  8. # Source function library
  9. . /etc/rc.d/init.d/functions
  10. # things from mongod.conf get there by mongod reading it
  11. # OPTIONS
  12. OPTIONS=" -f /etc/mongod.conf"
  13. #mongod
  14. mongod="/root/mongodb/bin/mongod"
  15. lockfile=/var/lock/subsys/mongod
  16. start()
  17. {
  18. echo -n $"Starting mongod: "
  19. daemon $mongod $OPTIONS
  20. RETVAL=$?
  21. echo
  22. [ $RETVAL -eq 0 ] && touch $lockfile
  23. }
  24. "/etc/init.d/mongod" 70L, 1036C
  25. #!/bin/sh
  26. #
  27. #mongod - Startup script for mongod
  28. #
  29. # chkconfig: - 85 15
  30. # description: Mongodb database.
  31. # processname: mongod
  32. # Source function library
  33. . /etc/rc.d/init.d/functions
  34. # things from mongod.conf get there by mongod reading it
  35. # OPTIONS
  36. OPTIONS=" -f /etc/mongod.conf"
  37. #mongod
  38. mongod="/root/mongodb/bin/mongod"
  39. lockfile=/var/lock/subsys/mongod
  40. start()
  41. {
  42. echo -n $"Starting mongod: "
  43. daemon $mongod $OPTIONS
  44. RETVAL=$?
  45. echo
  46. [ $RETVAL -eq 0 ] && touch $lockfile
  47. }
  48. stop()
  49. {
  50. echo -n $"Stopping mongod: "
  51. killproc $mongod -QUIT
  52. RETVAL=$?
  53. echo
  54. [ $RETVAL -eq 0 ] && rm -f $lockfile
  55. }
  56. restart () {
  57. stop
  58. start
  59. }
  60. ulimit -n 12000
  61. RETVAL=0
  62. case "$1" in
  63. start)
  64. start
  65. ;;
  66. stop)
  67. stop
  68. ;;
  69. restart|reload|force-reload)
  70. restart
  71. ;;
  72. condrestart)
  73. [ -f $lockfile ] && restart || :
  74. ;;
  75. status)
  76. status $mongod
  77. RETVAL=$?
  78. ;;
  79. *)
  80. echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
  81. RETVAL=1
  82. esac
  83. exit $RETVAL

4.添加文件执行权限

# chmod +x /etc/init.d/mongod

5.service命令

service mongod start

service mongod stop

service mongod restart

6.查看mongod服务的端口

netstat -npt | grep mongod

7.我操,哥哥也会写脚本了

8.这里我想把开机启动也写进去,奈何能力有限,开机启动还是只能放在 /etc/rc.local 下

# mongodb start
/root/mongodb/bin/mongod --repair
/root/mongodb/bin/mongod -f /etc/mongod.conf

9.linux果然博大精深

人气教程排行