MongoDB数据库安装部署及警告优化

MongoDB安装部署及警告优化

1.软件下载

3.6.13版本:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.13.tgz

4.0.14版本:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.14.tgz

官网文档:https://docs.mongodb.com/manual/

2.部署MongoDB

2.1.规划部署目录

程序目录
[[email protected] ~]# mkdir /data/mongodb_cluster/mongodb_27017/{conf,data,logs,pid} -p

软件目录
[[email protected] ~]# mkdir /data/soft

[[email protected] ~]# tree /data/
/data/
├── mongodb_cluster
│   └── mongodb_27017
│       ├── conf
│       ├── data
│       ├── logs
│       └── pid
└── soft

7 directories, 0 files

2.2.下载软件包

[[email protected] ~]# cd /data/soft/
[[email protected] /data/soft]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.14.tgz

2.3.安装MongoDB

MongoDB下载后直接解压即可使用,不包含配置文件,需要自己创建

1.解压MongoDB
[[email protected] /data/soft]# tar xf mongodb-linux-x86_64-4.0.14.tgz -C /data/mongodb_cluster/

2.制作软连接
[[email protected] /data/soft]# cd /data/mongodb_cluster/
[[email protected] /data/mongodb_cluster]# ln -s mongodb-linux-x86_64-4.0.14/ mongodb

3.创建MongoDB配置文件目录
[[email protected] ~]# mkdir /data/mongodb_cluster/mongodb_27017/{conf,data,logs,pid} -p
[[email protected] ~]# tree /data/ -d
/data/
├── mongodb_cluster
│   ├── mongodb -> mongodb-linux-x86_64-4.0.14/
│   ├── mongodb_27017
│   │   ├── conf
│   │   ├── data
│   │   ├── logs
│   │   └── pid
│   └── mongodb-linux-x86_64-4.0.14
│       └── bin
└── soft

2.4.MongoDB配置文件介绍

配置文件注解:
systemLog:
  destination: file 	//Mongodb日志输出为文件
  logAppend: true 			//当实例重启时,不创建新的日志文件, 在老的日志文件末尾继续添加
  path: /data/mongodb_cluster/mongodb_27017/logs/mongodb.log 		//日志路径

storage:
  journal: 				//回滚日志,类似于mysql的binlog
    enabled: true			//开启回滚日志
  dbPath: /data/mongodb_cluster/mongodb_27017/data 		//数据存储目录
  directoryPerDB: true 		//默认,false不适用inmemoryengine
  wiredTiger:				//存储引擎
    engineConfig:
      cacheSizeGB: 1 		//将用于所有数据缓存的大小
      directoryForIndexes: true 	//默认false索引集合storage.dbPath存储在数据单独子目录,这里必须配置为true,否则所有库的数据文件都会存放在一个目录中
    collectionConfig:
      blockCompressor: zlib			//开启压缩
    indexConfig:	
      prefixCompression: true			//开启索引

processManagement: 			//系统守护进程控制处理
  fork: true 			//后台运行
  pidFilePath: /data/mongodb_cluster/mongodb_27017/pid/mongod.pid   //pid文件路径

net:
  port: 27017 			//监听端口
  bindIp: 127.0.0.1,192.168.81.210			//绑定ip

2.5.编写MongoDB配置文件

[[email protected] ~]# cd /data/mongodb_cluster/mongodb_27017/
[[email protected] /data/mongodb_cluster/mongodb_27017]# vim conf/mongodb.yml
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb_cluster/mongodb_27017/logs/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /data/mongodb_cluster/mongodb_27017/data
  directoryPerDB: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /data/mongodb_cluster/mongodb_27017/pid/mongod.pid

net:
  port: 27017
  bindIp: 127.0.0.1,192.168.81.210

2.6.启动MongoDB

1.启动MongoDB
[[email protected] ~]# cd /data/mongodb_cluster
[[email protected] /data/mongodb_cluster]# ./mongodb/bin/mongod -f mongodb_27017/conf/mongodb.yml 
about to fork child process, waiting until server is ready for connections.
forked process: 73550
child process started successfully, parent exiting

2.查看进程和端口
[[email protected] /data/mongodb_cluster]# ps aux | grep mongo
[[email protected] /data/mongodb_cluster]# netstat -lnpt | grep mongo

在这里插入图片描述

2.7.如何关闭MongoDB

直接用启动命令后面加一个–shutdown即可关闭MongoDB

两种方式关闭MongoDB

1.命令行关闭MongoDB
[[email protected] ~]# cd /data/mongodb_cluster
[[email protected] /data/mongodb_cluster]# ./mongodb/bin/mongod -f mongodb_27017/conf/mongodb.yml --shutdown
killing process with pid: 73550

2.交互式关闭MongoDB
[[email protected] ~]$mongo
> use admin
switched to db admin
> db.shutdownServer()

查看进程和端口
[[email protected] /data/mongodb_cluster]# ps aux | grep mongo
[[email protected] /data/mongodb_cluster]# netstat -lnpt | grep mongo

2.8.登录MongoDB

登录MongoDB需要使用mongo命令

