能否在未本地安装MongoDB的树莓派3B上用PyMongo连接AWS EC2的MongoDB?
Absolutely! You don’t need to install MongoDB locally on your Raspberry Pi 3B (even with its 32-bit OS running on a 64-bit processor) to connect to a MongoDB instance hosted on AWS EC2 using PyMongo. Here’s how to make it work reliably:
Step 1: Install PyMongo on your Raspberry Pi
PyMongo offers pre-built packages compatible with 32-bit ARM systems (which your Pi’s OS uses), so installation is straightforward. Open a terminal on your Pi and run:
pip3 install pymongo # Use pip instead if you're still on Python 2 (though we recommend Python 3)
This will install all the dependencies needed to establish a remote connection to MongoDB—no local MongoDB server required.
Step 2: Configure your AWS EC2 MongoDB instance
Before you can connect, you need to ensure your EC2-hosted MongoDB allows remote connections from your Pi:
- Update EC2 Security Group: Go to the AWS Console, navigate to your EC2 instance’s security group, and add an inbound rule allowing traffic on port
27017(MongoDB’s default port) from your Raspberry Pi’s public IP address. For testing purposes, you can temporarily allow0.0.0.0/0, but restrict it to your Pi’s IP for production use to keep things secure. - Adjust MongoDB Bind IP: On your EC2 instance, edit the MongoDB configuration file (usually located at
/etc/mongod.conf). Find thenetsection and setbindIpto0.0.0.0(to allow connections from any IP) or your Pi’s public IP. Save the file and restart the MongoDB service:sudo systemctl restart mongod - Set Up Authentication (Recommended): If you haven’t already, create a MongoDB user with remote access permissions. Connect to your local MongoDB instance on EC2, switch to your target database, and run:
Enable authentication in thedb.createUser({ user: "your_username", pwd: "your_secure_password", roles: [{ role: "readWrite", db: "your_target_db" }] })mongod.conffile by uncommenting thesecurity.authorization: enabledline, then restart MongoDB again.
Step 3: Write your PyMongo connection code
Once the setup is done, you can connect from your Pi with a simple Python script. Here’s an example:
from pymongo import MongoClient # Replace placeholders with your EC2 details and MongoDB credentials ec2_public_ip = "your_ec2_public_ip" username = "your_mongodb_username" password = "your_mongodb_password" target_db = "your_target_database" # Establish the connection client = MongoClient(f"mongodb://{username}:{password}@{ec2_public_ip}:27017/{target_db}") # Test the connection by inserting a sample document db = client[target_db] test_collection = db.test_collection result = test_collection.insert_one({"device": "Raspberry Pi 3B", "status": "Connected successfully!"}) print(f"Connection worked! Inserted document with ID: {result.inserted_id}")
Run this script on your Pi—if everything’s configured correctly, you’ll see a success message, and the document will appear in your EC2 MongoDB instance.
Troubleshooting common issues
- If you get a connection timeout: Double-check your EC2 security group rules and ensure your Pi can ping the EC2 public IP.
- If authentication fails: Verify your username, password, and that the user has permissions for the target database.
- If MongoDB refuses the connection: Confirm the
bindIpsetting inmongod.confis correct and the MongoDB service is running on EC2 (usesudo systemctl status mongodto check).
内容的提问来源于stack exchange,提问作者Caolan McBride

