截至 2021-01-12 最新 MongoDB 服务器单台环境搭建参考

Posted Defonds

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了截至 2021-01-12 最新 MongoDB 服务器单台环境搭建参考相关的知识,希望对你有一定的参考价值。

文章目录


MongoDB 高可用参考博客《 我们的一个已投产项目的高可用数据库实战 - mongo 副本集的搭建详细过程》。
本文验证环境:Centos8.1,64 位。

1.下载服务器的rpm安装包

https://www.mongodb.com/try/download/community

mongodb-org-server-4.2.11-1.el8.x86_64.rpm

2. 安装Python2

2.1. 直接rmp安装 python2 is needed

[root@3sistbq5kn6q9Z ~]# rpm -ivh mongodb-org-server-4.2.11-1.el8.x86_64.rpm

warning: mongodb-org-server-4.2.11-1.el8.x86_64.rpm: Header V3 RSA/SHA1 Signatur e, key ID 058f8b6b: NOKEY error: Failed dependencies: python2 is needed by mongodb-org-server-4.2.11-1.el8.x86_64

2.2. 安装并配置Python2

[root@3sistbq5kn6q9Z ~]# whereis python2

python2:

[root@3sistbq5kn6q9Z ~]# dnf install python2

CentOS-8 - AppStream 88 kB/s | 4.3 kB 00:00 CentOS-8 - Base 119 kB/s | 3.9 kB 00:00 CentOS-8 - Extras 46 kB/s | 1.5 kB 00:00 Extra Packages for Enterprise Linux 8 - x86_64 243 kB/s | 4.7 kB 00:00 Dependencies resolved. Package Arch Version Repo Size Installing: python2 x86_64 2.7.17-2.module_el8.3.0+478+7570e00c AppStream 109 k Installing dependencies: python2-libs x86_64 2.7.17-2.module_el8.3.0+478+7570e00c AppStream 6.0 M python2-pip-wheel noarch 9.0.3-18.module_el8.3.0+478+7570e00c AppStream 1.0 M python2-setuptools-wheel noarch 39.0.1-12.module_el8.3.0+478+7570e00c AppStream 287 k Installing weak dependencies: python2-pip noarch 9.0.3-18.module_el8.3.0+478+7570e00c AppStream 1.7 M python2-setuptools noarch 39.0.1-12.module_el8.3.0+478+7570e00c AppStream 642 k Enabling module streams: python27 2.7 Transaction Summary Install 6 Packages Total download size: 9.7 M Installed size: 37 M Is this ok [y/N]: y Downloading Packages: (1/6): python2-pip-9.0.3-18.module_el8.3.0+478+ 22 MB/s | 1.7 MB 00:00 (2/6): python2-2.7.17-2.module_el8.3.0+478+7570 1.3 MB/s | 109 kB 00:00 (3/6): python2-pip-wheel-9.0.3-18.module_el8.3. 38 MB/s | 1.0 MB 00:00 (4/6): python2-setuptools-wheel-39.0.1-12.modul 50 MB/s | 287 kB 00:00 (5/6): python2-setuptools-39.0.1-12.module_el8. 6.2 MB/s | 642 kB 00:00 Total 36 MB/s | 9.7 MB 00:00 Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : python2-setuptools-wheel-39.0.1-12.module_el8.3.0+47 1/6 Installing : python2-pip-wheel-9.0.3-18.module_el8.3.0+478+7570e0 2/6 Installing : python2-libs-2.7.17-2.module_el8.3.0+478+7570e00c.x8 3/6 Installing : python2-pip-9.0.3-18.module_el8.3.0+478+7570e00c.noa 4/6 Installing : python2-setuptools-39.0.1-12.module_el8.3.0+478+7570 5/6 Installing : python2-2.7.17-2.module_el8.3.0+478+7570e00c.x86_64 6/6 Running scriptlet: python2-2.7.17-2.module_el8.3.0+478+7570e00c.x86_64 6/6 Verifying : python2-2.7.17-2.module_el8.3.0+478+7570e00c.x86_64 1/6 Verifying : python2-libs-2.7.17-2.module_el8.3.0+478+7570e00c.x8 2/6 Verifying : python2-pip-9.0.3-18.module_el8.3.0+478+7570e00c.noa 3/6 Verifying : python2-pip-wheel-9.0.3-18.module_el8.3.0+478+7570e0 4/6 Verifying : python2-setuptools-39.0.1-12.module_el8.3.0+478+7570 5/6 Verifying : python2-setuptools-wheel-39.0.1-12.module_el8.3.0+47 6/6 Installed: python2-2.7.17-2.module_el8.3.0+478+7570e00c.x86_64 python2-pip-9.0.3-18.module_el8.3.0+478+7570e00c.noarch python2-setuptools-39.0.1-12.module_el8.3.0+478+7570e00c.noarch python2-libs-2.7.17-2.module_el8.3.0+478+7570e00c.x86_64 python2-pip-wheel-9.0.3-18.module_el8.3.0+478+7570e00c.noarch python2-setuptools-wheel-39.0.1-12.module_el8.3.0+478+7570e00c.noarch Complete!

