mongodb安装

本文档详细记录了在Linux环境下安装MongoDB的过程,包括解压安装包、配置环境变量、创建数据目录、编写配置文件、启动服务、创建数据库用户以及设置服务开机自启。此外,还展示了通过systemctl命令管理MongoDB服务的状态。
摘要由CSDN通过智能技术生成

mongodb安装

1. 解压软件包

[root@oracledb ~]# tar xf mongodb-linux-x86_64-rhel70-5.0.5.tgz  -C /usr/local/ 
[root@oracledb ~]# cd /usr/local/
## 重命名
[root@oracledb local]# mv mongodb-linux-x86_64-rhel70-5.0.5/ mongodb
[root@oracledb local]# ls
bin  games    jdk  lib64    mongodb  openresty  sbin   src
etc  include  lib  libexec  mysql    redis      share  tomcat9

2.配置环境变量

[root@oracledb ~]# vim /etc/profile
export MONGO_HOME=/usr/local/mongodb
export PATH=$PATH:$MONGO_HOME/bin
[root@oracledb ~]# vim /etc/profile
[root@oracledb ~]# mongo
mongo   mongod  mongos

3.创建数据存放目录

[root@oracledb ~]# mkdir /data
[root@oracledb ~]# cd /data 
[root@oracledb data]# mkdir -p mongodb/db 
[root@oracledb data]# mkdir -p mongodb/logs

4.创建配置文件

[root@oracledb data]# vim /usr/local/mongodb/mongodb.conf 
dbpath = /data/mongodb/db
logpath = /data/mongodb/logs/mongodb.log
port = 27017
fork = true
bind_ip = 192.168.18.132
auth = true
wiredTigerCacheSizeGB=2

##启动服务
[root@oracledb data]# mongod --config /usr/local/mongodb/mongodb.conf 
about to fork child process, waiting until server is ready for connections.
forked process: 2114
child process started successfully, parent exiting
[root@oracledb data]# ss -antl 
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128          *:22                       *:*                  
LISTEN      0      100    127.0.0.1:25                       *:*                  
LISTEN      0      128    192.168.18.132:27017                    *:*                  
LISTEN      0      128       [::]:22                    [::]:*                  
LISTEN      0      100      [::1]:25                    [::]:*  

[root@oracledb data]# mongo --host 192.168.18.132 --port 27017
MongoDB shell version v5.0.5
connecting to: mongodb://192.168.18.132:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("05341d37-e12d-4b60-a7f7-2d07d2cafa1b") }
MongoDB server version: 5.0.5
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
	https://community.mongodb.com
> 

##创建用户
> use admin
switched to db admin
> db.createUser({user:"root",pwd:"123456",roles:[{role:"root",db:"admin"}]})
Successfully added user: {
	"user" : "root",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}
> 

5.创建服务控制文件

[root@oracledb bin]# vim /usr/lib/systemd/system/mongodb.service 
[Unit]
Description=Mongodb server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf --shutdown

[Install]
WantedBy=multi-user.target
[root@oracledb data]# systemctl daemon-reload 

[root@oracledb bin]# systemctl start mongodb 
[root@oracledb bin]# ss -antl 
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128           *:22                        *:*                  
LISTEN     0      100    127.0.0.1:25                        *:*                  
LISTEN     0      128    192.168.18.132:27017                     *:*                  
LISTEN     0      128        [::]:22                     [::]:*                  
LISTEN     0      100       [::1]:25                     [::]:*                  
[root@oracledb bin]# systemctl stop mongodb 
[root@oracledb bin]# ss -antl 
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128           *:22                        *:*                  
LISTEN     0      100    127.0.0.1:25                        *:*                  
LISTEN     0      128        [::]:22                     [::]:*                  
LISTEN     0      100       [::1]:25                     [::]:*
##设置开机自启                  
[root@oracledb bin]# systemctl enable --now mongodb 
Created symlink from /etc/systemd/system/multi-user.target.wants/mongodb.service to /usr/lib/systemd/system/mongodb.service.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值