预备工作

  1. #目录编排
  2. /opt/mongo_27017/conf #配置文件目录
  3. /opt/mongo_27017/pid #pid位置目录
  4. /opt/mongo_27017/logs #日志存放目录
  5. /data/mongo_27017/ #数据目录

安装

  1. #获取mongodb
  2. https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.3.tgz
  3. #解压到opt下
  4. tar xf mongodb-linux-x86_64-rhel70-4.2.3.tgz -C /opt/
  5. #创建软链接
  6. ln -s mongodb-linux-x86_64-rhel70-4.2.3 mongodb
  7. #将mongodb添加到环境变量
  8. cat > /etc/profile.d/mongodb.sh <<EOF
  9. export PATH="/opt/mongodb/bin:$PATH"
  10. EOF

配置

  1. cat >/opt/mongo_27017/conf/mongodb.conf<<EOF
  2. systemLog:
  3. destination: file
  4. logAppend: true
  5. path: /opt/mongo_27017/log/mongodb.log
  6. storage:
  7. journal:
  8. enabled: true
  9. dbPath: /data/mongo_27017
  10. directoryPerDB: true
  11. wiredTiger:
  12. engineConfig:
  13. cacheSizeGB: 1
  14. directoryForIndexes: true
  15. collectionConfig:
  16. blockCompressor: zlib
  17. indexConfig:
  18. prefixCompression: true
  19. processManagement:
  20. fork: true
  21. pidFilePath: /opt/mongo_27017/pid/mongod.pid
  22. net:
  23. port: 27017
  24. bindIp: 127.0.0.1,10.0.0.51
  25. EOF

配置文件解释:

  1. systemLog:
  2. destination: file #Mongodb 日志输出的目的地,指定一个file或者syslog,如果指定file,必须指定
  3. logAppend: true #当实例重启时,不创建新的日志文件, 在老的日志文件末尾继续添加
  4. path: /opt/mongo_27017/logs/mongodb.log #日志路径
  5. storage:
  6. journal: #回滚日志
  7. enabled: true
  8. dbPath: /data/mongo_27017 #数据存储目录
  9. directoryPerDB: true #默认,false不适用inmemoryengine
  10. wiredTiger:
  11. engineConfig:
  12. cacheSizeGB: 1 #将用于所有数据缓存的最大小
  13. directoryForIndexes: true #默认false索引集合storage.dbPath存储在数据单独子目录
  14. collectionConfig:
  15. blockCompressor: zlib
  16. indexConfig:
  17. prefixCompression: true
  18. processManagement: #使用处理系统守护进程的控制处理
  19. fork: true #后台运行
  20. pidFilePath: /opt/mongo_27017/pid/mongod.pid #创建 pid 文件
  21. net:
  22. port: 27017 #监听端口
  23. bindIp: 127.0.0.1,10.0.0.51 #绑定ip

启动

#启动后台进程
mongod  -f /opt/mongo_27017/conf/mongodb.conf

#进入mongodb
mongo

还可以设置使用systemctl来管理mongdb进程

#创建用户mongo
groupadd -g 777 mongo
useradd -u 777 -g 777 -s /sbin/nologin -M mongo

#添加配置文件
cat >/usr/lib/systemd/system/mongod.service <<EOF
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target

[Service]
User=mongo
Group=mongo
ExecStart=/opt/mongodb/bin/mongod -f /opt/mongo_27017/conf/mongodb.conf
ExecStartPre=/usr/bin/chown -R mongo:mongo /opt/mongo_27017/
ExecStartPre=/usr/bin/chown -R mongo:mongo /data/mongo_27017/

PermissionsStartOnly=true
PIDFile=/opt/mongo_27017/pid/mongod.pid
Type=forking
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings

[Install]
WantedBy=multi-user.target
EOF

#使生效
systemctl daemon-reload

关于mongodb出现warning的解决方法

1.内存不足
** WARNING: The configured WiredTiger cache size is more than 80% of available RAM.
See http://dochub.mongodb.org/core/faq-memory-diagnostics-wt
解决方法1: 加大机器内存
解决方法2: 调小配置文件里的缓存大小 cacheSizeGB: 0.5

2.没有开启访问控制
** WARNING: Access control is not enabled for the database.
Read and write access to data and configuration is unrestricted.
解决方法:
开启安全账户功能

3.不建议以root身份运行
** WARNING: You are running this process as the root user, which is not recommended.
解决方法1: 创建普通用户mongo,然后切换到mongo用户启动
解决方法2: 使用system方式登陆,指定运行用户为普通用户mongo

4.关闭大内存页
WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always’.
We suggest setting it to ‘never’
WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always’.
We suggest setting it to ‘never’
解决方法:改完要重启
echo “never” > /sys/kernel/mm/transparent_hugepage/enabled
echo “never” > /sys/kernel/mm/transparent_hugepage/defrag

5.数据目录磁盘不是XFS格式
** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
解决方法:
把磁盘格式转成XFS

6.rlimits太低
** WARNING: soft rlimits too low. rlimits set to 7266 processes, 100001 files. Number of processes should be at least 50000.5 : 0.5 times number of files.
解决方法:
vim /etc/profile
ulimit -f unlimited
ulimit -t unlimited
ulimit -v unlimited
ulimit -n 64000
ulimit -m unlimited
ulimit -u 64000

生效配置:
source /etc/profile

验证:
systemctl stop mongod
systemctl start mongod
mongo

7.关闭监控服务体验
—-
Enable MongoDB’s free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
—-
解决方法:
db.disableFreeMonitoring()