0
点赞
收藏
分享

微信扫一扫

mongodb安装部署

1安装:

[root@instance-r5y0pf5d ~]# mkdir /data/soft -p #创建软件存放目录
[root@instance-r5y0pf5d ~]# mkdir /opt/mongo_cluster/ -p #创建软件安装目录
[root@instance-r5y0pf5d ~]# mkdir /opt/mongo_cluster/mongo_27017/{conf,log,pid} -p #创建配置文件日志文件路径
[root@instance-r5y0pf5d ~]# mkdir /data/mongo_cluster/mongo_27017 -p #创建数据文件路径
[root@instance-r5y0pf5d ~]# chown `whoami` /opt/mongo_cluster/ #授权目录
[root@instance-r5y0pf5d ~]# chown `whoami` /opt/mongo_cluster/mongo_27017/{conf,log,pid}
[root@instance-r5y0pf5d tools]# cd /data/soft
[root@instance-r5y0pf5d tools]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel80-4.2.13.tgz
[root@instance-r5y0pf5d soft]# ls
mongodb-linux-x86_64-rhel80-4.2.13.tgz
[root@instance-r5y0pf5d soft]# tar zxf mongodb-linux-x86_64-rhel80-4.2.13.tgz -C /opt/mongo_cluster/ #解压软件到指定目录
[root@instance-r5y0pf5d soft]# cd /opt/mongo_cluster
[root@instance-r5y0pf5d mongo_cluster]# ls
mongodb-linux-x86_64-rhel80-4.2.13
[root@instance-r5y0pf5d mongo_cluster]# ln -s mongodb-linux-x86_64-rhel80-4.2.13 mongodb #创建软连接
[root@instance-r5y0pf5d mongo_cluster]#

 

 2编写配置文件:

[root@db04 ~]# vim /opt/mongo_cluster/mongo_27017/conf/mongodb.conf

systemLog:

#日志路径

#重启后在旧日志末尾追加日志

#日志输出为文件

processManagement:

#后台运行

#pid文件

net:

#绑定外网

#端口

storage:

#数据目录

    indexBuildRetry: true

#是否开启回滚日志

        enabled: true

#将不同的数据库存放在不同的目录中

#如下配置仅对最新的wiredTiger引擎有效(3.0以上版本)

        engineConfig:

#限制mongodb对内存的使用量,默认为物理内存的一半

开启一个库都创建一个单独的目录

        collectionConfig:

#数据压索算法

        indexConfig:

#是否对经过排序的索引数据使用前缀压索

 

 3启动停止mongodb:

[root@db04 ~]# rm -rf /opt/mongo_cluster/mongo_27017/log/mongodb.log
[root@db04 ~]# rm -rf /data/mongo_cluster/mongo_27017/*
[root@db04 ~]# rm -rf /opt/mongo_cluster/mongo_27017/pid/mongodb.pid
[root@db04 ~]# /opt/mongo_cluster/mongodb/bin/mongod -f /opt/mongo_cluster/mongo_27017/conf/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 1515
child process started successfully, parent exiting
[root@db04 ~]#

[root@db04 mongodb]# /opt/mongo_cluster/mongodb/bin/mongod -f /opt/mongo_cluster/mongo_27017/conf/mongodb.conf --shutdown

4将mongodb启动命令添加至环境变量中并启动测试:

[root@db04 ~]# echo 'PATH=$PATH:/opt/mongo_cluster/mongodb/bin' >> /etc/profile
[root@db04 ~]# tail -1 /etc/profile
PATH=$PATH:/opt/mongo_cluster/mongodb/bin
[root@db04 ~]# source /etc/profile
[root@db04 ~]# mongo
MongoDB shell version v4.2.13
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("823ed2da-bc10-456b-944d-1b290ba63adf") }
MongoDB server version: 4.2.13
Server has startup warnings:
2021-04-30T11:56:43.808+0800 I STORAGE [initandlisten]
2021-04-30T11:56:43.808+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2021-04-30T11:56:43.808+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2021-04-30T11:56:43.808+0800 I STORAGE [initandlisten]
2021-04-30T11:56:43.808+0800 I STORAGE [initandlisten] ** WARNING: The configured WiredTiger cache size is more than 80% of available RAM.
2021-04-30T11:56:43.808+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/faq-memory-diagnostics-wt
2021-04-30T11:56:44.964+0800 I CONTROL [initandlisten]
2021-04-30T11:56:44.964+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2021-04-30T11:56:44.964+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2021-04-30T11:56:44.964+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2021-04-30T11:56:44.964+0800 I CONTROL [initandlisten]
2021-04-30T11:56:44.964+0800 I CONTROL [initandlisten]
2021-04-30T11:56:44.964+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2021-04-30T11:56:44.964+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2021-04-30T11:56:44.964+0800 I CONTROL [initandlisten]
2021-04-30T11:56:44.964+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 3757 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2021-04-30T11:56:44.964+0800 I CONTROL [initandlisten]
---
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()
---

