MongoDB使用笔记

目录

Overview(概述)

MongoDB Version(MongoDB 版本)

Platform Support(平台支持)

Install MongoDB Community Edition(安装)

1.Configure the package management system (yum).(配置包管理系统)

2.Install the MongoDB packages.(安装MongoDB包。)

Run MongoDB Community Edition(运行MongoDB社区版)

Directory Paths(目录路径)

To Use Default Directories(默认目录)

Procedure(过程)

Init System(系统初始化)

1、Start MongoDB.(启动MongoDB。)

2、Verify that MongoDB has started successfully.(验证MongoDB是否已成功启动。)

3、Stop MongoDB.(停止MongoDB。)

4、Restart MongoDB.(重新启动MongoDB。)

5、Begin using MongoDB.(开始使用MongoDB。)

Uninstall MongoDB Community Edition(卸载MongoDB社区版)

1、Stop MongoDB.(停止MongoDB.)

2、Remove Packages.(删除程序包。)

3、Remove Data Directories.(删除数据目录。)

配置副本集

1、关闭正在运行的MongoDB服务器。

2、开启MongoDB数据库

3、副本集添加成员


目标环境:阿里云服务器


Overview(概述)

Use this tutorial to install MongoDB 6.0 Community Edition on Red Hat Enterprise Linux, CentOS Linux, or Oracle Linux [1] using the yum package manager.

使用本教程,使用yum软件包管理器在Red Hat Enterprise Linux、CentOS Linux或Oracle Linux[1]上安装MongoDB 6.0 Community Edition。


MongoDB Version(MongoDB 版本)

This tutorial installs MongoDB 6.0 Community Edition. 

本教程安装MongoDB 6.0社区版。


Platform Support(平台支持)

MongoDB 6.0 Community Edition supports the following 64-bit versions of Red Hat Enterprise Linux (RHEL), CentOS Linux, Oracle Linux [1], Rocky Linux, and AlmaLinux [2] on x86_64 architecture:

  • RHEL / CentOS / Oracle / Rocky / AlmaLinux 9 (Starting in MongoDB 6.0.4)
  • RHEL / CentOS / Oracle / Rocky / AlmaLinux 8
  • RHEL / CentOS / Oracle 7
  • RHEL / CentOS / Oracle 6

MongoDB 6.0 Community Edition also supports the ARM64 architecture on RHEL / CentOS / Oracle / Rocky / AlmaLinux 8.

MongoDB 6.0 Community Edition支持以下64位版本的Red Hat Enterprise Linux(RHEL)、CentOS Linux、Oracle Linux[1]、Rocky Linux和AlmaLinux[2],基于x86_64体系结构:

  • RHEL/CNTOS/Oracle/Locky/AlmaLinux 9(在MongoDB 6.0.4中启动)
  • RHEL/可操作系统/甲骨文/洛基/AlmaLinux 8
  • RHEL/可操作系统/Oracle 7
  • RHEL/可操作系统/Oracle 6

MongoDB 6.0 Community Edition还支持RHEL/CNTOS/Oracle/Locky/AlmaLinux 8上的ARM64体系结构。


Install MongoDB Community Edition(安装)

Follow these steps to install MongoDB Community Edition using the yum package manager.

按照以下步骤使用yum软件包管理器安装MongoDB Community Edition。

