无法连接到 google colab 上的 mongodb 本地服务器
有没有办法在 google colab 上使用我的本地 mongodb 服务器我无法使用本地主机链接访问它它正在创建一个新数据库,因为它有不同的 IP 地址也许
import datetime
import random
import string
import uuid
import time
from pymongo import MongoClient
class DataObj:
def __init__(self, db, collection):
self.db = db[collection]
self.collection = collection
self.id = str(uuid.uuid4())[:6]
def insert(self, n_sensors):
keys_data = [self.collection + '_id','room_name','temperature', 'date']
list_data = []
list_room_name = ['bedroom','livingroom','guestroom','kitchen']
instrument = ['AirCondition','fan','refrigerator']
for i in range(n_sensors):
room_name= random.choice(list_room_name)
instrument_name = random.choice(instrument)
value1 = self.id + room_name+ 'sns' + str(i)
value2 = random.uniform(85.5,90)
date = datetime.datetime.now()
temperature = {'sensor_id': value1,'instruments':instrument_name,'temperature':value2 }
json = dict(zip(keys_data, [self.id,room_name ,temperature, date]))
# print(json)
list_data.append(json)
self.db.insert_many(list_data)
client = MongoClient('mongodb://localhost:27017/')
db = client['smart-housedb']
house_1 = DataObj(db, 'house_1')
house_2 = DataObj(db, 'house_2')
s = time.time()
# for j in range(10):
# house_1.insert(3)
# house_2.insert(5)
e = time.time()
# print(e-s)
def fiveHundredMb():
house_1.insert(3)
house_2.insert(5)
dicti = db.command("dbstats")
t1 = time.time()
while int(dicti["totalSize"]) <= 500000000:
fiveHundredMb()
dicti = db.command("dbstats")
# print(dicti["totalSize"])
t2 = time.time()
print(t2-t1)
这是我在 Visual Studio 代码上运行的本地代码它正在运行,我可以通过我的终端使用 mongodb 查询来处理它,但是当我在 google colab 中运行相同的代码时,它要求我启动 mongo 服务器服务并且它正在创建新的数据库,他们是否有机会在我的 google 上运行相同的数据库科拉布?
Is there any way to use my local mongodb server on google colab I can't access it with the local host link it is creating a new database as It has different IP address maybe
import datetime
import random
import string
import uuid
import time
from pymongo import MongoClient
class DataObj:
def __init__(self, db, collection):
self.db = db[collection]
self.collection = collection
self.id = str(uuid.uuid4())[:6]
def insert(self, n_sensors):
keys_data = [self.collection + '_id','room_name','temperature', 'date']
list_data = []
list_room_name = ['bedroom','livingroom','guestroom','kitchen']
instrument = ['AirCondition','fan','refrigerator']
for i in range(n_sensors):
room_name= random.choice(list_room_name)
instrument_name = random.choice(instrument)
value1 = self.id + room_name+ 'sns' + str(i)
value2 = random.uniform(85.5,90)
date = datetime.datetime.now()
temperature = {'sensor_id': value1,'instruments':instrument_name,'temperature':value2 }
json = dict(zip(keys_data, [self.id,room_name ,temperature, date]))
# print(json)
list_data.append(json)
self.db.insert_many(list_data)
client = MongoClient('mongodb://localhost:27017/')
db = client['smart-housedb']
house_1 = DataObj(db, 'house_1')
house_2 = DataObj(db, 'house_2')
s = time.time()
# for j in range(10):
# house_1.insert(3)
# house_2.insert(5)
e = time.time()
# print(e-s)
def fiveHundredMb():
house_1.insert(3)
house_2.insert(5)
dicti = db.command("dbstats")
t1 = time.time()
while int(dicti["totalSize"]) <= 500000000:
fiveHundredMb()
dicti = db.command("dbstats")
# print(dicti["totalSize"])
t2 = time.time()
print(t2-t1)
This is my local code which I ran on m y visual studio code it is running and I can aches it through my terminal with mongodb queries but when I run the same code in google colab it asked me to start mongo server services and it is creating new database, is their any chance to run same database on my google colab?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
本地主机意味着您的数据库无法在线访问。 Google collab 是一项云编程服务,这意味着您在服务器上运行代码。您在 Google collab 上的代码将无法访问您的本地数据库。您应该创建一个在线数据库,然后 Google collab 将可以访问它。
在这里你可以免费创建一个在线MongoDB。
Localhost means your DB is not accessible online. Google collab is a cloud programming service, this means you run code on a server. Your code on Google collab will not be able to have access to your local DB. you should create an online DB, and then Google collab will have access to it.
Here you can create an online MongoDB for free.
好吧,Colab 不支持连接到 MongoDb Atlas
.......mongodb.net:27017: 连接已关闭
Well, Colab does not support connect to MongoDb Atlas
.......mongodb.net:27017: connection closed