> show databases
admin 0.000GB
config 0.000GB
local 0.000GB
>

 

 5警告优化:

(1)WARNING: You are running this process as the root user, which is not recommended.

创建一个普通用户,并授权这个普通用户对mongo权限,最后用这个普通用户启动mongo即可将警告消除

[root@db04 ~]# useradd mongo
[root@db04 ~]# echo '123456'|passwd --stdin mongo
[root@db04 ~]# chown -R mongo:mongo /opt/mongo_cluster
[root@db04 ~]# chown -R mongo:mongo /data/mongo_cluster
[root@db04 ~]# su mongo
[mongo@db04 root]$ /opt/mongo_cluster/mongodb/bin/mongod -f /opt/mongo_cluster/mongo_27017/conf/mongodb.conf
备注:
每次使用mongo用户登录数据库时都要执行上面的授权,否则没有权限,报错!

 (2)WARNING: The configured WiredTiger cache size is more than 80% of available RAM.

修改配置文件把cacheSizeGB的值调小:

[mongo@db04 root]$ grep "cacheSizeGB" /opt/mongo_cluster/mongo_27017/conf/mongodb.conf
cacheSizeGB: 1
[mongo@db04 root]$ vim /opt/mongo_cluster/mongo_27017/conf/mongodb.conf
[mongo@db04 root]$ grep "cacheSizeGB" /opt/mongo_cluster/mongo_27017/conf/mongodb.conf
cacheSizeGB: 0.5
[mongo@db04 root]$ /opt/mongo_cluster/mongodb/bin/mongod -f /opt/mongo_cluster/mongo_27017/conf/mongodb.conf --shutdown
[mongo@db04 root]$ /opt/mongo_cluster/mongodb/bin/mongod -f /opt/mongo_cluster/mongo_27017/conf/mongodb.conf

 (3)WARNING: Access control is not enabled for the database.

在配置文件中添加用户认证参数:

[mongo@db04 root]$ vim /opt/mongo_cluster/mongo_27017/conf/mongodb.conf
[mongo@db04 root]$ tail -n 2 /opt/mongo_cluster/mongo_27017/conf/mongodb.conf
security:
authorization: enabled
[mongo@db04 root]$ /opt/mongo_cluster/mongodb/bin/mongod -f /opt/mongo_cluster/mongo_27017/conf/mongodb.conf --shutdown
killing process with pid: 2442
[mongo@db04 root]$ /opt/mongo_cluster/mongodb/bin/mongod -f /opt/mongo_cluster/mongo_27017/conf/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 2481
child process started successfully, parent exiting
[mongo@db04 root]$ mongo

 (4)WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.

大内存页处理:

#查看状态:
[root@db04 ~]# cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
[root@db04 ~]# cat /sys/kernel/mm/transparent_hugepage/defrag
[always] madvise never

#临时关闭:
[root@db04 ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@db04 ~]# echo never > /sys/kernel/mm/transparent_hugepage/defrag
[root@db04 ~]# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
[root@db04 ~]# cat /sys/kernel/mm/transparent_hugepage/defrag
always madvise [never]
[root@db04 ~]#

#永久关闭:
[root@db04 ~]# vim /etc/init.d/disable-transparent-hugepages

[root@localhost ~]# vim /etc/rc.d/rc.local

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local

if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi


[root@localhost ~]# chmod +x /etc/rc.d/rc.local

#加入开机启动
[root@db04 ~]# chmod 755 /etc/init.d/disable-transparent-hugepages
[root@db04 ~]# chkconfig --add disable-transparent-hugepages

 (5)WARNING: soft rlimits too low. rlimits set to 3757 processes, 32000 files. Number of processes should be at least 16

[root@instance-r5y0pf5d ~]# vim /etc/security/limits.conf #永久生效
[root@instance-r5y0pf5d ~]# tail -n 4 /etc/security/limits.conf
root soft nofile 65535
root hard nofile 65535
* soft nofile 32000
* hard nofile 32000
[root@instance-r5y0pf5d ~]# ulimit -u 65535 #即时生效

 



举报

相关推荐

0 条评论