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

本文涉及的产品
云数据库 MongoDB,通用型 2核4GB
简介: 初识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的安装是连接使用到此就结束的。

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。 &nbsp; 相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
目录
相关文章
|
1月前
|
NoSQL Linux MongoDB
linux麒麟系统安装mongodb7.0
linux麒麟系统安装mongodb7.0
69 10
|
13天前
|
NoSQL 网络协议 MongoDB
docker安装mongodb(单点)图文详解
该文档提供了一个快速搭建MongoDB环境的指南,适用于开发和学习,但不适用于生产。主要步骤包括:1) 使用Docker创建数据卷`mongo_data_db`和`mongo_data_configdb`。2) 拉取`mongo`镜像并运行名为`mymongo`的容器,映射端口并挂载数据卷。3) 初始化管理员账号,创建具有`root`权限的用户`admin`,密码为`adminpwd`。4) 防火墙开放端口27017。最后,提到了使用第三方客户端进行连接。
|
14天前
|
弹性计算 NoSQL Shell
一键安装 MongoDB 数据库脚本
【4月更文挑战第29天】
18 4
|
15天前
|
NoSQL Linux 网络安全
【专栏】在 RHEL 8 或者 CentOS 8 上顺利安装并运行MongoDB,以处理非结构化数据和扩展技术栈
【4月更文挑战第28天】本文档介绍了如何在RHEL或CentOS 8上安装MongoDB,包括环境准备(系统更新、依赖安装、硬件需求和sudo用户)、导入MongoDB GPG公钥、创建Yum仓库、安装MongoDB社区版,以及后续的基本配置和验证(启动服务、防火墙设置和连接验证)。通过这些步骤,用户可以顺利安装并运行MongoDB,以处理非结构化数据和扩展技术栈。
|
20天前
|
存储 分布式计算 NoSQL
MongoDB的简介和安装(在服务器上)
MongoDB的简介和安装(在服务器上)
30 0
|
1月前
|
存储 NoSQL Linux
CentOS7安装MongoDB
CentOS7安装MongoDB
18 0
|
2月前
|
NoSQL MongoDB Docker
docker安装MongoDB
docker安装MongoDB
24 0
|
3月前
|
NoSQL 关系型数据库 MySQL
Windows、Linux、Mac安装数据库(mysql、MongoDB、Redis)#0
不同系统下进行MySQL安装、MongoDB安装、Redis安装【2月更文挑战第5天】
447 5
Windows、Linux、Mac安装数据库(mysql、MongoDB、Redis)#0
|
4月前
|
存储 JSON NoSQL
【MongoDB】<文档型数据库>Windows&Liunx安装MongoDB(无错完整)
【1月更文挑战第26天】【MongoDB】<文档型数据库>Windows&Liunx安装MongoDB(无错完整)
|
4月前
|
存储 NoSQL MongoDB
学习如何使用 Python 连接 MongoDB: PyMongo 安装和基础操作教程
Python 需要一个 MongoDB 驱动程序来访问 MongoDB 数据库。我将使用 MongoDB 驱动程序 PyMongo 建议您使用 PIP 来安装 PyMongo。PIP 很可能已经安装在您的 Python 环境中。将命令行导航到 PIP 的位置,然后键入以下内容:
99 1