目录
01.创建相关目录
安装目录:/data/mongodb/app
数据目录:/data/mongodb/data
日志目录:/data/mongodb/log
配置文件目录:/data/mongodb/conf
mkdir -pv /data/mongodb/{app,data,log,conf}
02.创建系统mongodb用户
groupadd mongodb
useradd -r -g mongodb -s /bin/false mongodb
03.解压mongodb压缩包
下载地址:https://www.mongodb.com/try/download/community
上传压缩包到/data/mongodb/app目录
解压:
cd /data/mongodb/app
tar -zxvf mongodb-linux-x86_64-rhel70-4.0.28.tgz -C /data/mongodb/app/
[root@localhost app]# tar -zxvf mongodb-linux-x86_64-rhel70-4.0.28.tgz -C /data/mongodb/app/
mongodb-linux-x86_64-rhel70-4.0.28/THIRD-PARTY-NOTICES.gotools
mongodb-linux-x86_64-rhel70-4.0.28/README
mongodb-linux-x86_64-rhel70-4.0.28/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel70-4.0.28/MPL-2
mongodb-linux-x86_64-rhel70-4.0.28/LICENSE-Community.txt
mongodb-linux-x86_64-rhel70-4.0.28/bin/mongodump
mongodb-linux-x86_64-rhel70-4.0.28/bin/mongorestore
mongodb-linux-x86_64-rhel70-4.0.28/bin/mongoexport
mongodb-linux-x86_64-rhel70-4.0.28/bin/mongoimport
mongodb-linux-x86_64-rhel70-4.0.28/bin/mongostat
mongodb-linux-x86_64-rhel70-4.0.28/bin/mongotop
mongodb-linux-x86_64-rhel70-4.0.28/bin/bsondump
mongodb-linux-x86_64-rhel70-4.0.28/bin/mongofiles
mongodb-linux-x86_64-rhel70-4.0.28/bin/mongoreplay
mongodb-linux-x86_64-rhel70-4.0.28/bin/mongod
mongodb-linux-x86_64-rhel70-4.0.28/bin/mongos
mongodb-linux-x86_64-rhel70-4.0.28/bin/mongo
mongodb-linux-x86_64-rhel70-4.0.28/bin/install_compass
04.设置软链接
ln -s mongodb-linux-x86_64-rhel70-4.0.28 mongodb
ll
[root@localhost app]# ll
total 104272
lrwxrwxrwx. 1 mongodb mongodb 34 Sep 1 16:17 mongodb -> mongodb-linux-x86_64-rhel70-4.0.28
drwxr-xr-x. 3 mongodb mongodb 135 Sep 1 16:17 mongodb-linux-x86_64-rhel70-4.0.28
-rw-r--r--. 1 mongodb mongodb 106773511 Sep 1 16:11 mongodb-linux-x86_64-rhel70-4.0.28.tgz
05.配置环境变量
vi /etc/profile
添加
export PATH=$PATH:/data/mongodb/app/mongodb/bin
. /etc/profile
06.准备mongodb配置文件
vi /data/mongodb/conf/mongod.conf
storage:
dbPath: "/data/mongodb/data"
systemLog:
destination: file
path: "/data/mongodb/log/mongod.log"
logAppend: true
net:
port: 27017
bindIpAll: true
maxIncomingConnections: 10240
processManagement:
fork: true
pidFilePath: /data/mongodb/data/mongodb.pid
注意配置文件注意格式,见下文
07.对相关目录就行授权
chown -R mongodb.mongodb /data/mongodb
#chown -R mongodb.mongodb /tmp/mongodb-27017.sock
08.设置systemd管理mongod
vi /usr/lib/systemd/system/mongod.service
[Unit]
Description=mongodb Server
After=network.target
[Service]
Type=forking
User=mongodb
Group=mongodb
PIDFile=/data/mongodb/data/mongodb.pid
ExecStart=/data/mongodb/app/mongodb/bin/mongod --config /data/mongodb/conf/mongod.conf
#ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/data/mongodb/app/mongodb/bin/mongod --shutdown --config /data/mongodb/conf/mongod.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
09.初始化启动mongod服务
方法一:
/data/mongodb/app/mongodb/bin/mongod --config /data/mongodb/conf/mongod.conf
或者
/data/mongodb/app/mongodb/bin/mongod -f /data/mongodb/conf/mongod.conf
方法二:
systemctl start mongod
#关闭mongod服务
/data/mongodb/app/mongodb/bin/mongod --shutdown --config /data/mongodb/conf/mongod.conf
或者
/data/mongodb/app/mongodb/bin/mongod --shutdown -f /data/mongodb/conf/mongod.conf
systemctl stop mongod
10.查看mongod服务
systemctl status mongod
ps aux|grep mongod
[root@localhost app]# systemctl status mongod
● mongod.service - mongodb Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2022-09-01 16:18:16 CST; 20s ago
Process: 1939 ExecStart=/data/mongodb/app/mongodb/bin/mongod --config /data/mongodb/conf/mongod.conf (code=exited, status=0/SUCCESS)
Main PID: 1941 (mongod)
CGroup: /system.slice/mongod.service
└─1941 /data/mongodb/app/mongodb/bin/mongod --config /data/mongodb/conf/mongod.conf
Sep 01 16:18:15 localhost.localdomain systemd[1]: Starting mongodb Server...
Sep 01 16:18:16 localhost.localdomain mongod[1939]: about to fork child process, waiting until server is ready for connections.
Sep 01 16:18:16 localhost.localdomain mongod[1939]: forked process: 1941
Sep 01 16:18:16 localhost.localdomain systemd[1]: Started mongodb Server.
[root@localhost app]#
[root@localhost app]#
[root@localhost app]# ps aux|grep mongod
mongodb 1991 0.2 4.1 1096360 76660 ? Sl 16:18 0:09 /data/mongodb/app/mongodb/bin/mongod --config /data/mongodb/conf/mongod.conf
root 2102 0.0 0.0 112808 964 pts/0 S+ 17:15 0:00 grep --color=auto mongod
11.登录验证
初次安装输入 mongo 即可进入数据库,执行db.version()输出mongodb版本即为安装成功
[root@localhost app]# mongo
MongoDB shell version v4.0.28
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c4618c98-cdcc-474f-b15c-fa43d5bd100c") }
MongoDB server version: 4.0.28
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2022-09-01T16:18:56.006+0800 I CONTROL [initandlisten]
2022-09-01T16:18:56.006+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2022-09-01T16:18:56.006+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2022-09-01T16:18:56.006+0800 I CONTROL [initandlisten]
2022-09-01T16:18:56.006+0800 I CONTROL [initandlisten]
2022-09-01T16:18:56.006+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2022-09-01T16:18:56.006+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2022-09-01T16:18:56.006+0800 I CONTROL [initandlisten]
2022-09-01T16:18:56.006+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2022-09-01T16:18:56.006+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2022-09-01T16:18:56.006+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()
---
> db.version()
4.0.28
>
> db
test
>
12.注意事项
mongodb 3.0之后配置文件采用YAML格式,这种格式非常简单,使用<key>:<value>表示,开头使用“空格”作为缩进。需要注意的是,“:”之后有value的话,需要紧跟一个空格,如果key只是表示层级,则无需在“:”后增加空格(比如:systemLog:后面既不需要空格)。按照层级,每行4个空格缩进,第二级则8个空格,依次轮推,顶层则不需要空格缩进。如果格式不正确,将会出现错误
若之前有安装过mongodb,可能在/tmp目录下有/tmp/mongodb-27017.sock文件,初始化启动mongod可能报错,解决办法是第7步的授权:
chown -R mongodb.mongodb /tmp/mongodb-27017.sock