树莓派运行 docker_如何使用树莓派和docker在本地网络上运行mongodb

树莓派运行 docker

NoSQL databases have been trending upward in popularity and adoption in recent years. MongoDB has separated itself from the NoSQL pack because it is a highly scalable and easy to learn technology. Its flexible data model allows for adding and changing fields easily, allowing applications to decide the data schema instead of forcing an application to fit a data schema. In this post I want to walk through my experience of setting up MongoDB on a local network so that multiple devices can communicate with the same database. To accomplish this, I’m going to configure a Raspberry Pi, running the recently supported Ubuntu Server, to run a MongoDB Docker container that will be configured so that it is accessible to any machine on my network. Of course you don’t need to use Docker to run MongoDB. I found the tradeoff between performance overhead and ease of use to be acceptable for my use case, at least to start things off.

近年来,NoSQL数据库的普及和采用趋势呈上升趋势。 MongoDB已从NoSQL包中脱颖而出,因为它是高度可伸缩且易于学习的技术。 其灵活的数据模型允许轻松地添加和更改字段,从而允许应用程序确定数据模式,而不是强制应用程序适合数据模式。 在这篇文章中,我想介绍一下我在本地网络上设置MongoDB的经验,以便多个设备可以与同一个数据库进行通信。 为此,我将配置运行最近受支持的Ubuntu Server的Raspberry Pi,以运行将配置的MongoDB Docker容器,以便我的网络上的任何计算机都可以访问它。 当然,您无需使用Docker即可运行MongoDB。 我发现我的用例可以接受性能开销和易用性之间的折衷,至少是从头开始。

为什么在Raspberry Pi上? (Why on a Raspberry Pi?)

There are a number of reasons why you could want MongoDB running on a Raspberry Pi. Firstly, the Raspberry Pi was designed as an affordable learning device, so you could be using it as a sandbox for learning a new technology like MongoDB. You could also be trying to cut costs if you have low resource requirements by hosting your own database. You could have multiple IoT devices on your network whose data you want to centralize, so you can back up, run analytics, or even source ML training data from one place. Personally I had a couple Raspberry Pis sitting around and wanted to stay technology woke, so I’ll share the process I took in setting this up and enable anyone to do their own projects. In my experience, I tend get in my own way in learning a new thing or starting a project because I’m not sure how to get started. Hopefully this helps break down that barrier!

有许多原因使您希望MongoDB在Raspberry Pi上运行。 首先,Raspberry Pi被设计为一种经济实惠的学习设备,因此您可以将其用作学习MongoDB等新技术的沙箱。 如果您对资源的要求不高,也可以通过托管自己的数据库来降低成本。 您的网络上可能有多个要集中其数据的IoT设备,因此您可以从一个地方备份,运行分析甚至获取ML培训数据。 就我个人而言,我有几个Raspberry Pis闲坐着,想让技术醒来,所以我将分享设置过程中使用的过程,并使任何人都能进行自己的项目。 以我的经验,我倾向于以自己的方式学习新事物或开始一个项目,因为我不确定如何开始。 希望这有助于打破障碍!

设置Raspberry Pi (Setting up the Raspberry Pi)

To get things going, set up Ubuntu Server on the Raspberry Pi. I’m not going through the setup process here because the folks at Ubuntu wrote some great instructions here. The setup used here was on a Raspberry Pi 3B+ over private WiFi (i.e. my home, not a coffee shop), using Ubuntu server 20.04 (the LTS at the time of this writing). The default user after setting up should be ubuntu. After setting this up, I ran it as headless and executed commands using ssh; connecting a monitor and keyboard to the Raspberry Pi also works fine. Once you get the Raspberry Pi set up, it should be relatively easy from here on out.

为了使事情顺利进行,请在Raspberry Pi上设置Ubuntu Server。 我不在这里进行设置过程,因为Ubuntu的人们在这里写了一些很好的说明。 这里使用的设置是在Raspberry Pi 3B +上通过私有WiFi(即我的家,而不是咖啡店),使用Ubuntu服务器20.04(在撰写本文时为LTS)。 设置后的默认用户应该是ubuntu 。 设置好之后,我将其作为无头文件运行并使用ssh执行命令; 将显示器和键盘连接到Raspberry Pi也可以正常工作。 设置好Raspberry Pi后,从现在开始它应该相对容易。

安装Docker (Installing Docker)

First step after setting up the Raspberry Pi is installing Docker. Installing Docker is a simple process on Ubuntu. According to the Docker docs, run the following three commands

设置Raspberry Pi后的第一步是安装Docker。 在Ubuntu上,安装Docker是一个简单的过程。 根据Docker文档,运行以下三个命令

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo usermod -aG docker ubuntu

This should install Docker on the Raspberry Pi and allow running it without having to use sudo. You can confirm that Docker is installed properly by running docker version.

这应该在Raspberry Pi上安装Docker并允许运行它,而不必使用sudo。 您可以通过运行docker version确认Docker已正确安装。

