MongoDB之旅:MongDB安装和使用(后台运行)

本文涉及的产品
云数据库 MongoDB,独享型 2核8GB
推荐场景:
构建全方位客户视图
简介: 初识MongDB随时都要保持求知欲!一、什么是MongDBMongDB的官网 什么是MongoDB? MongoDB是一个文档数据库,具有您想要的查询和索引所需的可扩展性和灵活性。

初识MongDB

随时都要保持求知欲!

一、什么是MongDB

MongDB的官网
什么是MongoDB?
MongoDB是一个文档数据库,具有您想要的查询和索引所需的可扩展性和灵活性。

  • MongoDB 将数据存储在灵活的,类似JSON的文档中,这意味着字段可能因文档而异,数据结构可随时间变化

  • 文档模型映射到应用程序代码中的对象,使数据易于使用

  • 即席查询,索引和实时聚合提供了访问和分析数据的强大方法

  • MongoDB是一个分布式数据库,它的核心是高可用性,横向扩展和地理分布,并且易于使用

  • MongoDB是免费且开源的,在GNU Affero General Public License下发布

二、MongoDB的架构

MongoDB架构官方文档
讲的非常好,有兴趣的同学可以自行前往多了解一下。

三、安装MongoDB

官方不同操作系统安装网址
在上面的网址中,你可以根据你的系统的不同选择不同的方式安装。

四、Mac安装MongoDB

下面以Mac系统安装为例:
Mac系统安装教程
使用Homebrew安装MongoDB。
Homebrew installs binary packages based on published “formulae.” This section describes how to update brew to the latest packages and install MongoDB Community Edition. Homebrew requires some initial setup and configuration, which is beyond the scope of this document.

1.升级Homebrew’s

Update Homebrew’s package database.
In a system shell, issue the following command:

brew update

2.安装MongoDB

You can install MongoDB via brew with several different options. Use one of the following operations:

brew install mongodb

五、运行MongoDB

1.创建MongoDB的数据库文件夹

Before you start MongoDB for the first time, create the directory to which the mongod process will write data. By default, the mongod process uses the /data/db directory. If you create a directory other than this one, you must specify that directory in the dbpath option when starting the mongod process later in this procedure.

The following example command creates the default /data/db directory:

mkdir -p /data/db

2.给文件夹设置权限

Before running mongod for the first time, ensure that the user account running mongod has read and write permissions for the directory.(必须得有可读可写权限)

  • 我们在这里给予所有的用户的可读可写权限
/data ⌚ 15:34:06
$ ll
total 0
drwxr-xr-x  2 root  wheel    64B Jun  8 15:33 db

/data ⌚ 15:34:07
$ chmod  777 /data/db
chmod: Unable to change file mode on /data/db: Operation not permitted

/data ⌚ 15:35:48
$ sudo chmod  777 /data/db

/data ⌚ 15:35:57
$ ll
total 0
drwxrwxrwx  2 root  wheel    64B Jun  8 15:33 db

/data ⌚ 15:35:59
$ 

3.运行MongoDB

To run MongoDB, run the mongod process at the system prompt. If necessary, specify the path of the mongod or the data directory. See the following examples.(两种启动方式:你如果使用的默认db的文件夹,就可以默认启动,还有一种需要结合db的位置启动)

  • Run without specifying paths
    If your system PATH variable includes the location of the mongod binary and if you use the default data directory (i.e., /data/db), simply enter mongod at the system prompt:(默认db位置,默认启动方式)
mongod
  • Specify the path of the mongod
    If your PATH does not include the location of the mongod binary, enter the full path to the mongod binary at the system prompt:(二进制文件启动)
<path to binary>/mongod
  • Specify the path of the data directory
    If you do not use the default data directory (i.e., /data/db), specify the path to the data directory using the –dbpath option:(使用自定义的文件夹的方式启动)
mongod --dbpath <path to data directory>

4.测试MongoDB是否安装成功

这里写图片描述
有以下的输出就说明启动成功了:

[initandlisten] waiting for connections on port 27017

5.开始使用MongoDB

Start a mongo shell on the same host machine as the mongod. Use the –host command line option to specify the localhost address and port that the mongod listens on:
方式一:

mongo

方式二

mongo --host 127.0.0.1:27017

六、后台启动OR杀死程序(以MongoDB为例)

