MongoDB(一):CentOS7离线安装MongoDB单机版与简单使用

1、概述

大家好,我是欧阳方超,可以关注我的公众号“欧阳方超”,后续内容将在公众号首发。
由于服务器处于内网去,所有本篇介绍使用.tgz包安装MongoDB的服务端,尽管使用.tgz包可以完成MongoDB服务端的安装,但是官方依然推荐使用yum包管理器来进行安装,因为yum包管理器会自动安装所需要的依赖,并且还会提供一个名为mongod.conf的示例文件,方便将来升级及维护工作,使用.tgz安装的话,该文件需要自行创建。使用.tgz安装服务端后,还需要单独安装 mongosh,在没有GUI连接工具的情况下,可以通过mongosh命令行连接MongoDB服务端。

2、安装社区版

2.1、前置条件

MongoDB依赖libcurl、openssl、xz-libs这几个包,请确保机器上已经安装这些包,可以使用下面的命令检查相关依赖是否已安装,比如下面的命令查询openssl是否已安装:

rpm -qa|grep openssl

如果已安装的话,会有内容返回:

openssl-1.0.2k-21.el7_9.x86_64
openssl-static-1.0.2k-21.el7_9.x86_64
openssl-devel-1.0.2k-21.el7_9.x86_64
openssl-libs-1.0.2k-21.el7_9.x86_64

2.2、下载.tgz文件

根据操作系统选择不合适的版本进行下载,这里已下面的版本为例进行安装:

mongodb-linux-x86_64-rhel70-7.0.2.tgz

2.3、解压文件

解压文件到指定目录,

tar -zxvf mongodb-linux-x86_64-rhel70-7.0.2.tgz -C /path/to/destination

这里暂时先不降MongoDB的二进制命令配置到系统的PATH中,如果需要找二进制命令的话直接到安装目录去找即可。

2.4、安装MongoDB Shell

作为一个追求使用命令行的少年,哪能错过使用命令行的快乐,所以mongosh必须要装啊,这里也下载的.tgz包:

mongosh-2.0.2-linux-x64.tgz

同样,将其解压到指定目录:

tar -zxvf mongosh-2.0.2-linux-x64.tgz -C /path/to/destination

在解压目录的bin目录下,可以看到mongosh命令:

-rwxr-xr-x 1 root root 111667760 Oct 14 20:23 mongosh
-rwxr-xr-x 1 root root 107165512 Oct 14 20:23 mongosh_crypt_v1.so

不过别着急用,因为MongoDB的server还没启动。

3、运行MongoDB服务端

3.1、关于ulimit

大多数类Unix操作系统限制进程可以使用的系统资源。这些限制可能会对MongoDB的运行产生负面影响,因此应进行调整,这里可以所搜下UNIX ulimit设置,根据实际情况设置合适的值,测试时设置为了1024000。

3.2、目录设置

跟很多服务一样,MongoDB运行时也会产生数据和日志,数据和日志会存储在默认的位置,这里不打算使用默认目录, 分别为数据和日志创建目录:

/path/to/data
/path/to/log

创建mongod用户及mongod用户组,把数据目录和日志目录的所有者和所属组设置为刚刚创建的用户和用户组。

chown -R mongod:mongod /path/to/data
chown -R mongod:mongod /path/to/log

3.3、创建mongod.conf

先配置下面几个配置项吧,

dbpath=/path/to/data
logpath=/path/to/log/mongodb.log
port=27017
fork=true

dbpath指定了数据目录的存储位置,logpath指定了日志文件的存储位置,注意需要指定到文件,否则启动会报错,port指定运行时占用的端口,可以根据情况调整,fork为true指明以后台方式启动。

3.4、运行MongoDB

使用MongoDB安装目录下的mongod命令,并使用–config或-f指定配置文件,即可启动MongoDB服务。

[root@xxx bin]#  ./mongod -f  /path/to/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 80863
child process started successfully, parent exiting

3.5、检查MongoDB是否已运行

可以使用检查端口号的形式检查MongoDB是否已运行。

[root@xxx bin]# netstat -tunlp|grep 27017
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      977415/./bin/mongod

4、使用MongoDB

之前的操作中已将安装了mongosh,此时只要执行该命令就会默认连接本机的MongoDB服务(以默认端口连接),并进入到test数据库:

[root@hadoop104 bin]# ./mongosh
Current Mongosh Log ID: 6538cf151bec061745212a9b
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.0.2

Using MongoDB:          7.0.2
Using Mongosh:          2.0.2

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

------
   The server generated these startup warnings when booting
   2023-10-25T16:17:10.127+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2023-10-25T16:17:10.127+08:00: You are running this process as the root user, which is not recommended
   2023-10-25T16:17:10.127+08:00: This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip <address> to specify which IP addresses it should serve responses from, or with --bind_ip_all to bind to all interfaces. If this behavior is desired, start the server with --bind_ip 127.0.0.1 to disable this warning
   2023-10-25T16:17:10.130+08:00: You are running on a NUMA machine. We suggest launching mongod like this to avoid performance problems: numactl --interleave=all mongod [other options]
   2023-10-25T16:17:10.130+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
   2023-10-25T16:17:10.131+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
   2023-10-25T16:17:10.131+08:00: vm.max_map_count is too low
------


Deprecation warnings:
  - Using mongosh on the current operating system is deprecated, and support may be removed in a future release.
See https://www.mongodb.com/docs/mongodb-shell/install/#supported-operating-systems for documentation on supported platforms.
test>

4.1、操作数据库

4.1.1、显示当前数据库

显示当前数据库:

test> db
test

这将显示当前选定的数据库。

4.1.2、显示所有数据库

显示所有数据库:

test> show dbs
admin   40.00 KiB
config  72.00 KiB
local   72.00 KiB
test    72.00 KiB
test>

