linux安装mongodb实例分析

在Linux系统中安装MongoDB,首先需要下载MongoDB的安装包,可以通过访问MongoDB官方网站()下载对应版本的安装包,本文以下载MongoDB Community Server 4.4.3版本为例进行介绍。

1. 下载MongoDB安装包

linux安装mongodb实例分析

访问MongoDB官方网站,点击“Downloads”选项卡,选择“Community Server”,然后点击“Download”按钮,在弹出的页面中,选择适合您的操作系统的版本,在本例中,我们选择“Linux”下的“x86_64-redhat-70-ssl”版本。

2. 上传安装包到Linux服务器

将下载好的安装包上传到Linux服务器上,可以使用SCP命令或者FTP工具进行上传,使用SCP命令上传安装包:

scp mongodb-linux-x86_64-4.4.3.tgz username@your_server_ip:/home/username

3. 解压安装包

在Linux服务器上,使用以下命令解压安装包:

tar -zxvf mongodb-linux-x86_64-4.4.3.tgz

4. 创建数据和日志目录

为了方便管理,我们需要为MongoDB创建一个数据和日志目录,使用以下命令创建目录:

mkdir -p /data/db /var/log/mongodb

5. 修改配置文件

在解压后的文件夹中,找到`mongod.conf`文件,使用文本编辑器打开并修改以下配置:

# 设置数据库存储路径
storage:
  dbPath: /data/db

# 设置日志文件路径
systemLog:
  destination: file
  path: /var/log/mongodb/mongod.log
  logAppend: true

6. 创建系统服务

为了让MongoDB在系统启动时自动运行,我们需要创建一个系统服务,创建一个名为`mongod`的文件:

sudo touch /etc/systemd/system/mongod.service

使用文本编辑器打开该文件,并添加以下内容:

```ini

[Unit]

Description=MongoDB Database Server

After=network.target

Documentation=-on-linux/#run-mongod-as-a-service-unit

Wants=network-online.target

AssertFileIsExecutable=/usr/bin/mongod

AssertPathIsDirectory=/data/db

AssertFileIsReadable=/etc/mongod.conf

AssertFileIsWritable=/data/db/transactions-tmpdir

AssertFileIsExecutable=/usr/bin/mongod --config /etc/mongod.conf

[Service]

Type=forking

PIDFile=/var/run/mongodb/mongod.pid

ExecStart=/usr/bin/mongod --config /etc/mongod.conf --fork --logpath /var/log/mongodb/mongod.log --pidfile /var/run/mongodb/mongod.pid --quiet --replSet MaintainerMode --bind_ip_all --oplogSizeMB 128 --smallfiles --noprealloc --journalCommitIntervalMs 5000 --syncPeriodSecs 0 --shutdownTimeoutMinutes 90 --dbpath /data/db --auth &

User=mongodb

Group=nogroup

UMask=00077 # Only umask permissions r relevant for non-root users, so this is the recommended umask value for non-root users. If you're running as root, umask doesn't matter and you can set it to whatever value you like.Restart=always

RestartSec=10s # time to wait before restarting when process exits on non-zero status code or if the config files have been modified since the last restart (in seconds) [START = 10s] [MIN = 30s] [MAX = 3600s] [GRACE = 10m] [DEFAULT = 30s]KillMode=mixed # Indicates how processes are terminated when system resources are low or if an error occurs while the process is running. The available options are "graceful", "fast", and "mixed" (default). KillMode=mixed will attempt to kill all processes in a graceful manner first, but if that fails, it will resort to killing them quickly with SIGKILL if necessary.Environment=MONGO_INITDB_ROOT_USERNAME=root # Set this variable to specify the name of the initial root user created when the database is first installed and started up without any authentication enabled. This user has full access to the database and should only be used for initial setup purposes or testing purposes during development.Environment=MONGO_INITDB_ROOT_PASSWORD=example # Set this variable to specify the password for the initial root user created when the database is first installed and started up without any authentication enabled.Environment=MONGO_INITDB_DATABASE=test # Set this variable to specify the name of the database to create when the database is first installed and started up without any authentication enabled.Environment=MONGO_INITDB_AUTH=false # Set this variable to specify whether or not authentication should be enabled when the database is first installed and started up without any authentication enabled.Environment=MONGO_INITDB_READ_PRIVILEGES=user # Set this variable to specify which user(s) have read access when the database is first installed and started up without any authentication enabled.Environment=MONGO_INITDB_WRITE_PRIVILEGES=user # Set this variable to specify which user(s) have write access when the database is first installed and started up without any authentication enabled.[Install]WantedBy=multi-user.service # After=network.target means that network interfaces are up before starting this service, but we want to start this service after network interfaces are up so that we can connect to other servers in the cluster if needed. # Note that we use WantedBy=multi-user.service instead of WantedBy=graphical.target because we don't want this service to start automatically when the system boots up in graphical mode (i.e. single-user mode). We only want it to start automatically when the system boots up in multi-user mode (i.e. server mode). Save and close the file.

原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/66382.html

Like (0)
Donate 微信扫一扫 微信扫一扫
K-seoK-seo
Previous 2023-12-01 06:16
Next 2023-12-01 06:20

相关推荐

  • mongodb有时候查询慢怎么解决

    一、文章排版MongoDB 是一款高性能的 NoSQL 数据库,广泛应用于各种场景,在实际使用过程中,我们可能会遇到查询速度慢的问题,本文将介绍如何解决 MongoDB 查询慢的问题,并提供详细的技术教程。二、问题分析1. 数据量过大:当数据量达到一定程度时,查询速度会受到影响。2. 索引不合理:没有为查询字段创建合适的索引,导致查询……

    2023-11-24
    0608
  • Linux扩容要注意哪些事项

    在Linux系统中,扩容是一个常见的操作,无论是硬盘、内存还是文件系统,都需要进行扩容,在进行扩容操作时,需要注意一些事项,否则可能会导致数据丢失或者系统崩溃,本文将详细介绍Linux扩容要注意的事项。硬盘扩容1、确认磁盘空间是否足够在进行硬盘扩容之前,需要先确认磁盘空间是否足够,可以使用df -h命令查看磁盘空间使用情况,如果磁盘空……

    2024-03-09
    0192
  • linux防止攻击

    什么是IP攻击?IP攻击,即网络地址攻击(Internet Protocol Attack),是一种针对网络设备的恶意攻击行为,攻击者通过伪造源IP地址,伪装成合法用户发起请求,从而达到窃取信息、破坏系统等目的,常见的IP攻击类型有:端口扫描、SYN洪泛攻击、UDP反射攻击、ICMP Flood攻击等。如何防范IP攻击?1、使用防火墙……

    2023-12-19
    0133
  • MongoDB索引策略与优化的方法是什么

    MongoDB索引策略包括唯一索引、复合索引和全文索引,优化方法包括建立合适的索引、避免过度索引和定期维护索引。

    2024-05-15
    0138
  • centos7面板

    CentOS 7面板是一种基于Web的服务器管理工具,提供直观的操作界面,简化了Linux系统的配置和管理。

    2024-05-05
    0142
  • linux如何开启ssl服务器

    在Linux中开启SSL服务器,可以通过以下步骤进行:1. 安装OpenSSL:首先需要安装OpenSSL库,可以使用包管理器来安装,例如在Ubuntu上可以使用apt-get命令进行安装,运行以下命令来更新软件包列表并安装OpenSSL: sudo apt-get update sudo apt-get install openss……

    2023-11-13
    0259

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

免备案 高防CDN 无视CC/DDOS攻击 限时秒杀,10元即可体验  (专业解决各类攻击)>>点击进入