alexellis / mongodb-function Goto Github PK
View Code? Open in Web Editor NEWOpenFaaS Function that makes use of a connection pool to access MongoDB
Home Page: https://www.openfaas.com
License: MIT License
OpenFaaS Function that makes use of a connection pool to access MongoDB
Home Page: https://www.openfaas.com
License: MIT License
# export OPENFAAS_URL=127.0.0.1:31112
# curl http://$OPENFAAS_URL/function/insert-user \
> --data-binary '{"first":"Alex", "last": "Ellis"}' \
> -H "Content-Type: application/json"
MongoError: there are no users authenticated
Here is my stack.yml.
# cat stack.yml
provider:
name: faas
gateway: http://127.0.0.1:31112
functions:
insert-user:
lang: node8-express
handler: ./insert-user
image: alexellis2/insert-user
environment:
write_timeout: 10s
read_timeout: 10s
mongo: openfaas-db-mongodb
Steps to Reproduce.
# helm install stable/mongodb --name openfaas-db \
> --namespace openfaas-fn \
> --set persistence.enabled=false
NAME: openfaas-db
LAST DEPLOYED: Wed Jun 13 23:39:58 2018
NAMESPACE: openfaas-fn
STATUS: DEPLOYED
RESOURCES:
==> v1beta1/Deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
openfaas-db-mongodb 1 1 1 0 1s
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
openfaas-db-mongodb-547fd5b696-mpl6n 0/1 ContainerCreating 0 1s
==> v1/Secret
NAME TYPE DATA AGE
openfaas-db-mongodb Opaque 1 1s
==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
openfaas-db-mongodb ClusterIP 10.102.106.157 <none> 27017/TCP 1s
NOTES:
** Please be patient while the chart is being deployed **
MongoDB can be accessed via port 27017 on the following DNS name from within your cluster:
openfaas-db-mongodb.openfaas-fn.svc.cluster.local
To get the root password run:
export MONGODB_ROOT_PASSWORD=$(kubectl get secret --namespace openfaas-fn openfaas-db-mongodb -o jsonpath="{.data.mongodb-root-password}" | base64 --decode)
To connect to your database run the following command:
kubectl run openfaas-db-mongodb-client --rm --tty -i --image bitnami/mongodb --command -- mongo --host openfaas-db-mongodb -p $MONGODB_ROOT_PASSWORD
To connect to your database from outside the cluster execute the following commands:
export POD_NAME=$(kubectl get pods --namespace openfaas-fn -l "app=mongodb" -o jsonpath="{.items[0].metadata.name}")
kubectl port-forward --namespace openfaas-fn $POD_NAME 27017:27017 &
mongo --host 127.0.0.1 -p $MONGODB_ROOT_PASSWORD
[root@xxx mongodb-function]# export MONGODB_ROOT_PASSWORD=$(kubectl get secret --namespace openfaas-fn openfaas-db-mongodb -o jsonpath="{.data.mongodb-root-password}" | base64 --decode)
[root@xxx mongodb-function]# kubectl run openfaas-db-mongodb-client-1 --rm --tty -i --image bitnami/mongodb --command -- mongo --host openfaas-db-mongodb.openfaas-fn -p $MONGODB_ROOT_PASSWORD
If you don't see a command prompt, try pressing enter.
> use clients
switched to db clients
> show collections
2018-06-14T03:55:58.061+0000 E QUERY [thread1] Error: listCollections failed: {
"ok" : 0,
"errmsg" : "not authorized on clients to execute command { listCollections: 1.0, filter: {}, $db: \"clients\" }",
"code" : 13,
"codeName" : "Unauthorized"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:941:1
DB.prototype.getCollectionInfos@src/mongo/shell/db.js:953:19
DB.prototype.getCollectionNames@src/mongo/shell/db.js:964:16
shellHelper.show@src/mongo/shell/utils.js:842:9
shellHelper@src/mongo/shell/utils.js:739:15
@(shellhelp2):1:1
> db.users.find()
Error: error: {
"ok" : 0,
"errmsg" : "not authorized on clients to execute command { find: \"users\", filter: {}, $db: \"clients\" }",
"code" : 13,
"codeName" : "Unauthorized"
}
We tried porting this to python (below), but it seems that the module is reloaded every time, as the cache is always None
. Is this functionality available only in node? Do you know of a workaround for python?
Thanks!
import os
import pymongo
import json
mongo_client = None
def handle(req):
"""
create a record,
get the database, get the users collection from the database,
and insert the record into the collection.
return a result, setting status appropriately
"""
record = json.loads(req)
db = prepareDB()
users = db.users
id = users.insert_one(record).inserted_id
return {
'status': 'Inserted record with id {}: {}'.format(
id, record)
}
def prepareDB():
global mongo_client
if mongo_client is None:
url = "mongodb://" + os.environ["mongo"] + ":27017"
mongo_client = pymongo.MongoClient(url)
return mongo_client.clients
is there an example of golang with databses pool of mysql or postgresql ... ?
Following instruction in README but I got MongoError: command insert requires authentication
.
I tried to install mogodb with username/password following stable/mongodb chart documentation.
$ helm install --name openfaas-db \
--namespace openfaas-fn \
--set persistence.enabled=false,mongodbRootPassword=secretpassword,mongodbUsername=my-user,mongodbPassword=my-password,mongodbDatabase=clients \
stable/mongodb
Change two lines of handle.js
to:
const mongoURL = `mongodb://${username}:${password}@${url} + ":27017/clients`
....
MongoClient.connect(mongoURL,{useNewUrlParser: true, useUnifiedTopology: true} ,...
Change stack.yml
:
url: openfaas-db-mongodb.openfaas-fn.svc.cluster.local
username: my-user
password: my-password
Hi there, I meet some problem that access to the mongodb service
2018/07/17 17:53:14 stderr: (node:7) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.