[root@3sistbq5kn6q9Z ~]# whereis python2

python2: /usr/bin/python2 /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/pytho n2.7 /usr/include/python2.7 /usr/share/man/man1/python2.1.gz

[root@3sistbq5kn6q9Z ~]# ln -s /usr/bin/python2

/bin/python

[root@3sistbq5kn6q9Z ~]# python

Python 2.7.17 (default, Aug 31 2020, 21:02:14) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> exit Use exit() or Ctrl-D (i.e. EOF) to exit >>> exit()

3. 执行安装rpm

[root@3sistbq5kn6q9Z ~]# dir

mongodb-org-server-4.2.11-1.el8.x86_64.rpm

[root@3sistbq5kn6q9Z ~]# rpm -ivh mongodb-org-server-4.2.11-1.el8.x86_64.rpm

warning: mongodb-org-server-4.2.11-1.el8.x86_64.rpm: Header V3 RSA/SHA1 Signatur e, key ID 058f8b6b: NOKEY Verifying... ################################# [100%] Preparing... ################################# [100%] Updating / installing... 1:mongodb-org-server-4.2.11-1.el8 ################################# [100%] sreated symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /u r/lib/systemd/system/mongod.service.

[root@3sistbq5kn6q9Z ~]#

4. 配置并启动mongod

4.1. 处理异常code=exited, status=100

[root@3sistbq5kn6q9Z etc]# systemctl start mongod

Job for mongod.service failed because the control process exited with error code . See "systemctl status mongod.service" and "journalctl -xe" for details.

[root@3sistbq5kn6q9Z etc]# systemctl status

mongod.service ● mongod.service - MongoDB Database Server Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor pres> Active: failed (Result: exit-code) since Fri 2021-01-08 21:00:25 CST; 35s ago Docs: https://docs.mongodb.org/manual Process: 5589 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=100) Process: 5586 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited,> Process: 5585 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (cod> Process: 5583 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, s> Jan 08 21:00:25 3sistbq5kn6q9Z systemd[1]: Starting MongoDB Database S> Jan 08 21:00:25 3sistbq5kn6q9Z mongod[5589]: about to fork child proce> Jan 08 21:00:25 3sistbq5kn6q9Z mongod[5589]: forked process: 5591 Jan 08 21:00:25 3sistbq5kn6q9Z mongod[5589]: ERROR: child process fail> Jan 08 21:00:25 3sistbq5kn6q9Z mongod[5589]: To see additional informa> Jan 08 21:00:25 3sistbq5kn6q9Z systemd[1]: mongod.service: Control pro> Jan 08 21:00:25 3sistbq5kn6q9Z systemd[1]: mongod.service: Failed with> Jan 08 21:00:25 3sistbq5kn6q9Z systemd[1]: Failed to start MongoDB Dat>

[root@3sistbq5kn6q9Z etc]# systemctl status

mongod.service > ~err.log

[root@3sistbq5kn6q9Z etc]# chown -R mongodb:mongodb /var/lib/mongodb

chown: invalid user: ‘mongodb:mongodb’

[root@3sistbq5kn6q9Z etc]# chown -R mongod:mongod /var/lib/mongodb

chown: cannot access '/var/lib/mongodb': No such file or directory

[root@3sistbq5kn6q9Z etc]# chown -R mongod:mongod

/var/run/mongodb

[root@3sistbq5kn6q9Z etc]# systemotl restart mongod

-bash: systemotl: command not found
[root@3sistbq5kn6q9Z etc]# systemctl restart mongod
Job for mongod.service failed because the control process exited with error code . See "systemctl status mongod.service" and "journalctl -xe" for details.

[root@3sistbq5kn6q9Z etc]# systemctl status mongod.service

