Linux教程

Linux环境下搭建MongoDB

本文主要是介绍Linux环境下搭建MongoDB,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Linux环境下搭建MongoDB

MongoDB企业版官方下载地址,下载版本:

在这里插入图片描述

说明选择
VersionMongodb版本最新版本(5.0.1)
Platform 运行平台CentOS 7.0
Package打包形式tgz

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

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

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

mv 原文件名 mongodb-server

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

编辑配置文件

vim /usr/local/etc/mongodb.conf

配置文件内容

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=false #不使用身份认证进行登录,默认使用root权限

启动mongodb

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

启动成功反馈

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

进入mongodb客户端

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

进入成功界面

MongoDB shell version v5.0.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("29cb300f-a182-4b9b-bfda-4a1173fa7792") }
MongoDB server version: 5.0.0
================
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.
We recommend you begin using "mongosh".
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
---
The server generated these startup warnings when booting: 
        2021-07-23T16:27:59.830+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2021-07-23T16:28:00.363+08:00: You are running this process as the root user, which is not recommended
        2021-07-23T16:28:00.363+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2021-07-23T16:28:00.363+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'

停止服务

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

停止服务成功

{"t":{"$date":"2021-07-23T08:32:19.408Z"},"s":"I",  "c":"CONTROL",  "id":20697,   "ctx":"-","msg":"Renamed existing log file","attr":{"oldLogPath":"/usr/local/mongodb-server/logs/mongodb.log","newLogPath":"/usr/local/mongodb-server/logs/mongodb.log.2021-07-23T08-32-19"}}
killing process with pid: 26352

开放端口

# 查看防火墙状态 
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
这篇关于Linux环境下搭建MongoDB的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!