1
0

mongodb.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 7+ Debian 8+ and Ubuntu 16+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/oneinstack/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. if [ -e /bin/systemctl ]; then
  18. /bin/cp ${oneinstack_dir}/init.d/mongod.service /lib/systemd/system/
  19. sed -i "s@=/usr/local/mongodb@=${mongo_install_dir}@g" /lib/systemd/system/mongod.service
  20. systemctl enable mongod
  21. else
  22. [ "${PM}" == 'yum' ] && { /bin/cp ../init.d/MongoDB-init-RHEL /etc/init.d/mongod; sed -i "s@/usr/local/mongodb@${mongo_install_dir}@g" /etc/init.d/mongod; chkconfig --add mongod; chkconfig mongod on; }
  23. [ "${PM}" == 'apt-get' ] && { /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; }
  24. fi
  25. cat > /etc/mongod.conf << EOF
  26. # mongod.conf
  27. # for documentation of all options, see:
  28. # http://docs.mongodb.org/manual/reference/configuration-options/
  29. # where to write logging data.
  30. systemLog:
  31. destination: file
  32. logAppend: true
  33. path: ${mongo_data_dir}/mongod.log
  34. # Where and how to store data.
  35. storage:
  36. dbPath: ${mongo_data_dir}
  37. journal:
  38. enabled: true
  39. # engine:
  40. # mmapv1:
  41. # wiredTiger:
  42. # how the process runs
  43. processManagement:
  44. fork: true # fork and run in background
  45. pidFilePath: /var/run/mongodb/mongod.pid
  46. # network interfaces
  47. net:
  48. port: 27017
  49. bindIp: 0.0.0.0
  50. unixDomainSocket:
  51. enabled: false
  52. #security:
  53. # authorization: enabled
  54. #operationProfiling:
  55. #replication:
  56. #sharding:
  57. EOF
  58. service mongod start
  59. echo ${mongo_install_dir}/bin/mongo 127.0.0.1/admin --eval \"db.createUser\(\{user:\'root\',pwd:\'$dbmongopwd\',roles:[\'userAdminAnyDatabase\']\}\)\" | bash
  60. sed -i 's@^#security:@security:@' /etc/mongod.conf
  61. sed -i 's@^# authorization:@ authorization:@' /etc/mongod.conf
  62. if [ -e "${mongo_install_dir}/bin/mongo" ]; then
  63. sed -i "s+^dbmongopwd.*+dbmongopwd='$dbmongopwd'+" ../options.conf
  64. echo "${CSUCCESS}MongoDB installed successfully! ${CEND}"
  65. rm -rf mongodb-linux-${SYS_BIT_b}-${mongodb_ver}
  66. else
  67. rm -rf ${mongo_install_dir} ${mongo_data_dir}
  68. echo "${CFAILURE}MongoDB install failed, Please contact the author! ${CEND}" && lsb_release -a
  69. kill -9 $$; exit 1;
  70. fi
  71. popd
  72. [ -z "$(grep ^'export PATH=' /etc/profile)" ] && echo "export PATH=${mongo_install_dir}/bin:\$PATH" >> /etc/profile
  73. [ -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
  74. . /etc/profile
  75. }