● mongod.service - MongoDB Database Server Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor pres> Active: failed (Result: exit-code) since Fri 2021-01-08 21:07:56 CST; 26s ago Docs: https://docs.mongodb.org/manual Process: 5624 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=100) Process: 5621 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited,> Process: 5620 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (cod> Process: 5618 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, s> Jan 08 21:07:56 3sistbq5kn6q9Z systemd[1]: Starting MongoDB Database S> Jan 08 21:07:56 3sistbq5kn6q9Z mongod[5624]: about to fork child proce> Jan 08 21:07:56 3sistbq5kn6q9Z mongod[5624]: forked process: 5626 Jan 08 21:07:56 3sistbq5kn6q9Z mongod[5624]: ERROR: child process fail> Jan 08 21:07:56 3sistbq5kn6q9Z mongod[5624]: To see additional informa> Jan 08 21:07:56 3sistbq5kn6q9Z systemd[1]: mongod.service: Control pro> Jan 08 21:07:56 3sistbq5kn6q9Z systemd[1]: mongod.service: Failed with> Jan 08 21:07:56 3sistbq5kn6q9Z systemd[1]: Failed to start MongoDB Dat>
[root@3sistbq5kn6q9Z etc]# journalctl -xe
-- The result is RESULT. Jan 08 21:01:01 3sistbq5kn6q9Z CROND[5599]: (root) CMD (run-parts /etc> Jan 08 21:01:01 3sistbq5kn6q9Z run-parts[5602]: (/etc/cron.hourly) sta> Jan 08 21:01:01 3sistbq5kn6q9Z run-parts[5608]: (/etc/cron.hourly) fin> Jan 08 21:07:56 3sistbq5kn6q9Z systemd[1]: Starting MongoDB Database S> -- Subject: Unit mongod.service has begun start-up -- Defined-By: systemd -- Support: https://access.redhat.com/support -- -- Unit mongod.service has begun starting up. Jan 08 21:07:56 3sistbq5kn6q9Z mongod[5624]: about to fork child proce> Jan 08 21:07:56 3sistbq5kn6q9Z mongod[5624]: forked process: 5626 Jan 08 21:07:56 3sistbq5kn6q9Z mongod[5624]: ERROR: child process fail> Jan 08 21:07:56 3sistbq5kn6q9Z mongod[5624]: To see additional informa> Jan 08 21:07:56 3sistbq5kn6q9Z systemd[1]: mongod.service: Control pro> Jan 08 21:07:56 3sistbq5kn6q9Z systemd[1]: mongod.service: Failed with> Jan 08 21:07:56 3sistbq5kn6q9Z systemd[1]: Failed to start MongoDB Dat> -- Subject: Unit mongod.service has failed -- Defined-By: systemd -- Support: https://access.redhat.com/support -- -- Unit mongod.service has failed. -- -- The result is RESULT.

[root@3sistbq5kn6q9Z etc]# cd /data/mongo
[root@3sistbq5kn6q9Z mongo]# chown -R mongod:mongod /data
[root@3sistbq5kn6q9Z mongo]# cd …
[root@3sistbq5kn6q9Z data]# cd …
[root@3sistbq5kn6q9Z /]# ll

total 24 lrwxrwxrwx. 1 root root 7 May 11 2019 bin -> usr/bin dr-xr-xr-x. 5 root root 4096 Feb 18 2020 boot drwxr-xr-x 3 mongod mongod 19 Jan 8 20:57 data drwxr-xr-x 19 root root 2940 Jan 2 16:33 dev drwxr-xr-x. 97 root root 8192 Jan 8 21:02 etc drwxr-xr-x. 2 root root 6 May 11 2019 home lrwxrwxrwx. 1 root root 7 May 11 2019 lib -> usr/lib lrwxrwxrwx. 1 root root 9 May 11 2019 lib64 -> usr/lib64 drwxr-xr-x. 2 root root 6 May 11 2019 media drwxr-xr-x. 2 root root 6 May 11 2019 mnt drwxr-xr-x. 2 root root 6 May 11 2019 opt dr-xr-xr-x 104 root root 0 Jan 2 16:33 proc dr-xr-x---. 6 root root 239 Jan 7 23:03 root drwxr-xr-x 31 root root 900 Jan 8 20:41 run lrwxrwxrwx. 1 root root 8 May 11 2019 sbin -> usr/sbin drwxr-xr-x. 2 root root 6 May 11 2019 srv dr-xr-xr-x 13 root root 0 Jan 3 00:33 sys drwxrwxrwt. 9 root root 4096 Jan 8 21:07 tmp drwxr-xr-x. 12 root root 144 Feb 18 2020 usr drwxr-xr-x. 21 root root 4096 Feb 18 2020 var

[root@3sistbq5kn6q9Z /]# cd data
[root@3sistbq5kn6q9Z data]# ll

total 0 drwxr-xr-x 2 mongod mongod 6 Jan 8 20:57 mongo

[root@3sistbq5kn6q9Z data]# cd mongo/
[root@iZuf6ad6x3sistbq5kn6q9Z mongo]# ll

total 0

[root@3sistbq5kn6q9Z mongo]# systemctl restart mongod
[root@3sistbq5kn6q9Z mongo]# systemctl status mongod

● mongod.service - MongoDB Database Server Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor pres> Active: active (running) since Fri 2021-01-08 21:12:39 CST; 7s ago Docs: https://docs.mongodb.org/manual Process: 5651 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCE> Process: 5649 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited,> Process: 5647 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (cod> Process: 5645 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, s> Main PID: 5653 (mongod) Memory: 80.8M CGroup: /system.slice/mongod.service └─5653 /usr/bin/mongod -f /etc/mongod.conf Jan 08 21:12:39 3sistbq5kn6q9Z systemd[1]: Starting MongoDB Database S> Jan 08 21:12:39 3sistbq5kn6q9Z mongod[5651]: about to fork child proce> Jan 08 21:12:39 3sistbq5kn6q9Z mongod[5651]: forked process: 5653 Jan 08 21:12:39 i3sistbq5kn6q9Z mongod[5651]: child process started suc> Jan 08 21:12:39 3sistbq5kn6q9Z systemd[1]: Started MongoDB Database Se>

[root@3sistbq5kn6q9Z mongo]#

5. 连接控制

5.1. 下载mongo shell安装包

https://www.mongodb.com/try/download/shell

mongosh-0.6.1-x86_64.rpm

5.2. 安装mongosh

[root@3sistbq5kn6q9Z ~]# rpm -ivh mongosh-0.6.1-x86_64.rpm

Verifying... ################################# [100%] Preparing... ################################# [100%] Updating / installing... 1:mongosh-0.6.1-1 ################################# [100%]

5.3. 连接mongo

[root@3sistbq5kn6q9Z ~]# mongo --host 127.0.0.1:27017

-bash: mongo: command not found

[root@3sistbq5kn6q9Z ~]# whereis mongo

mongo:

启动mongod后,缺省状态下可匿名访问
[root@3sistbq5kn6q9Z ~]# mongosh
“mongodb://localhost:27017”

Current sessionID: 5ff8651c135341cac9593656 Connecting to: mongodb://localhost:27017 Using MongoDB: 4.2.11 Using Mongosh Beta: 0.6.1 For more information about mongosh, please see our docs: https://docs.mongodb.co m/mongodb-shell/ To help improve our products, anonymous usage data is collected and sent to Mong oDB periodically (https://www.mongodb.com/legal/privacy-policy). You can opt-out by running the disableTelemetry() command. >

参考自:

6. 建立用户,建应用库和应用用户

6.1. 建库

> use defondscloud

switched to db defondscloud

> db.usr.insert(‘name’:‘tompig’);

DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany or bulkWrite. acknowledged: true, insertedIds: '0': ObjectId("5ff870a2ce3b631665ec28ef")

> db.usr.insertOne(‘name’:‘tompig1’,‘id’:1);

acknowledged: true, insertedId: ObjectId("5ff870c2ce3b631665ec28f0")

> show collections;

usr

> show dbs;

admin 41 kB config 94.2 kB local 41 kB defondscloud 41 kB

>

6.2. 配置用户

db.createUser(

user: “mongodb”,
pwd: “defonds2345”, // or cleartext password
roles: [ role: “readWrite”, db: “defondscloud” ]

);

参考自:https://docs.mongodb.com/manual/tutorial/create-users/

6.3. 允许远程访问

vi /etc/mongod.conf

bindIp: 127.0.0.1
修改为
bindIp: 0.0.0.0

然后重启mongod服务:
systemctl restart mongod.service

参考自:https://medium.com/founding-ithaka/setting-up-and-connecting-to-a-remote-mongodb-database-5df754a4da89

7. 远程GUI连接数据库

7.1. 下载官方GUI工具compass

https://www.mongodb.com/try/download/compass
mongodb-compass-1.24.6-win32-x64.msi
使用管理员CMD执行之安装。

7.2. 使用连接compass连接

以上是关于截至 2021-01-12 最新 MongoDB 服务器单台环境搭建参考的主要内容,如果未能解决你的问题,请参考以下文章

1.MongoDB简介

MongoDB用Docker安装一个MongoDB最新版玩玩

北京大会福利 | 送MongoDB最新书籍

最新版 MongoDB 安装

每个项目 mongodb 组的最新记录(按日期)

获取 MongoDB 的最新年份