1
0

mongodb.sh 2.5 KB

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