这将显示服务器上的所有数据库。

4.1.3、切换到其他数据库

注意下面的命令在切换数据库时,如果数据库不存在,则会创建数据库,否则切换到指定数据库:

use <database_name>

将 <database_name> 替换为您要切换到的数据库名称。如果该数据库不存在,MongoDB 将在您首次向其写入数据时创建该数据库。
MongoDB中数据库的概念与关系型数据库中数据库的概念基本一致。

4.2、对集合的操作

4.2.1、显示数据库中所有集合

集合存在于MongoDB的单个数据库中,它等效于关系型数据库中的表。使用下面三种方式都可以查询数据库中的所有集合:

test> show collections
liCollection
test>

test> show tables
liCollection
test>

test> db.getCollectionNames()
[ 'liCollection' ]
test>

4.2.2、创建集合

使用use命令切换到要创建集合的数据库,使用db.createCollection()命令创建集合,要创建的集合的名称作为参数,例如如下命令创建了一个名为myCollection的集合:

test> db.createCollection("myCollection")
{ ok: 1 }
test>

注意:如果在向集合中插入文档时,假如集合并不存在,那么将自动创建该集合,下面的命令向myCollectionOne集合插入一条数据,但是集合并未事先创建,执行插入操作时集合myCollectionOne将会被自动创建:

test> db.myCollectionOne.insertOne({name:"Jhone", age:23})
{
  acknowledged: true,
  insertedId: ObjectId("654ed4c0fba0f2e5ddf52464")
}
test>

4.2.3、删除集合

删除集合,可以使用db.yourCollectionName.drop()命令,下面是删除集合的示例:

test> db.liCollection.drop()
true
test>

如果成功删除选定集合,则 drop() 方法返回 true,否则返回 false。请注意,集合删除是一个不可逆的操作,一旦删除,其中的数据将无法恢复。

4.2.3、集合改名

MongoDB中,可以使用 db.collection.renameCollection() 命令来给集合改名:

test> db.myCollectionOne.renameCollection("newMyCollection")
{ ok: 1 }
test>

4.3、对文档的操作

MongoDB中文档一词源于document,文档的数据结构和JSON基本一致,所有存储在集合中的数据都是BSON格式,

4.3.1、插入文档

使用db.yourCollectionName.insertOne()方法插入单个文档:

test> db.newMyCollection.insertOne({name:"Jhone", age:23})
{
  acknowledged: true,
  insertedId: ObjectId("654eddadfba0f2e5ddf52465")
}

使用db.yourCollectioinName.insertMany()方法插入多个文档:

test> db.newMyCollection.insertMany([{name:"Jhone", age:21},{name:"Peter", age:20}])
{
  acknowledged: true,
  insertedIds: {
    '0': ObjectId("654ede7efba0f2e5ddf52467"),
    '1': ObjectId("654ede7efba0f2e5ddf52468")
  }
}

4.3.2、查询文档

使用db.yourCollectionName.find()方法查询集合中的文档:

test> db.newMyCollection.find()
[
  { _id: ObjectId("654ed4c0fba0f2e5ddf52464"), name: 'Jhone', age: 23 },
  { _id: ObjectId("654eddadfba0f2e5ddf52465"), name: 'Jhone', age: 23 },
  { _id: ObjectId("654ede65fba0f2e5ddf52466"), name: 'Jhone', age: 21 },
  { _id: ObjectId("654ede7efba0f2e5ddf52467"), name: 'Jhone', age: 21 },
  { _id: ObjectId("654ede7efba0f2e5ddf52468"), name: 'Peter', age: 20 }
]

该方法会显示集合中的所有文档。
如果要查询单个文档,可以使用db.yourCollectionName.findOne()方法:

test> db.newMyCollection.findOne()
{ _id: ObjectId("654ed4c0fba0f2e5ddf52464"), name: 'Jhone', age: 23 }
test>

4.3.3、更新文档

使用db.yourCollectionName.updateOne()方法更新单个文档,例如下面的命令将名字为Jhone的文档中的age更新为34:
test> db.newMyCollection.updateOne({name:“Jhone”},{$set:{age:34}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
db.yourCollectionName.updateMany()方法更新符合条件的多个文档:

test> db.newMyCollection.updateMany({name:"Jhone"}, {$set:{age:34}})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 4,
  modifiedCount: 3,
  upsertedCount: 0
}
test> db.newMyCollection.find()
[
  { _id: ObjectId("654ed4c0fba0f2e5ddf52464"), name: 'Jhone', age: 34 },
  { _id: ObjectId("654eddadfba0f2e5ddf52465"), name: 'Jhone', age: 34 },
  { _id: ObjectId("654ede65fba0f2e5ddf52466"), name: 'Jhone', age: 34 },
  { _id: ObjectId("654ede7efba0f2e5ddf52467"), name: 'Jhone', age: 34 },
  { _id: ObjectId("654ede7efba0f2e5ddf52468"), name: 'Peter', age: 20 }
]

4.3.4、删除文档

使用 db.yourCollectionName.deleteOne() 方法删除单个文档:

test> db.newMyCollection.deleteOne({name:"Jhone"})
{ acknowledged: true, deletedCount: 1 }
test>

使用使用 db.yourCollectionName.deleteMany() 方法删除符合条件的多个文档:

test> db.newMyCollection.deleteMany({name:"Jhone"})
{ acknowledged: true, deletedCount: 3 }
test>

5、总结

以上就是MongoDB的单机安装与简单使用,高阶内容敬请期待。
我是欧阳方超,把事情做好了自然就有兴趣了,如果你喜欢我的文章,欢迎点赞、转发、评论加关注。我们下次见。
这一篇是在酒店所写,写了几个小时终于写完,好畅快:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值