1.Configure the package management system (yum).(配置包管理系统

Create a /etc/yum.repos.d/mongodb-org-6.0.repo file so that you can install MongoDB directly using yum:

创建一个/etc/yum.repos.d/mongodb-org-60.repo文件,以便您可以使用以下命令直接安装mongodb:

[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc

2.Install the MongoDB packages.(安装MongoDB包。)

To install the latest stable version of MongoDB, issue the following command:

要安装MongoDB的最新稳定版本,请发出以下命令:

sudo yum install -y mongodb-org

You can specify any available version of MongoDB. However yum upgrades the packages when a newer version becomes available. To prevent unintended upgrades, pin the package. To pin a package, add the following exclude directive to your /etc/yum.conf file:

您可以指定任何可用版本的MongoDB。然而,当有新版本可用时,yum会升级这些软件包。为了防止意外升级,请固定程序包。要固定包,请将以下exclude指令添加到/etc/yum.conf文件中:

exclude=mongodb-org,mongodb-org-database,mongodb-org-server,mongodb-mongosh,mongodb-org-mongos,mongodb-org-tools


Run MongoDB Community Edition(运行MongoDB社区版)

Directory Paths(目录路径)

To Use Default Directories(默认目录)

By default, MongoDB runs using the mongod user account and uses the following default directories:

  • /var/lib/mongo (the data directory)
  • /var/log/mongodb (the log directory)

The package manager creates the default directories during installation. The owner and group name are mongod.

默认情况下,MongoDB使用mongod用户帐户运行,并使用以下默认目录:

  • /var/lib/mongo(数据目录)
  • /var/log/mongodb(日志目录)

包管理器在安装过程中创建默认目录。所有者和组名是mongod。


Procedure(过程)

Follow these steps to run MongoDB Community Edition on your system. These instructions assume that you are using the default settings.

按照以下步骤在您的系统上运行MongoDB社区版。这些说明假定您使用的是默认设置。

Init System(系统初始化)

To run and manage your mongod process, you will be using your operating system's built-in init system. Recent versions of Linux tend to use systemd (which uses the systemctl command), while older versions of Linux tend to use System V init (which uses the service command).

要运行和管理mongod进程,您将使用操作系统的内置init系统。最新版本的Linux倾向于使用systemd(使用systemctl命令),而较旧版本的Linux则倾向于使用System V init(使用service命令)。

If you are unsure which init system your platform uses, run the following command:

如果您不确定平台使用的是哪个init系统,请运行以下命令:

ps --no-headers -o comm 1

Then select the appropriate tab below based on the result:

  • systemd
  • init.

然后根据结果选择下面适当的选项卡:

  • systemd。
  • init。

1、Start MongoDB.(启动MongoDB。)

You can start the mongod process by issuing the following command:

您可以通过发出以下命令来启动mongod进程:

sudo systemctl start mongod

2、Verify that MongoDB has started successfully.(验证MongoDB是否已成功启动。)

You can verify that the mongod process has started successfully by issuing the following command:

您可以通过发出以下命令来验证mongod进程是否已成功启动:

sudo systemctl status mongod

You can optionally ensure that MongoDB will start following a system reboot by issuing the following command:

您可以通过发出以下命令来确保MongoDB在系统重新启动后启动:

sudo systemctl enable mongod

3、Stop MongoDB.(停止MongoDB。)

As needed, you can stop the mongod process by issuing the following command:

根据需要,您可以通过发出以下命令来停止mongod进程:

sudo systemctl stop mongod

4、Restart MongoDB.(重新启动MongoDB。)

You can restart the mongod process by issuing the following command:

您可以通过发出以下命令重新启动mongod进程:

sudo systemctl restart mongod

5、Begin using MongoDB.(开始使用MongoDB。)

Start a mongosh session on the same host machine as the mongod. You can run mongosh
 without any command-line options to connect to a mongod that is running on your localhost with default port 27017.

启动 mongosh 与mongod在同一主机上的会话。你可以运行 mongosh 没有任何命令行选项来连接到使用默认端口27017在本地主机上运行的mongod。

mongosh


Uninstall MongoDB Community Edition(卸载MongoDB社区版)

To completely remove MongoDB from a system, you must remove the MongoDB applications themselves, the configuration files, and any directories containing data and logs. The following section guides you through the necessary steps.

要从系统中完全删除MongoDB,必须删除MongoDB应用程序本身、配置文件以及任何包含数据和日志的目录。以下部分将指导您完成必要的步骤。

1、Stop MongoDB.(停止MongoDB.)

Stop the mongod process by issuing the following command:

通过发出以下命令停止mongod进程:

sudo service mongod stop

2、Remove Packages.(删除程序包。)

Remove any MongoDB packages that you had previously installed.

删除您以前安装的所有MongoDB包。

sudo yum erase $(rpm -qa | grep mongodb-org)

3、Remove Data Directories.(删除数据目录。)

Remove MongoDB databases and log files.

删除MongoDB数据库和日志文件。

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongo


配置副本集

MongoDB复制是将数据同步在多个服务器的过程。

复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性。

复制还允许您从硬件故障和服务中断中恢复数据。

1、关闭正在运行的MongoDB服务器。

sudo systemctl stop mongod

现在我们通过指定 --replSet 选项来启动mongoDB。--replSet 基本语法格式如下:

mongod --port "端口" --dbpath "数据路径" --replSet "副本名"

实例

mongod --port 27017 --dbpath "/var/lib/mongo" --replSet rs0

执行命令后,当前终端会监听连接。需要打开一个新终端执行以下步骤。

2、开启MongoDB数据库

通过以下命令,指定主机和端口打开数据库:

mongosh 127.0.0.1:27017

3、副本集添加成员

添加副本集的成员,我们需要使用多台服务器来启动mongo服务。进入Mongo客户端,并使用rs.add()方法来添加副本集的成员。

rs.add(HOST_NAME:PORT)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值