Linux下MongoDB的安装与配置
# Linux下MongoDB的安装与配置
MongoDB版本:4.2.10
下载地址:https://www.mongodb.com/try/download/community
#1.下载上传解压
`tar -zxvf mongodb-linux-x86_64-rhel70-4.2.10.tgz -C /usr/local`
#2.重命名
`cd /usr/local`
`mv mongodb-linux-x86_64-rhel70-4.2.10 mongodb/`
#3.创建用户和组
`groupadd mongodb`
`useradd -s /sbin/nologin -g mongodb -M mongodb`
#4.创建目录并赋权
`mkdir data log run`
`chown -R mongodb:mongodb data log run`
#5.MongoDB配置
在/usr/local/mongodb 里面创建一个配置文件 mongodb.conf
vi mongodb.conf 并写入下面的信息:
bind_ip=0.0.0.0
port=27017
dbpath=/usr/local/mongodb/data/
logpath=/usr/local/mongodb/log/mongodb.log
pidfilepath =/usr/local/mongodb/run/mongodb.pid
logappend=true
fork=true
maxConns=500
noauth = true
#6.注册服务配置
`vi /usr/lib/systemd/system/mongodb.service`
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target
[Service]
Type=forking
User=mongodb
Group=mongodb
ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/mongodb.conf
[Install]
WantedBy=multi-user.target
#8.开机启动,并启动。
`systemctl daemon-reload`
`systemctl enable mongodb`
`systemctl start mongodb`
#9.常用命令
启动:`systemctl start mongodb`
停止:`systemctl stop mongodb`
状态:`systemctl status mongodb`
shell:`mongo`
版本:`mongod --version`
#10.配置mongodb为环境变量,方便直接在shell中操作
`vim /etc/profile `
在/etc/profile文件末尾添加一行:
export PATH=/usr/local/mongodb/bin:$PATH
让其生效:
`source /etc/profile`
#11.远程连接mongodb时,连接不上的解决
远程连接mongodb失败,网上资料显示原因有两个:
1、mongodb的配置文件中的bind_ip 默认为127.0.0.1,默认只有本机可以连接。此时,需要将bind_ip配置为0.0.0.0,表示接受任何IP的连接。
2、防火墙阻止了27017端口。
## 开放防火墙端口并重启防火墙 ##
`firewall-cmd --zone=public --add-port=27017/tcp --permanent`
`firewall-cmd --reload`

浙公网安备 33010602011771号