关于后端:MongoDB-安装

5次阅读

共计 3341 个字符,预计需要花费 9 分钟才能阅读完成。

每日一句

Sometimes it takes going through something so awful to realize the beauty that is out there in this world.
有时候就是要经验一些蹩脚的事件能力意识到世间存在的漂亮。

概述

本文介绍 MongoDB 别离在 windows 和 linux 平台下的装置与配置和 MongoDB 服务启动与连贯。

windows 装置与配置

MongoDB 提供了可用于 32 位和 64 位零碎的预编译二进制包,你能够从 MongoDB 官网下载安装,MongoDB 预编译二进制包下载地址:https://www.mongodb.com/try/download/community

留神:在 MongoDB 2.2 版本后曾经不再反对 Windows XP 零碎。最新版本也曾经没有了 32 位零碎的安装文件。

你也间接能够间接点击链接下载 windows 5.0.5 版本→https://fastdl.mongodb.org/windows/mongodb-windows-x86_64-5.0.5-signed.msi

装置过程中,你能够通过点击 “Custom(自定义)” 按钮来设置你的装置目录。

启动服务

进入 bin 目录:D:\devTools\MongoDB\Server\5.0\bin 双击 mongo.exe 浏览器拜访:http://127.0.0.1:27017/

看到页面呈现:It looks like you are trying to access MongoDB over HTTP on the native driver port. 就阐明启动胜利了。

MongoDB Compass

new Connection:输出 mongodb://127.0.0.1:27017 就能够连贯到本地的 MongoDB 服务了。

linux 装置

MongoDB 提供了 linux 各个发行版本 64 位的安装包,你能够在官网下载安装包。

前置依赖包

装置前咱们须要装置各个 Linux 平台依赖包。

Red Hat/CentOS:sudo yum install libcurl openssl

Ubuntu 18.04 LTS (“Bionic”)/Debian 10 “Buster”:sudo apt-get install libcurl4 openssl

Ubuntu 16.04 LTS (“Xenial”)/Debian 9 “Stretch”:sudo apt-get install libcurl3 openssl

装置并启动

MongoDB 源码下载地址:https://www.mongodb.com/try/download/community

# 创立 安装包寄存目录并进入该目录
mkdir -p /usr/yltrcc/mongodb
cd usr/yltrcc/mongodb/
# 下载并解压缩
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-5.0.5.tgz
tar -xvf mongodb-linux-x86_64-rhel70-5.0.5.tgz 
# 新建几个目录,别离用来存储数据和日志
mkdir -p /usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/single/data/db
mkdir -p /usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/single/log
# 新建并批改配置文件
vi /usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/single/mongod.conf

# 向 mongod.conf 增加如下内容:systemLog:
  #MongoDB 发送所有日志输入的指标指定为文件
  # #The path of the log file to which mongod or mongos should send all diagnostic logging information
  destination: file
  #mongod 或 mongos 应向其发送所有诊断日志记录信息的日志文件的门路
  path: "/usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/single/log/mongod.log"
  #当 mongos 或 mongod 实例重新启动时,mongos 或 mongod 会将新条目附加到现有日志文件的开端。logAppend: true
storage:
  #mongod 实例存储其数据的目录。storage.dbPath 设置仅实用于 mongod。##The directory where the mongod instance stores its data.Default Value is "/data/db".
  dbPath: "/usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/single/data/db"
  journal:
    #启用或禁用持久性日志以确保数据文件放弃无效和可复原。enabled: true
processManagement:
  #启用在后盾运行 mongos 或 mongod 过程的守护过程模式。fork: true
net:
  #服务实例绑定的 IP,默认是 localhost 192.168.20.131 示意近程拜访
  bindIp: localhost,192.168.20.131
  #绑定的端口,默认是 27017
  port: 27017
  
# 启动 MongoDB 服务
/usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/bin/mongod -f /usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/single/mongod.conf

# 通过过程来查看服务是否启动了
ps -ef |grep mongod

提醒:看到 child process started successfully, parent exiting 示意启动胜利了

连贯测试

提醒:如果近程连贯不上,须要配置防火墙放行,或间接敞开 linux 防火墙

# 查看防火墙状态 
systemctl status firewalld 
#长期敞开防火墙 
systemctl stop firewalld 
#开机禁止启动防火墙 
systemctl disable firewalld

compass 连贯

地址:mongodb://192.168.20.131:27017

进行敞开服务

进行服务的形式有两种:疾速敞开和规范敞开

疾速敞开

疾速,简略,数据可能会出错

# 通过 ps 命令查看 过程号
ps -ef | grep mongod
# kill 命令完结该过程
kill -9 54410

【补充】

如果一旦是因为数据损坏,则须要进行如下操作(理解):

1)删除 lock 文件:rm -f /usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/single/data/db/*.lock

2) 修复数据:/usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/bin/mongod --repair --dbpath=/usr/yltrcc/mongodb/mongodb-linux-x86_64-rhel70-5.0.5/single/data/db

规范敞开

通过 mongo 客户端中的 shutdownServer 命令来敞开服务

# 客户端登录服务,留神,这里通过 localhost 登录,如果须要近程登录,必须先登录认证才行。mongo --port 27017 
# 切换到 admin 库
use admin 
# 敞开服务 
db.shutdownServer()

美文佳句

同一个人,用不同的眼光去看,霎时就不一样。很多时候咱们会对身边的人求全责备,可如果一味盯着他人的毛病,生存天然一地鸡毛。只有看到对方的长处,能力难受相处。

敌人间如此,爱世间亦如此。越是优良的人,越容易看到他人的好。

你好,我是 yltrcc,日常分享技术点滴,欢送关注我的公众号:ylcoder

正文完
 0