I use docker-compose to start mongo on my local computer with a username and password. It looks running OK.
Then I run a python script, import pymongo to create a database. There's no error sending out. However, the new database is not created at all.
My docker-compose.yml script is as below
version: '3'
services:
mongodb:
image: mongo
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=mongodb
- MONGO_INITDB_ROOT_PASSWORD=mongodb
volumes:
- ./mongodb-data:/data/db
mongo-express:
image: mongo-express:0.54
ports:
- 8080:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=mongodb
- ME_CONFIG_MONGODB_ADMINPASSWORD=mongodb
- ME_CONFIG_MONGODB_SERVER=mongodb
depends_on:
- mongodb
volumes:
- ./mongodb-data:/data/db
volumes:
db-data:
Part of my python script is as below
from flask import Flask, render_template, request
from pymongo import MongoClient
app = Flask(__name__)
dbclient = MongoClient(
"mongodb://localhost:27017/",
username = 'mongodb',
password = 'mongodb'
)
appdb = dbclient['blog']
print(dbclient.list_database_names())
The shell output is only the system mongo's databases
['admin', 'config', 'local']
Can any one point out what I need to do to fix? Thanks a lot.
Try this instead, Add collection & some test data, Should work. Mongo doesn't create database without collection & data.
appdb = dbclient["blog"]
appcoll = appdb["blogcollection"]
document = {"user_id": 1, "user": "test"}
appcoll.insert_one(document)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.