mongodb.sh 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.cn
  4. #
  5. # Notes: OneinStack for CentOS/RadHat 6+ Debian 6+ and Ubuntu 12+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/lj2007331/oneinstack
  10. Install_MongoDB() {
  11. pushd ${oneinstack_dir}/src > /dev/null
  12. id -u mongod >/dev/null 2>&1
  13. [ $? -ne 0 ] && useradd -s /sbin/nologin mongod
  14. mkdir -p ${mongo_data_dir};chown mongod.mongod -R ${mongo_data_dir}
  15. tar xzf mongodb-linux-${SYS_BIT_b}-${mongodb_ver}.tgz
  16. /bin/mv mongodb-linux-${SYS_BIT_b}-${mongodb_ver} ${mongo_install_dir}
  17. [ "${OS}" == "CentOS" ] && { /bin/cp ../init.d/MongoDB-init-CentOS /etc/init.d/mongod; sed -i "s@/usr/local/mongodb@${mongo_install_dir}@g" /etc/init.d/mongod; chkconfig --add mongod; chkconfig mongod on; }
  18. [[ "${OS}" =~ ^Ubuntu$|^Debian$ ]] && { /bin/cp ../init.d/MongoDB-init-Ubuntu /etc/init.d/mongod; sed -i "s@/usr/local/mongodb@${mongo_install_dir}@g" /etc/init.d/mongod; update-rc.d mongod defaults; }
  19. cat > /etc/mongod.conf << EOF
  20. # mongod.conf
  21. # for documentation of all options, see:
  22. # http://docs.mongodb.org/manual/reference/configuration-options/
  23. # where to write logging data.
  24. systemLog:
  25. destination: file
  26. logAppend: true
  27. path: ${mongo_data_dir}/mongod.log
  28. # Where and how to store data.
  29. storage:
  30. dbPath: ${mongo_data_dir}
  31. journal:
  32. enabled: true
  33. # engine:
  34. # mmapv1:
  35. # wiredTiger:
  36. # how the process runs
  37. processManagement:
  38. fork: true # fork and run in background
  39. pidFilePath: /var/run/mongodb/mongod.pid
  40. # network interfaces
  41. net:
  42. port: 27017
  43. bindIp: 0.0.0.0
  44. unixDomainSocket:
  45. enabled: false
  46. #security:
  47. # authorization: enabled
  48. #operationProfiling:
  49. #replication:
  50. #sharding:
  51. EOF
  52. service mongod start
  53. echo ${mongo_install_dir}/bin/mongo 127.0.0.1/admin --eval \"db.createUser\(\{user:\'root\',pwd:\'$dbmongopwd\',roles:[\'userAdminAnyDatabase\']\}\)\" | bash
  54. sed -i 's@^#security:@security:@' /etc/mongod.conf
  55. sed -i 's@^# authorization:@ authorization:@' /etc/mongod.conf
  56. if [ -e "${mongo_install_dir}/bin/mongo" ]; then
  57. sed -i "s+^dbmongopwd.*+dbmongopwd='$dbmongopwd'+" ../options.conf
  58. echo "${CSUCCESS}MongoDB installed successfully! ${CEND}"
  59. rm -rf mongodb-linux-${SYS_BIT_b}-${mongodb_ver}
  60. else
  61. rm -rf ${mongo_install_dir} ${mongo_data_dir} mongodb-linux-${SYS_BIT_b}-${mongodb_ver}
  62. echo "${CFAILURE}MongoDB install failed, Please contact the author! ${CEND}"
  63. kill -9 $$
  64. fi
  65. popd
  66. [ -z "$(grep ^'export PATH=' /etc/profile)" ] && echo "export PATH=${mongo_install_dir}/bin:\$PATH" >> /etc/profile
  67. [ -n "$(grep ^'export PATH=' /etc/profile)" -a -z "$(grep ${mongo_install_dir} /etc/profile)" ] && sed -i "s@^export PATH=\(.*\)@export PATH=${mongo_install_dir}/bin:\1@" /etc/profile
  68. . /etc/profile
  69. }