1.后台启动MongoDB

nohup + 服务的启动项 + &
3913 是服务的启动端口

$ nohup mongod &
[1] 3913
appending output to nohup.out

2.连接MongoDB

$ mongo
MongoDB shell version v3.6.5
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.5
Server has startup warnings: 
2018-06-08T16:01:00.163+0800 I CONTROL  [initandlisten] 
2018-06-08T16:01:00.163+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-06-08T16:01:00.163+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-06-08T16:01:00.163+0800 I CONTROL  [initandlisten] 
2018-06-08T16:01:00.163+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.

3.查看nohup正在运行的后台程序

3913:服务的端口
mongod:服务

$ jobs -l                                              
[1]  + 3913 running    nohup mongod

4.杀死这个端口

$ kill -9 3913
[1]  + 3913 killed     nohup mongod                                                                                                           

~ ⌚ 16:07:33
$ 

好啦,现在MongoDB的安装是连接使用到此就结束的。

目录
相关文章
|
2月前
|
NoSQL Ubuntu MongoDB
在Ubuntu 22.04上安装MongoDB 6.0的步骤
这些步骤应该可以在Ubuntu 22.04系统上安装MongoDB 6.0。安装过程中,如果遇到任何问题,可以查阅MongoDB的官方文档或者Ubuntu的相关帮助文档,这些资源通常提供了解决特定问题的详细指导。
289 18
|
6月前
|
存储 NoSQL MongoDB
Docker中安装MongoDB并配置数据、日志、配置文件持久化。
现在,你有了一个运行在Docker中的MongoDB,它拥有自己的小空间,对高楼大厦的崩塌视而不见(会话丢失和数据不持久化的问题)。这个MongoDB的数据、日志、配置文件都会妥妥地保存在你为它精心准备的地方,天旋地转,它也不会失去一丁点儿宝贵的记忆(即使在容器重启后)。
699 4
|
9月前
|
NoSQL MongoDB 微服务
微服务2——MongoDB单机部署1——下载安装
本指南介绍在Windows系统上安装和启动MongoDB的步骤。首先,从官网下载适用于32位或64位系统的预编译二进制包,选择稳定版(y为偶数)。解压后创建数据目录`data/db`,可通过命令行参数(如`mongod --dbpath=..\data\db`)或配置文件启动服务。配置文件需注意转义字符与空格使用,支持自定义日志路径、端口等参数。将bin目录加入环境变量可简化启动操作。
247 0
微服务2——MongoDB单机部署1——下载安装
|
9月前
|
存储 NoSQL Linux
微服务2——MongoDB单机部署4——Linux系统中的安装启动和连接
本节主要介绍了在Linux系统中安装、启动和连接MongoDB的详细步骤。首先从官网下载MongoDB压缩包并解压至指定目录,接着创建数据和日志存储目录,并配置`mongod.conf`文件以设定日志路径、数据存储路径及绑定IP等参数。之后通过配置文件启动MongoDB服务,并使用`mongo`命令或Compass工具进行连接测试。此外,还提供了防火墙配置建议以及服务停止的两种方法:快速关闭(直接杀死进程)和标准关闭(通过客户端命令安全关闭)。最后补充了数据损坏时的修复操作,确保数据库的稳定运行。
624 0
|
NoSQL Ubuntu Linux
Linux平台安装MongoDB
10月更文挑战第11天
486 5
|
NoSQL Shell MongoDB
Mac OSX 平台安装 MongoDB
10月更文挑战第11天
299 4
|
NoSQL Shell MongoDB
Windows 平台安装 MongoDB
10月更文挑战第10天
415 0
Windows 平台安装 MongoDB
|
存储 NoSQL MongoDB
docker安装Mongodb
这篇文章详细说明了如何使用Docker安装MongoDB,包括拉取镜像、创建并运行容器、数据持久化存储以及访问容器内的MongoDB服务的具体步骤。
1207 1
|
存储 NoSQL 网络协议
【赵渝强老师】MongoDB的安装与访问
本文介绍了在Linux系统上安装和部署MongoDB的详细步骤,包括安装依赖包、解压安装包、配置环境变量、创建数据目录及启动服务等。文中还提供了相关命令示例和注意事项,帮助用户顺利完成MongoDB的安装与配置。
306 0
|
JavaScript NoSQL 前端开发

推荐镜像

更多