Mongodb数据库的安装部署及基本使用

举报
江湖有缘 发表于 2022/12/14 00:48:42 2022/12/14
【摘要】 Mongodb数据库的安装部署及基本使用

一、Mongodb数据库介绍

1.Mongodb简介

MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的,它是一个面向集合的,模式自由的文档型数据库。

2.Mongodb适用场景

MongoDB是一个基于分布式文件存储的数据库。由C++语言编写,旨在为WEB应用提供可扩展的高性能数据存储解决方案。

3.MongoDB特性

  • 面向集合存储,易于存储对象类型的数据。
  • 模式自由
  • 支持动态查询
  • 支持完全索引,包含内部对象
  • 支持复制和故障恢复
  • 使用高效的二进制数据存储,包括大型对象(如视频等)
  • 自动处理碎片,以支持云计算层次的扩展性
  • 文件存储格式为BSON(一种JSON的扩展

二、检查本地系统环境

1.检查系统版本

[root@master ~]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"




2.检查yum仓库

[root@master ~]# yum repolist all
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                                                  repo name                                                                     status
!base/7/x86_64                                           CentOS-7 - Base - mirrors.aliyun.com                                          enabled: 10,072
centosplus/7/x86_64                                      CentOS-7 - Plus - mirrors.aliyun.com                                          disabled
contrib/7/x86_64                                         CentOS-7 - Contrib - mirrors.aliyun.com                                       disabled
!extras/7/x86_64                                         CentOS-7 - Extras - mirrors.aliyun.com                                        enabled:    515
!mariadb                                                 MariaDB                                                                       enabled:     96
!updates/7/x86_64                                        CentOS-7 - Updates - mirrors.aliyun.com                                       enabled:  4,385
repolist: 15,068

三、Mongodb的安装

1.配置Mongodb的yum源

[root@server yum.repos.d]# cat mongodb.repo 
[mongodb-org]
name=MongoDB Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mongodb/yum/el$releasever/
gpgcheck=0
enabled=1



2.安装Mongodb

yum install -y mongodb-org-server mongodb-org

3.修改绑定IP


[root@server yum.repos.d]# grep bind /etc/mongod.conf 
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
[root@server yum.repos.d]#

4.启动Mongodb服务

systemctl start mongod.service
systemctl enable  mongod.service

四、检查Mongodb状态

1.查看Mongodb服务状态

[root@server ~]# systemctl status  mongod.service
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-11-24 15:27:34 CST; 12min ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 13566 (mongod)
   CGroup: /system.slice/mongod.service
           └─13566 /usr/bin/mongod -f /etc/mongod.conf

Nov 24 15:27:33 server systemd[1]: Starting MongoDB Database Server...
Nov 24 15:27:33 server mongod[13563]: about to fork child process, waiting until server is ready for connections.
Nov 24 15:27:33 server mongod[13563]: forked process: 13566
Nov 24 15:27:34 server mongod[13563]: child process started successfully, parent exiting
Nov 24 15:27:34 server systemd[1]: Started MongoDB Database Server.



2.检查Mongodb服务端口


[root@server ~]#  netstat -tunlp |grep 27017
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      13566/mongod 

3.检查Mongodb版本


[root@server ~]# mongo  --version
MongoDB shell version v5.0.14
Build Info: {
    "version": "5.0.14",
    "gitVersion": "1b3b0073a0b436a8a502b612f24fb2bd572772e5",
    "openSSLVersion": "OpenSSL 1.0.1e-fips 11 Feb 2013",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "rhel70",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}


五、连接Mongodb

1.本地连接Mongodb数据库

[root@server ~]# mongo
MongoDB shell version v5.0.14
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("0db22758-cc95-44c1-ad0f-d86eaf743a35") }
MongoDB server version: 5.0.14
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
---
The server generated these startup warnings when booting: 
        2022-11-24T15:27:33.653+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2022-11-24T15:27:34.223+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2022-11-24T15:27:34.223+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2022-11-24T15:27:34.223+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> 



2.查看默认数据库

  • admin: 从权限的角度来看,这是"root"数据库。超级管理员的库,存储用户信息。
  • local:这个数据永远不会被复制,可以用来存储限于本地单台服务器的任意集合。
  • config: 当Mongo用于分片设置时,config数据库在内部使用,用于保存分片的相关信息。

