在RHEL上安装最新版本的MongoDB
前提是指一个论述或者行动所依据的基本条件或假设。2021年1月4日
– RHEL7.6的AMI(比较旧,8系列是最新的)
– MongoDB4.4.2(根据本文写作时的最新版本)
因为我们只能使用类似EC2的环境,所以请不要指责为什么不使用Amazon DocumentDB。请理解。或者,由于托管服务通常不支持最新版本,所以对于那些一定要使用最新版本MongoDB的人来说,这篇文章可能会有所帮助。
下载MongoDB的tgz文件在中文中,只需要一个选项来释义:从https://www.mongodb.com/try/download/community下载MongoDB企业服务器的存档文件。
-
Choose which type of deployment is best for you:On-Premisesを選択する
- Choose which type of deployment is best for you:On-Premisesを選択する
-
- MongoDB Enterprise Serverのアコーディオンを展開する
-
- version:4.4.2(current)を選択する(デフォルトがcurrentなのでそのまま)
-
- Platform:RedHat / CentOS 6.2+を選択する
-
- package:tgzを選択する
- Downloadボタンをクリックする
可以下载mongodb-linux-x86_64-rhel62-4.4.2.tgz文件。
2. 启动EC2实例
2.1. AMI: 阿里游戏市场在东京地区使用RHEL 7.6在AMI上进行搜索时,
由Red Hat公司提供的RHEL-7.6_HVM_GA-20190128-x86_64-0-Hourly2-GP2的AMI是ami-00b95502a4d51a07e。
由Red Hat公司提供的RHEL-7.6_HVM_GA-20190128-x86_64-0-Hourly2-GP2的AMI是ami-00b95502a4d51a07e。
因为找到了,所以选择这个。
2.2. 实例类型t2.micro (如果只是试用的话,这个足够了)
2.3. 安全组打开TCP/27017 (如果只是试用的话不需要)
3. 安装MongoDB
3.1. sftp连接使用私钥文件 private.pem,通过 sftp 协议,连接到 EC2 用户 ec2-user@[公共 IP]。
3. 安装MongoDB
3.1. sftp连接使用私钥文件 private.pem,通过 sftp 协议,连接到 EC2 用户 ec2-user@[公共 IP]。
将下载的tgz文件上传到服务器上。
3.2. SSH连接使用private.pem作为身份验证密钥,连接到[public ip]上的ec2-user用户。
创建 liblzma.so.0 的符号链接解压tgz文件并启动mongod时,会出现如下错误,因此需要创建liblzma.so.0的符号链接。
[ec2-user@ip-172-30-2-127 bin]$ ./mongod --help
./mongod: error while loading shared libraries: liblzma.so.0: cannot open shared object file: No such file or directory
查看liblzma.so的版本3.3.1。查看/lib64目录下以liblzma开头的所有文件
[ec2-user@ip-172-30-2-127 bin]$ ./mongod --help
./mongod: error while loading shared libraries: liblzma.so.0: cannot open shared object file: No such file or directory
查看liblzma.so的版本3.3.1。查看/lib64目录下以liblzma开头的所有文件
下面是一个实例:
[ec2用户@ip-172-30-2-127 bin]$ 列出/lib64/liblzma*的文件
/lib64/liblzma.so.5 /lib64/liblzma.so.5.2.2
3.3.2 创建符号链接(需要sudo)sudo ln -s /lib64/liblzma.so.5.2.2 /lib64/liblzma.so.0
将/lib64/liblzma.so.5.2.2链接到/lib64/liblzma.so.0。
3.3.3 确认符号链接列出/lib64目录下所有以liblzma开头的文件的详细信息。
实施例子:
[ec2-user@ip-172-30-2-127 bin]$ ls -al /lib64/liblzma*
lrwxrwxrwx. 1 root root 23 Jan 4 01:34 /lib64/liblzma.so.0 -> /lib64/liblzma.so.5.2.2
lrwxrwxrwx. 1 root root 16 Jan 28 2019 /lib64/liblzma.so.5 -> liblzma.so.5.2.2
-rwxr-xr-x. 1 root root 157400 Mar 4 2016 /lib64/liblzma.so.5.2.2[ec2-user@ip-172-30-2-127 bin]$ ls -al /lib64/liblzma*
lrwxrwxrwx. 1 root root 23 Jan 4 01:34 /lib64/liblzma.so.0 链接至 /lib64/liblzma.so.5.2.2
lrwxrwxrwx. 1 root root 16 Jan 28 2019 /lib64/liblzma.so.5 链接至 liblzma.so.5.2.2
-rwxr-xr-x. 1 root root 157400 Mar 4 2016 /lib64/liblzma.so.5.2.2
对策是参考以下网址。
当mod_security在RHEL或Oracle Linux 7上启用时,Oracle HTTP Server无法启动。
https://docs.oracle.com/cd/E92951_01/webtier/administer-ohs/GUID-5571D9DB-8F96-4473-9D61-93249DE726AD.htm
3.4. 展开存档解压mongodb-linux-x86_64-rhel62-4.4.2.tgz文件,使用tar zxvf命令。
只要存在mongodb-linux-x86_64-rhel62-4.4.2目录,就表示成功。
4. 进行动作确认
4.1. 创建数据库目录进入mongodb-linux-x86_64-rhel62-4.4.2目录并创建一个名为db的文件夹。
4.2. 启动MongoDB服务器启动mongod的命令是 bin/mongod –dbpath db &,这将启动mongod。
4.3. 从命令行界面进行连接
使用bin/mongo
4.2. 启动MongoDB服务器启动mongod的命令是 bin/mongod –dbpath db &,这将启动mongod。
4.3. 从命令行界面进行连接
使用bin/mongo
使用bin/mongo
尝试了各种不同的事情
[ec2-user@ip-172-30-2-127 mongodb-linux-x86_64-rhel62-4.4.2]$ bin/mongo
MongoDB shell version v4.4.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
{"t":{"$date":"2021-01-04T01:35:21.601+00:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:52938","connectionId":1,"connectionCount":1}}
{"t":{"$date":"2021-01-04T01:35:21.602+00:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn1","msg":"client metadata","attr":{"remote":"127.0.0.1:52938","client":"conn1","doc":{"application":{"name":"MongoDB Shell"},"driver":{"name":"MongoDB Internal Client","version":"4.4.2"},"os":{"type":"Linux","name":"Red Hat Enterprise Linux Server release 7.6 (Maipo)","architecture":"x86_64","version":"Kernel 3.10.0-957.5.1.el7.x86_64"}}}}
Implicit session: session { "id" : UUID("fc1bfeb5-986f-4f36-ac7b-a278bb5bef57") }
MongoDB server version: 4.4.2
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:
2021-01-04T01:35:11.521+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2021-01-04T01:35:11.521+00: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
2021-01-04T01:35:11.521+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
2021-01-04T01:35:11.521+00:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
2021-01-04T01:35:11.521+00:00: Soft rlimits too low
2021-01-04T01:35:11.521+00:00: currentValue: 1024
2021-01-04T01:35:11.521+00:00: recommendedMinimum: 64000
---
---
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 dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> use admin
switched to db admin
> show collections
system.version
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
show logs show the accessible logger names
show log [name] prints out the last segment of log in memory, 'global' is default
use <db_name> set current database
db.mycoll.find() list objects in collection mycoll
db.mycoll.find( { a : 1 } ) list objects in mycoll where a == 1
it result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x set default number of items to display on shell
exit quit the mongo shell
> use test;
switched to db test
> db.people.insert({"name":"hoge", "age":30})
{"t":{"$date":"2021-01-04T01:37:26.397+00:00"},"s":"I", "c":"STORAGE", "id":20320, "ctx":"conn1","msg":"createCollection","attr":{"namespace":"test.people","uuidDisposition":"generated","uuid":{"uuid":{"$uuid":"7a68d757-0ffe-4a7c-9c1a-a1e15ff56aff"}},"options":{}}}
{"t":{"$date":"2021-01-04T01:37:26.405+00:00"},"s":"I", "c":"INDEX", "id":20345, "ctx":"conn1","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"test.people","index":"_id_","commitTimestamp":{"$timestamp":{"t":0,"i":0}}}}
WriteResult({ "nInserted" : 1 })
> db.people.find()
{ "_id" : ObjectId("5ff27156a04374c0a5db9eb2"), "name" : "hoge", "age" : 30 }
> exit
bye
{"t":{"$date":"2021-01-04T01:37:40.619+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn1","msg":"Connection ended","attr":{"remote":"127.0.0.1:52938","connectionId":1,"connectionCount":0}}
[ec2-user@ip-172-30-2-127 mongodb-linux-x86_64-rhel62-4.4.2]$
4. 随便聊一下
一般人如果使用亚马逊文档数据库(兼容MongoDB),我认为会是个不错的选择。
https://aws.amazon.com/jp/documentdb/
5. 请提供相关链接MongoDB官方网站
