ubuntu 2004下二进制方式安装MongoDB 5.0.13

官方手册:https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-ubuntu-tarball/

1.下载MongoDB二进制包

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2004-5.0.13.tgz

tar xvf mongodb-linux-x86_64-ubuntu2004-5.0.13 -C /usr/local

ln -s /usr/local/mongodb-linux-x86_64-ubuntu2004-5.0.13/ /usr/local/mongodb

2.关闭Transparent Huge Pages

 cat > /etc/systemd/system/disable-transparent-huge-pages.service <<EOF
[Unit]
Description=Disable Transparent Huge Pages (THP)
DefaultDependencies=no
After=sysinit.target local-fs.target
Before=mongod.service
[Service]
Type=oneshot
ExecStart=/bin/sh -c 'echo never | tee /sys/kernel/mm/transparent_hugepage/enabled > /dev/null'
[Install]
WantedBy=basic.target
EOF

生效

systemctl daemon-reload
systemctl enable --now disable-transparent-huge-pages.service

3.安装依赖包

apt install libcurl4 openssl liblzma5

4.添加账户

useradd mongod -r -s /usr/sbin/nologin -M

5.创建mongodb所需目录结构

mkdir /usr/local/mongodb/{conf,data,log}

6.设置mongodb目录权限

chown -R mongod:mongod /usr/local/mongodb-linux-x86_64-ubuntu2004-5.0.13/

7.创建YAML格式的配置文件

cat > /usr/local/mongodb/conf/mongo.conf <<EOF
systemLog:
  destination: file
  path: "/usr/local/mongodb/log/mongodb.log"
  logAppend: true
storage:
  dbPath: "/usr/local/mongodb/data/"
processManagement:
 fork : true
net:
  port: 27017
  bindIp: 0.0.0.0
security:
  authorization: enabled
EOF

配置文件解析

#日志相关
systemLog:
  destination: file
  path: "/usr/local/mongodb/log/mongodb.log"  #日志位置
  logAppend: true                             #追加日志
#数据存储有关
storage:
  dbPath: "/usr/local/mongodb/data/"          #数据路径的位置
#进程控制
processManagement:
 fork : true                                  #后台守护进程
#网络配置有关
net:
  port: 27017                                 #端口号,默认不配置端口号,是27017
  bindIp: 0.0.0.0                             #监听地址自MongoDB 3.6版本后默认监听在localhost
#安全验证有关配置
security:
  authorization: enabled                      #是否打开用户名密码验证,默认此项为关掉

8.设置PATH变量

echo PATH=/usr/local/mongodb/bin/:'$PATH' > /etc/profile.d/mongodb.sh
. /etc/profile.d/mongodb.sh

9.准备service文件

cat > /lib/systemd/system/mongod.service <<EOF
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
User=mongod
Group=mongod
ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/conf/mongo.conf
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/conf/mongo.conf --
shutdown
PrivateTmp=true
# 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 mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings
[Install]
WantedBy=multi-user.target
EOF

生效

systemctl daemon-reload
systemctl enable --now mongod

查看端口是否启动
image

posted @ 2022-10-25 15:32  wangxupeng  阅读(38)  评论(0编辑  收藏  举报