>  show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB


六、Mongodb数据库的基本操作

1.创建一个数据库

> use school;
switched to db school

2.进入某个数据库

> use shcool;
switched to db shcool

3.向数据库中插入数据

> db.class.insert({ "name": "王笑笑", "age": "18", "genden": "female" });
WriteResult({ "nInserted" : 1 })

4.查看数据库列表


> show dbs;
admin   0.000GB
config  0.000GB
local   0.000GB
shcool  0.000GB

5.查询表中数据

> db.class.find()
{ "_id" : ObjectId("637f239ac5eeb43eb6901899"), "name" : "王天天", "age" : "18", "genden" : "female" }
{ "_id" : ObjectId("637f23b9c5eeb43eb690189a"), "name" : "王笑笑", "age" : "18", "genden" : "female" }


6.删除数据库

> use school;
switched to db school
> db.dropDatabase();
{ "ok" : 1 }



7.创建单个集合


> use school;
switched to db school
> db.createCollection("class")
{ "ok" : 1 }

8.删除单个集合


> show tables;
aabb
aacc
class
> db.aabb.drop()
true
> show tables;
aacc
class

七、Mongodb数据的增删查改

1.向集合中插入文档数据——增


> db.mall.insert({  "type":  "phone",  "price":  "3799","xinghao":  "huawei", "desc":  "abbbb" })
WriteResult({ "nInserted" : 1 })


2.查找数据——查

①查找一个集合中所有数据

> db.class.find()
{ "_id" : ObjectId("637f281942616feb174fa1a8"), "name" : "王笑笑", "age" : "18", "genden" : "female" }
{ "_id" : ObjectId("637f28a442616feb174fa1a9"), "name" : "李一一", "age" : "16", "genden" : "female" }


②查询单个文档数据


> db.class.find({"name" : "王笑笑"})
{ "_id" : ObjectId("637f281942616feb174fa1a8"), "name" : "王笑笑", "age" : "18", "genden" : "female" }


③查看集合内容,以固定格式显示


>  db.class.find().pretty();
{
	"_id" : ObjectId("637f281942616feb174fa1a8"),
	"name" : "王笑笑",
	"age" : "18",
	"genden" : "female"
}
{
	"_id" : ObjectId("637f28a442616feb174fa1a9"),
	"name" : "李一一",
	"age" : "16",
	"genden" : "female"
}
> 


3.更改数据——改

①更新集合中文档数据


> db.mall.find().pretty();
{
	"_id" : ObjectId("637f277b42616feb174fa1a7"),
	"type" : "phone",
	"price" : "3799",
	"xinghao" : "huawei",
	"desc" : "abbbb"
}
> db.mall.find({"type" : "TV"})
{ "_id" : ObjectId("637f277b42616feb174fa1a7"), "type" : "TV", "price" : "3799", "xinghao" : "huawei", "desc" : "abbbb" }

①新文档替换旧文档

查看文档的ID号


> db.class.find()
{ "_id" : ObjectId("637f281942616feb174fa1a8"), "name" : "王笑笑", "age" : "18", "genden" : "female" }
{ "_id" : ObjectId("637f28a442616feb174fa1a9"), "name" : "李一一", "age" : "16", "genden" : "female" }


将将新文档替换ID为"637f281942616feb174fa1a8"

db.class.save({
    "_id" : ObjectId("637f281942616feb174fa1a8"),
    "title" : "MongoDB",
    "description" : "MongoDB 是一个 Nosql 数据库",
    "by" : "Runoob",
    "url" : "http://www.runoob.com",
    "tags" : [
            "mongodb",
            "NoSQL"
    ],
    "likes" : 110
})



查看集合内文档内容

> db.class.find()
{ "_id" : ObjectId("637f281942616feb174fa1a8"), "title" : "MongoDB", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "Runoob", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "NoSQL" ], "likes" : 110 }
{ "_id" : ObjectId("637f28a442616feb174fa1a9"), "name" : "李一一", "age" : "16", "genden" : "female" }
> 


4.删除数据——删

db.class.remove({"likes" : 110}, {justone: true})
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。