Mongodb-Single Server Install

蒲魁
2023-12-01

官网:MongoDB: the application data platform

下载社区版

MongoDB Community Download

MongoDB的体系结构

  • 数据库服务(mongod)
  • 分片集群部署中,数据和查询的路由服务(mongos)
  • Shell客户端(mongo)

其中最主要的程序当然是mongod(数据库服务),mongod在不同的部署方案中(单机部署,副本集部署,分片集群部署),通过不同的配置,可以扮演多种不同的角色:

  • 在单机部署中提供所有数据库服务读写功能
  • 在副本集部署中,部署可以是 primary节点(主服务器,负责写数据,也可以提供查询)、secondary节点(从服务器,它从主节点复制数据,也可以提供查询)、以及arbiter节点(仲裁节点,不保存数据,主要用于参与选举投票)
  • 在分片集群中,除了在每个分片中扮演上述角色外,还扮演着配置服务的角色(存储有分片集群的所有元数据信息,mongos的数据路由分发等都要依赖于它)

我们在实验中安装单机方案:

1.安装MongoDB

[root@Alma ~]# wget https://repo.mongodb.org/yum/redhat/8/mongodb-org/5.0/x86_64/RPMS/mongodb-org-server-5.0.5-1.el8.x86_64.rpm
--2022-01-21 13:06:01--  https://repo.mongodb.org/yum/redhat/8/mongodb-org/5.0/x86_64/RPMS/mongodb-org-server-5.0.5-1.el8.x86_64.rpm
Resolving repo.mongodb.org (repo.mongodb.org)... 13.35.101.102, 13.35.101.93, 13.35.101.21, ...
Connecting to repo.mongodb.org (repo.mongodb.org)|13.35.101.102|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 29100296 (28M)
Saving to: ‘mongodb-org-server-5.0.5-1.el8.x86_64.rpm’

mongodb-org-server-5.0.5-1.el8.x86_64.r 100%[=============================================================================>]  27.75M  9.49MB/s    in 2.9s    

2022-01-21 13:06:04 (9.49 MB/s) - ‘mongodb-org-server-5.0.5-1.el8.x86_64.rpm’ saved [29100296/29100296]

[root@Alma ~]# ls
anaconda-ks.cfg  hugo                              LICENSE                                    mslearn-frontend-deploy.yaml   Public     Videos
Desktop          hugo_0.91.2_Linux-64bit.tar.gz    mongodb-org-server-5.0.5-1.el8.x86_64.rpm
[root@Alma software]# rpm -ivh mongodb-org-server-5.0.5-1.el8.x86_64.rpm 
warning: mongodb-org-server-5.0.5-1.el8.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID e2c63c11: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:mongodb-org-server-5.0.5-1.el8   ################################# [100%]
Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /usr/lib/systemd/system/mongod.service.

服务软链接安装的时候会建好,在/etc会生成配置文件mangod.conf

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:
"mongod.conf" 44L, 828C written

修改了可访问的客户端地址127.0.0.1—>0.0.0.0

net:
  port: 27017
  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@Alma etc]# systemctl start mongod.service 
[root@Alma etc]# systemctl enable mongod
[root@Alma etc]# ps -ef |grep mongo
mongod    117280       1  2 13:15 ?        00:00:00 /usr/bin/mongod -f /etc/mongod.conf

[root@Alma ~]# which mongod #mongod的安装目录
/usr/bin/mongod

其他:

systemLog.path :mongoDB 系统日志的路径
storage.dbPath :mongoDB 数据文件存放路径
processManagement.pidFilePath : mongoDB 运行时进程文件存放路径
net.port : mongodDB 网络端口号

如果一旦是因为数据损坏,则需要进行如下操作(了解):
1)删除lock文件:

rm -f /var/lib/mongo/data/db/*.lock

2)修复数据:


/usr/bin/mongod --repair --dbpath=/var/lib/mongo/data/db

2.安装mongo并连接服务

[root@Alma software]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel80-5.0.5.tgz
--2022-01-21 13:49:20--  https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel80-5.0.5.tgz
Resolving fastdl.mongodb.org (fastdl.mongodb.org)... 143.204.128.129, 143.204.128.47, 143.204.128.19, ...
Connecting to fastdl.mongodb.org (fastdl.mongodb.org)|143.204.128.129|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 85635488 (82M) [application/gzip]
Saving to: ‘mongodb-linux-x86_64-rhel80-5.0.5.tgz’

mongodb-linux-x86_64-rhel80-5.0.5.tgz   100%[=============================================================================>]  81.67M  10.0MB/s    in 16s     

2022-01-21 13:49:37 (5.24 MB/s) - ‘mongodb-linux-x86_64-rhel80-5.0.5.tgz’ saved [85635488/85635488]

[root@Alma software]# tar -xvf mongodb-linux-x86_64-rhel80-5.0.5.tgz 
mongodb-linux-x86_64-rhel80-5.0.5/LICENSE-Community.txt
mongodb-linux-x86_64-rhel80-5.0.5/MPL-2
mongodb-linux-x86_64-rhel80-5.0.5/README
mongodb-linux-x86_64-rhel80-5.0.5/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel80-5.0.5/bin/install_compass
mongodb-linux-x86_64-rhel80-5.0.5/bin/mongo
mongodb-linux-x86_64-rhel80-5.0.5/bin/mongod
mongodb-linux-x86_64-rhel80-5.0.5/bin/mongos

本地连接

[root@Alma bin]# ll
total 236508
-rwxrwxr-x. 1 zyi zyi     15205 Dec  2 23:34 install_compass
-rwxrwxr-x. 1 zyi zyi  58239392 Dec  2 23:49 mongo
-rwxrwxr-x. 1 zyi zyi 108663728 Dec  3 00:29 mongod
-rwxrwxr-x. 1 zyi zyi  75255888 Dec  3 00:04 mongos
[root@Alma bin]# ./mongo 
MongoDB shell version v5.0.5
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("4251476b-b565-4374-bb82-2d8d11a15b23") }
MongoDB server version: 5.0.5
================
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/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
        https://community.mongodb.com
---
The server generated these startup warnings when booting: 
        2022-01-21T13:15:20.812+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2022-01-21T13:15:20.812+08:00: /sys/kernel/mm/transparent_hugepage/enabled 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()
---
> show databases;
admin   0.000GB
config  0.000GB
local   0.000GB
> exit

远程连接

[root@Alma ~]# mongo 172.16.21.9
MongoDB shell version v5.0.5
connecting to: mongodb://172.16.21.9:27017/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b6f36b5a-a6c6-4612-b4ee-3163137026a6") }
MongoDB server version: 5.0.5
================
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-01-21T14:03:59.440+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2022-01-21T14:03:59.441+08:00: /sys/kernel/mm/transparent_hugepage/enabled 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()

成功!

 类似资料: