Centos8 安装mongodb
2021-12-21 17:54 方斌 阅读(537) 评论(0) 编辑 收藏 举报Centos8 安装mongodb
下载
官方下载地址:https://www.mongodb.com/download-center/community
上传
解压
tar zxvf mongodb-linux-x86_64-rhel80-5.0.5.tgz
配置
cd /usr/local/src
tar zxvf mongodb-linux-x86_64-rhel80-5.0.5.tgz
mv mongodb-linux-x86_64-rhel80-5.0.5 mongodb
创建目录
mkdir -p /usr/local/src/mongodb/data/db
mkdir -p /usr/local/src/mongodb/logs
touch /usr/local/src/mongodb/logs/mongodb.log
创建配置文件
vim /usr/local/src/mongodb/mongodb.conf
#端口号
port=27017
#db目录
dbpath=/usr/local/src/mongodb/data/db
#日志目录
logpath=/usr/local/src/mongodb/logs/mongodb.log
#后台
fork=true
#日志输出
logappend=true
#允许远程IP连接
bind_ip=0.0.0.0
启动
/usr/local/src/mongodb/bin/mongod --config /usr/local/src/mongodb/mongodb.conf
连接测试
/usr/local/src/mongodb/bin/mongo
> use aaa
switched to db aaa
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> db.createCollection("col001")
{ "ok" : 1 }
> db.col001.insert({"name":"张三"})
WriteResult({ "nInserted" : 1 })
> db.col001.find()
{ "_id" : ObjectId("61c212f7c0e2db79d8ff648c"), "name" : "张三" }
>
配置 service服务
vim mongodb.service
[Unit]
Description=mongodb
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/src/mongodb/bin/mongod --config /usr/local/src/mongodb/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/src/mongodb/bin/mongod --shutdown --config /usr/local/src/mongodb/mongodb.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod u+x mongodb.service
cp mongodb.service /lib/systemd/system
设置开机启动
[root@mysql8 mongodb]# ps -ef | grep ongodb
root 12626 1 0 01:16 ? 00:00:02 ./bin/mongod --config mongodb.conf
root 12689 1648 0 01:22 pts/0 00:00:00 grep --color=auto ongodb
[root@mysql8 mongodb]# kill -9 12626
[root@mysql8 mongodb]# ps -ef | grep ongodb
root 12691 1648 0 01:22 pts/0 00:00:00 grep --color=auto ongodb
[root@mysql8 mongodb]#
[root@mysql8 mongodb]# ps -ef | grep mongodb
root 12693 1648 0 01:22 pts/0 00:00:00 grep --color=auto mongodb
[root@mysql8 mongodb]#
[root@mysql8 mongodb]# systemctl stop mongodb
[root@mysql8 mongodb]# systemctl start mongodb
[root@mysql8 mongodb]# ps -ef | grep mongodb
root 12700 1 17 01:22 ? 00:00:01 /usr/local/src/mongodb/bin/mongod --config /usr/local/src/mongodb/mongodb.conf
root 12741 1648 0 01:22 pts/0 00:00:00 grep --color=auto mongodb
[root@mysql8 mongodb]# systemctl status mongodb
● mongodb.service - mongodb
Loaded: loaded (/usr/lib/systemd/system/mongodb.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2021-12-22 01:22:53 CST; 15s ago
Process: 12698 ExecStart=/usr/local/src/mongodb/bin/mongod --config /usr/local/src/mongodb/mongodb.conf (code=exited, status=0/SUCCESS)
Main PID: 12700 (mongod)
Tasks: 34 (limit: 4774)
Memory: 162.7M
CGroup: /system.slice/mongodb.service
└─12700 /usr/local/src/mongodb/bin/mongod --config /usr/local/src/mongodb/mongodb.conf
Dec 22 01:22:52 mysql8 systemd[1]: Starting mongodb...
Dec 22 01:22:52 mysql8 mongod[12698]: about to fork child process, waiting until server is ready for connections.
Dec 22 01:22:52 mysql8 mongod[12698]: forked process: 12700
Dec 22 01:22:53 mysql8 mongod[12698]: child process started successfully, parent exiting
Dec 22 01:22:53 mysql8 systemd[1]: Started mongodb.
[root@mysql8 mongodb]#
[root@mysql8 mongodb]#
[root@mysql8 mongodb]# ps -ef | grep mongodb
root 12700 1 5 01:22 ? 00:00:01 /usr/local/src/mongodb/bin/mongod --config /usr/local/src/mongodb/mongodb.conf
root 12745 1648 0 01:23 pts/0 00:00:00 grep --color=auto mongodb
[root@mysql8 mongodb]#
[root@mysql8 mongodb]# systemctl enable mongodb.service
Created symlink /etc/systemd/system/multi-user.target.wants/mongodb.service → /usr/lib/systemd/system/mongodb.service.
[root@mysql8 mongodb]#
添加环境变量
修改profile文件:
cat >>/etc/profile<<"EOF"
export PATH=$PATH:/usr/local/src/mongodb/bin
EOF
#刷新profile文件:
source /etc/profile