启动MongoDB容器 (Starting up the MongoDB container)

Next step is to start up the MongoDB Docker container. Running the container is as simple as the following line of code

下一步是启动MongoDB Docker容器。 运行容器就像下面的代码行一样简单

docker run -d -p 27017:27017 -v ~/data:/data/db --name mongo mongo:bionic

Breaking this down, firstly I chose the image mongo:bionic because it was the latest image I could find that was compiled for the arm64 cpu architecture. Other images may work, but I wanted to play it on the safe side. The -d flag ensures that the container runs as detached. The -p 27017:27017 flag maps port 27017 of the container to port 27017 on the host. This is the default port that MongoDB runs on and we are just making it externally available. The -v ~/data:/data/db flag volume mounts /data/db in the container to ~/data on the host. This is important in persisting data if the container needs to be restarted, etc. Lastly --name mongo names the container something meaningful

分解一下,首先,我选择了映像mongo:bionic因为它是我发现的最新映像,是针对arm64 cpu架构编译的。 其他图像可能会起作用,但我想在安全方面进行播放。 -d标志可确保容器以分离状态运行。 -p 27017:27017标志将容器的端口27017映射到主机上的端口27017。 这是MongoDB运行的默认端口,我们只是使其在外部可用。 -v ~/data:/data/db标志卷将容器中的/data/db挂载到主机上的~/data 。 如果需要重新启动容器等,这对于持久化数据很重要。最后--name mongo为容器命名了一些有意义的东西

如何检查容器是否正在运行 (How to check if the container is running)

It’s important to ensure that the container is up and running as you expect, which can be accomplished by running docker ps (apologies for Medium’s line wrapping)

确保容器按预期启动并运行很重要,这可以通过运行docker ps (为Medium换行道歉)来完成

ubuntu@ubuntu:~$ docker psCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMESb6f2f789a11f        mongo:bionic        "docker-entrypoint.s…"   12 hours ago        Up 12 hours         0.0.0.0:27017->27017/tcp   mongo

Note the line 0.0.0.0:27017->27017/tcp. It’s important that the Docker container is mapped to the host’s 0.0.0.0 address. This, while it does have its security risks, makes the Docker container available to other machines through the Raspberry Pi’s IP address. The security risk is mitigated here by the fact that this is only available on my private local network.

注意行0.0.0.0:27017->27017/tcp 。 将Docker容器映射到主机的0.0.0.0地址非常重要。 尽管这样做确实存在安全隐患,但仍可以通过Raspberry Pi的IP地址将Docker容器提供给其他机器。 由于此风险仅在我的专用本地网络上可用,因此可以缓解安全风险。

连接到MongoDB容器 (Connecting to the MongoDB container)

Now that the Docker container is up and running on your local network, you can connect to the database using the Python client. You will need to get the IP address of the Raspberry Pi, which can be done through a variety of ways. For example, I checked my router’s device list to see what address my Raspberry Pi was running on. For the sake of example, let’s say the Raspberry Pi is running on 192.168.100.100. Ensure that pymongo is installed on your machine, and run the following code pulled from this guide

现在,Docker容器已在本地网络上启动并运行,您可以使用Python客户端连接到数据库了。 您将需要获取Raspberry Pi的IP地址,可以通过多种方式来完成。 例如,我检查了路由器的设备列表,以查看Raspberry Pi运行的地址。 举例来说,假设Raspberry Pi在192.168.100.100上运行。 确保您的计算机上安装了pymongo ,然后运行从本指南中提取的以下代码

import pymongomyclient = pymongo.MongoClient("192.168.100.100:27017")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]mydict = { "name": "John", "address": "Highway 37" }
mycol.insert_one(mydict)x = mycol.find_one()print(x)

This does the following: creates a client instance, creates a database called mydatabase, creates a collection in customers, and inserts a dictionary into your collection. If it all works correctly then the client should be able to retrieve the dictionary and print it out. If this works successfully then congrats! You’ve now set up a locally networked MongoDB instance on a Raspberry Pi using Docker. If you find that this database is the technology for you and once you’re a MongoDB legend you’ll find that transferring this setup to better hardware to be simple because we used Docker. Good luck with your own learning and projects!

这将执行以下操作:创建一个客户端实例,创建一个名为mydatabase的数据库,在customers创建一个集合,并将字典插入到您的集合中。 如果一切正常,那么客户端应该能够检索字典并将其打印出来。 如果成功,那就恭喜! 现在,您已经使用Docker在Raspberry Pi上设置了本地联网的MongoDB实例。 如果您发现此数据库适合您,并且一旦成为MongoDB传奇,您就会发现将设置转移到更好的硬件变得很简单,因为我们使用了Docker。 祝您自己的学习和项目顺利!

资源资源 (Resources)

翻译自: https://medium.com/swlh/how-to-run-mongodb-on-local-network-using-a-raspberry-pi-and-docker-4e5c4379cea2

树莓派运行 docker

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值