1、创建数据目录和日志目录
mkdir -p /opt/mongodb/{data,logs}
2、安装tar包所需依赖
yum install -y libcurl openssl xz-libs
3、下载mongodb二进制包并解压
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.4.tgz -P /tmp
tar -zxvf /tmp/mongodb-linux-x86_64-* -C /opt/mongodb --strip=1
4、配置环境变量
echo "PATH=$PATH:/opt/mongodb/bin" > /etc/profile.d/mongodb.sh
source /etc/profile.d/mongodb.sh
mongo --version
5、创建mongodb配置文件mongod.conf
wget https://raw.githubusercontent.com/mongodb/mongo/master/rpm/mongod.conf -P /opt/mongodb/
6、修改配置文件内容如下:
cat > /opt/mongodb/mongod.conf <<EOF
systemLog:
destination: file
logAppend: true
path: /opt/mongodb/logs/mongod.log
storage:
dbPath: /opt/mongodb/data
journal:
enabled: true
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 27017
bindIp: 0.0.0.0
#security:
EOF
7、创建mongodb启动用户:
groupadd mongod
useradd -g mongod mongod
8、配置mongodb目录权限
chown -R mongod:mongod /opt/mongodb
9、创建systemd启动配置文件
cd /usr/lib/systemd/system/
wget https://raw.githubusercontent.com/mongodb/mongo/master/rpm/mongod.service
10、修改启动文件内容如下:
cat > /usr/lib/systemd/system/mongod.service <<'EOF'
[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 /opt/mongodb/mongod.conf"
EnvironmentFile=-/etc/sysconfig/mongod
ExecStart=/opt/mongodb/bin/mongod $OPTIONS
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb
ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb
PermissionsStartOnly=true
PIDFile=/var/run/mongodb/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
EOF
11、启动mongodb服务并配置随系统启动
systemctl enable --now mongod
12、查看服务运行状态
systemctl status mongod
13、给mongod用户赋予systemd权限,用于启动mongdodb服务。
vi /usr/share/polkit-1/actions/org.freedesktop.systemd1.policy
把org.freedesktop.systemd1.manage-units节点下,defaults下的auth admin修改为yes
systemctl restart polkit
14、然后就可以去普通用户下启停服务
systemctl stop mongod
systemctl start mongod
