Mongodb数据库的安装部署及基本使用
2023-09-14 09:15:41 时间
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})
相关文章
- MongoDB数据库安装部署及优化使用
- MongoDB数据库两阶段提交实现事务的方法详解
- MongoDB使用场景和局限 (转)详解数据库
- MongoDB安装——快速简单轻松(mongodb安装包)
- MongoDB安装及其配置必备实操指南(mongodb安装和配置)
- 利用MongoDB实现高效的内存映射.(mongodb内存映射)
- 数据库终止MongoDB数据库运行(停止mongodb)
- Mongodb官方网站:丰富的数据库解决方案(mongodb官网)
- MongoDB:构建数据库的强大利器(mongodb数据库)
- MongoDB优化分页查询性能(mongodb分页优化)
- 优化MongoDB,提升数据库性能(mongodb优化方案)
- MongoDB稳步升级,提升数据库性能(mongodb升级)
- Centos6.5搭建MongoDB数据库主从架构详细实验步骤
- MongoDB 轻松安装,成功入门啦!(mongodb安装成功)
- MongoDB 授权:如何保护你的数据?(mongodb授权)
- 深入探索MongoDB:数据库文件解读(mongodb数据库文件)
- MongoDB:实现大规模应用所必备的强大功能(mongodb功能)
- 如何快速重新启动 MongoDB 数据库服务(mongodb重启服务)
- MongoDB轻松建立数据库和表(mongodb建库建表)
- MongoDB开启北京之旅!(mongodb北京)
- CSDN教程:学习MongoDB数据库管理及应用(csdnmongodb)
- MongoDB数据库优化技巧与方法(mongodb调优)