MongoDb-Llinux 环境下部署

发布时间 2023-09-21 11:51:30作者: tan253

1.使用远程工具登录Linux系统后, 将Mongodb压缩文件上传到/usr/local目录下解压缩

cd /usr/local
tar -zxvf 压缩包名称

2.将解压好的mongodb文件名修改为mongodb-server【看个人喜好,可以不做修改】

mv 原文件名 mongodb-server

3.在mongodb-server目录下为mongodb配置数据库保存的目录、日志保存的目录以及配置文件

cd /usr/local/mongodb-server
mkdir data #数据库保存的目录
mkdir logs #日志保存的目录
mkdir etc/mongodb.conf #启动配置文件
或者
cd /usr/local/mongodb-server
mkdir data logs etc/mongodb.conf

4.编辑配置文件

vim /usr/local/etc/mongodb.conf

5.配置文件内容

dbpath=/usr/local/mongodb-server/data #数据库保存的目录,后续创建的数据库和数据表都会保存在该目录下
logpath=/usr/local/mongodb-server/logs/mongodb.log #日志文件保存目录,mongodb.log文件无需自己创建,mongodb会为我们自动生成
port=27017
fork=true #启动后,开启后台运行
journal=false
bind_ip=0.0.0.0 #允许外网访问【记得防火墙打开端口】
auth=true #不使用身份认证进行登录,默认使用root权限,true开启

6.启动mongodb

cd /usr/local/mongodb-server/bin
./mongod -f /usr/local/mongodb-server/etc/mongodb.conf


-----/root/mongo/mongodb-server/bin/mongod -f /root/mongo/mongodb-server/etc/mongodb.conf
./mongod -f /root/mongo/mongodb-server/etc/mongodb.conf

7.启动成功反馈

about to fork child process, waiting until server is ready for connections.
forked process: 26352
child process started successfully, parent exiting

8.进入mongodb客户端

cd /usr/local/mongodb-server/bin
./mongo

9.停止服务

cd /usr/local/mongodb-server/bin
./mongod --shutdown -f /usr/local/mongodb-server/etc/mongodb.conf

 

 

 

 ####备注 


--------------------------------------
一、MongoDB开机自启服务# (先停止服务)
在/lib/systemd/system/目录创建服务脚本mongodb.service , 命令:vim /lib/systemd/system/mongodb.service

mongodb.service内容:

[Unit]
Description=Mongodb Service
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/root/mongo/mongodb-server/bin/mongod -f /root/mongo/mongodb-server/etc/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/root/mongo/mongodb-server/bin/mongod --shutdown -f /root/mongo/mongodb-server/etc/mongodb.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target


注意给mongodb.service添加权限:chmod 777 /lib/systemd/system/mongodb.service

二、启动服务#
#刷新配置(在启动前,必须先执行,否则报警告)
systemctl daemon-reload

#启动服务
systemctl start mongodb

#关闭服务
systemctl stop mongodb

#服务加入开机自启
systemctl enable mongodb

#查看状态
systemctl status mongodb

 

 

---------------------------------
vim /etc/profile
export PATH=/usr/local/mongodb-server/bin:$PATH

启动mongo服务器端被拒绝或者重启集群报错。

原因:bin里面的文件没有运行权限

解决方法:

chmod -R 777 mongodb-server/bin


---------------------------------

  开放端口

# 查看防火墙状态
systemctl status firewalld
# 开启防火墙
systemctl start firewalld
# 关闭防火墙
systemctl stop firewalld
# 开启防火墙
service firewalld start
# 若遇到无法开启
先用:systemctl unmask firewalld.service
然后:systemctl start firewalld.service

# 添加指定需要开放的端口:
firewall-cmd --add-port = 端口号/tcp --permanent
# 重载入添加的端口:
firewall-cmd --reload
# 查询指定端口是否开启成功,如果端口已开启,返回yes:
firewall-cmd --query-port = 端口号/tcp
# 移除指定端口:
firewall-cmd --permanent --remove-port=123/tcp