MongoDB 单机安装部署

文若2年前技术文章827

说明

本篇文章介绍 MongoDB 二进制安装的步骤,整个过程还是比较简单。

1. 下载安装包

进入 MongoDB 官网,获取安装包的下载链接:

https://www.mongodb.com/try/download/community 以下载 5.0.23 版本为例:

3288720edd4e4f27a2880950ced485c1.png

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-5.0.23.tgz

2. 安装数据库

解压缩包:

tar -zxvf mongodb-linux-x86_64-rhel70-5.0.23.tgz

drwxr-xr-x 2 root root    70 12月 19 10:21 bin -rw-r--r-- 1 root root 30608 11月 20 23:29 LICENSE-Community.txt -rw-r--r-- 1 root root 16726 11月 20 23:29 MPL-2 -rw-r--r-- 1 root root  1977 11月 20 23:29 README -rw-r--r-- 1 root root 77913 11月 20 23:29 THIRD-PARTY-NOTICES

mv mongodb-linux-x86_64-rhel70-5.0.23 /usr/local/mongodb

创建配置文件:

touch /usr/local/mongodb/mongod.conf

创建数据目录:

mkdir -p /data/mongodb/{data,logs,run}

创建 mongod 用户:

groupadd mongod
useradd -g mongod -s /sbin/nologin -r mongod

修改文件属组:

chown -R mongod:mongod /usr/local/mongodb/
chown -R mongod:mongod /data/mongodb/

配置环境变量:

echo "export PATH=\$PATH:/usr/local/mongodb/bin" >> /etc/profile
source /etc/profile

写入配置文件:

vi /usr/local/mongodb/mongod.conf

# where to write logging data. 
systemLog:
 destination: file
 logAppend: true
 path: /data/mongodb/logs/mongod.log
# Where and how to store data.
storage:
 dbPath: /data/mongodb/data
 journal:
   enabled: true

# how the process runs
processManagement:
 fork: true  # fork and run in background
 pidFilePath: /data/mongodb/run/mongod.pid  # location of pidfile

# network interfaces
net:
 port: 27017
 bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
security:
 authorization: enabled

启动 MongoDB:

PS:可以不用启动,直接配置 systemctl   : )

/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/mongod.conf

about to fork child process, waiting until server is ready for connections. forked process: 28580 child process started successfully, parent exiting

关闭 MongoDB:

/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/mongod.conf --shutdown

3. 配置 systemctl

配置前需要把刚启动的 MongoDB 手动关闭掉,否则测试会报错。

vi /usr/lib/systemd/system/mongod.service

将下面内容,写入 mongod.service 中。

[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network-online.target
Wants=network-online.target


[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /usr/local/mongodb/mongod.conf"
ExecStart=/usr/local/mongodb/bin/mongod $OPTIONS
PermissionsStartOnly=true
PIDFile=/data/mongodb/run/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 mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings


[Install]
WantedBy=multi-user.target

重新加载配置:

systemctl daemon-reload
# 启动
systemctl start mongod
# 查看状态
systemctl status mongod
# 设置开机自启
systemctl enable mysqld

PS:如果使用 systemctl 启动失败,通常是目录授权问题,或者是未删除 mongod.lock 找到它并删除再重试。

4. 创建 root 用户

配置文件中开启了授权,所以进入 MongoDB 后需要先创建 root 高权限账号。

use admin
db.createUser ( {user: "root",pwd: "admin123",roles: [{role: "root", db: "admin"}]})


相关文章

CDH实操--修改集群主机名

CDH实操--修改集群主机名

1、停止集群2、停止cmsystemctl stop cloudera-scm-serversystemctl stop cloudera-scm-agnet3、修改服务器主机名修改下列配置文件下的主...

使用CoreDNS搭建DNS服务器

使用CoreDNS搭建DNS服务器

简介CoreDNS是一个DNS服务器/转发器,用Go编写,可以链接插件。每个插件执行一个 (DNS) 功能。CoreDNS是云原生计算基金会毕业的项目。CoreDNS是一个快速灵活的DNS服务器。这里...

grafana7 监控https证书过期时间

grafana7 监控https证书过期时间

使用 Grafana 7 中new table visualization功能,使用Prometheus监视证书的到期日期,并使用Grafana进行展示。所有证书一目了然:证书到期之前的剩余时间,HT...

Presto临时设置session超时时间

Presto临时设置session超时时间

全局默认超时时间为200s:测试语句:select    "t7"."__fcol_16" "__fcol_22",    date_trunc('month', "t7"."__fcol_21"...

副本集的同步、心跳、选举、回滚

一、副本集同步过程   MongoDB的复制功能是依赖与oplog来实现的。   1.数据写入主节点同时在local.oplog.rs集合写入对应的oplog   2.备份节点第一次同步数据时,会先进...

Clickhouse表引擎介绍

Clickhouse表引擎介绍

引擎分类ClickHouse表引擎一共分为四个系列,分别是Log、MergeTree、Integration、Special。其中包含了两种特殊的表引擎Replicated、Distributed,功...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。