1.启动MongoDB
[[email protected] ~]# cd /data/mongodb_cluster
[[email protected] /data/mongodb_cluster]# ./mongodb/bin/mongod -f mongodb_27017/conf/mongodb.yml 

2.登录MongoDB
[[email protected] /data/mongodb_cluster]# ./mongodb/bin/mongo

登录后会有一些警告信息,我们再3里面进行优化

在这里插入图片描述

3.优化MongoDB警告信息

3.1.优化启动用户警告

警告内容:2021-02-13T10:44:47.832+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

这个警告内容就提示我们尽量不要用root直接启动,我们需要用普通用户启动

思路:创建一个普通用户,将MongoDB的部署目录赋权给普通用户,用普通用户启动即可

1.关闭MongoDB
[[email protected] /data/mongodb_cluster]# ./mongodb/bin/mongod -f mongodb_27017/conf/mongodb.yml --shutdown

2.创建用户
[[email protected] ~]# useradd mongo
[[email protected] ~]# echo "123456" | passwd --stdin mongo

3.赋权
[[email protected] ~]# chown -R mongo.mongo /data/mongodb_cluster/

4.登陆普通用户并配置环境变量
[[email protected] ~]# su - mongo
[[email protected] ~]$ vim .bashrc
export PATH=/data/mongodb_cluster/mongodb/bin/:$PATH
[[email protected] ~]$ source .bashrc

5.启动MongoDB
[[email protected] ~]$ mongod -f /data/mongodb_cluster/mongodb_27017/conf/mongodb.yml 
about to fork child process, waiting until server is ready for connections.
forked process: 73835
child process started successfully, parent exiting

6.登陆MongoDB
[[email protected] ~]$ mongo

可以到启动用户的警告信息已经消失

在这里插入图片描述

3.2.优化大内存页警告

告警内容:** 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'

这是由于大内存设置了always,MongoDB建议使用never

在这里插入图片描述

3.2.1.永久关闭大内存页

官方文档:https://docs.mongodb.com/manual/tutorial/transparent-huge-pages/

1.准备init脚本
[[email protected] ~]# vim /etc/init.d/disable-transparent-hugepages
#!/bin/bash
### BEGIN INIT INFO
# Provides:          disable-transparent-hugepages
# Required-Start:    $local_fs
# Required-Stop:
# X-Start-Before:    mongod mongodb-mms-automation-agent
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description:       Disable Linux transparent huge pages, to improve
#                    database performance.
### END INIT INFO

case $1 in
  start)
    if [ -d /sys/kernel/mm/transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/transparent_hugepage
    elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/redhat_transparent_hugepage
    else
      return 0
    fi

    echo 'never' | tee ${thp_path}/enabled > /dev/null

    unset thp_path
    ;;
esac

2.赋权并添加为开机自启
[[email protected] ~]# chmod 755 /etc/init.d/disable-transparent-hugepages
[[email protected] ~]# chkconfig --add disable-transparent-hugepages
[[email protected] ~]# chkconfig --list | grep disa

在这里插入图片描述

3.重启MongoDB
[[email protected] ~]$ mongod -f /data/mongodb_cluster/mongodb_27017/conf/mongodb.yml --shutdown
[[email protected] ~]$ mongod -f /data/mongodb_cluster/mongodb_27017/conf/mongodb.yml 

4.登陆mongdo查看警告
[[email protected] ~]$ mongo

3.2.2.临时关闭大内存页

1.临时关闭内存页
[[email protected] ~]# echo "never" > /sys/kernel/mm/transparent_hugepage/enabled
[[email protected] ~]# echo "never" > /sys/kernel/mm/transparent_hugepage/defrag
[[email protected] ~]# cat /sys/kernel/mm/transparent_hugepage/enabled 
always madvise [never]
[[email protected] ~]# cat /sys/kernel/mm/transparent_hugepage/defrag 
always madvise [never]

2.重启MongoDB
[[email protected] ~]# su - mongo
[[email protected] ~]$ mongod -f /data/mongodb_cluster/mongodb_27017/conf/mongodb.yml --shutdown
[[email protected] ~]$ mongod -f /data/mongodb_cluster/mongodb_27017/conf/mongodb.yml

3.登陆MongoDB查看告警
[[email protected] ~]$ mongo

可以看到大内存页警告已经接解决

在这里插入图片描述

3.3.优化limit警告

告警内容:** WARNING: soft rlimits too low. rlimits set to 15324 processes, 65535 files. Number of processes should be at least 32767.5: 0.5 times number of files.

提示的是limt设置的打开文件数太低

1.调整limit(此方法是不重启机器的情况下生效)
cat > /etc/profile<<EOF
ulimit -f unlimited
ulimit -t unlimited
ulimit -v unlimited
ulimit -n 64000
ulimit -m unlimited
ulimit -u 64000
EOF

source /etc/profile

2.重启MongoDB
[[email protected] ~]# su - mongo
[[email protected] ~]$ mongod -f /data/mongodb_cluster/mongodb_27017/conf/mongodb.yml --shutdown
[[email protected] ~]$ mongod -f /data/mongodb_cluster/mongodb_27017/conf/mongodb.yml


本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>