Error: Authentication failed in mongodb 4.4.4 running in docker - mongodb

I used to run mongodb in docker with this config
~/project/.env file is
# Mongodb common config
CONFIG_MONGODB_PORT=27020
CONFIG_MONGODB_PREFIX=mongodb://
CONFIG_MONGODB_AUTH_ENABLED=true
CONFIG_MONGODB_USERNAME=alok
CONFIG_MONGODB_PASSWORD=alokm
CONFIG_MONGODB_AUTH_SOURCE=admin
~/project/docker-compose.yml file is
version: '3.4'
mongodb:
image: mongo:4.0.3
restart: unless-stopped
command: mongod --port ${CONFIG_MONGODB_PORT}
volumes:
- ${HOST_MONGODB_DATA}:/data/db
ports:
- 5010:${CONFIG_MONGODB_PORT}
expose:
- ${CONFIG_MONGODB_PORT}
environment:
MONGO_INITDB_ROOT_USERNAME: ${CONFIG_MONGODB_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${CONFIG_MONGODB_PASSWORD}
To run mongodb
sudo docker-compose up --build
To access mongodb from my laptop
mongo localhost:5010 --password alokm --username alok --authenticationDatabase admin
This all setup used to work fine without any extra setup.
But when I changed mongodb image from mongo:4.0.3 to mongo:4.4.4 and run freshly on another laptop I see
ubuntu#myhost:~/project$ mongo localhost:5010 --password alokm --username alok --authenticationDatabase admin
MongoDB shell version v3.6.8
connecting to: mongodb://localhost:5010/test
Implicit session: session { "id" : UUID("4282986e-eb0b-4a88-8d39-1295192a66e7") }
MongoDB server version: 4.4.4
WARNING: shell and server versions do not match
2021-04-16T01:41:18.225+0000 E QUERY [thread1] Error: Authentication failed. :
DB.prototype._authOrThrow#src/mongo/shell/db.js:1608:20
#(auth):6:1
#(auth):1:2
exception: login failed
Why my this setup is not working in case of image: mongo:4.4.4 like it was working in case of image: mongo:4.0.3?

Related

can't connect to mongo docker within gitlab

I have the following docker image built by the next docker-compose
services:
give_sync_test:
image: mongo:4.2.10
restart: always
environment:
MONGO_INITDB_DATABASE: mybase
MONGO_INITDB_ROOT_PASSWORD: admin
MONGO_INITDB_DATABASE: password
ports:
- 27017:27017
which i am using through the following gitlab step :
test:
stage: test
dependencies:
- maven_build_snapshot
image: 'my_image:latest'
before_script:
- apt-get update
- apt-get -y install git
- mongo "mongodb://localhost:27017/mybase" < ./utils/test
the content of utils/test is simply :
use myBase;
no action in this, the real problem is that i'm getting the following error from gitlab-ci :
connecting to:
mongodb://localhost:27017/give?compressors=disabled&gssapiServiceName=mongodb
2020-12-09T14:44:21.828+0000 E QUERY [js] Error: couldn't connect
to server localhost:27017, connection attempt failed: SocketException:
Error connecting to localhost:27017 (127.0.0.1:27017) :: caused by ::
Connection refused : connect#src/mongo/shell/mongo.js:353:17
#(connect):2:6 2020-12-09T14:44:21.830+0000 F - [main]
exception: connect failed 2020-12-09T14:44:21.830+0000 E -
[main] exiting with code 1
I've already try the mongo string with some authentication as well
mongodb://admin:password#localhost:27017/mybase
Does anyone know what i'm doing wrong ?
You should use alias of the services to connect to the containers. In this case, you can access mongo by using this connection string mongodb://admin:password#give_sync_test:27017/mybase

Gitlab ci, symfony + postgres Error Connection refused

I have an error when I create the schema with doctine
$ php bin/console doctrine:schema:create --env=test
In AbstractPostgreSQLDriver.php line 73:
An exception occurred in driver: SQLSTATE[08006] [7] could not connect to s
erver: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
with my gitlab-ci.yml
database:
stage: building
services:
- postgres:10
variables:
POSTGRES_DB: back
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ''
POSTGRES_HOST: postgres
POSTGRES_PORT: 5432
before_script:
# Install pdo_pgsql driver
- apt-get update
- apt-get install -y libpq-dev
- docker-php-ext-install pdo_pgsql
script:
- php bin/console doctrine:schema:create --env=test
- php -d memory_limit=2048M bin/console --env=test cache:warmup --no-debug
And my gitlab-parameters.yml (who is cp on app/config/parameters.yml)
parameters:
database_host: postgres
database_port: 5432
database_name: back
database_user: postgres
database_password: ''
Other information :
when I try to create database, I have the good message from gitlab-ci
$ php bin/console doctrine:database:create --env=test
Could not create database "back" for connection named default
An exception occurred while executing 'CREATE DATABASE "back"':
SQLSTATE[42P04]: Duplicate database: 7 ERROR: database "back" already exists
Version :
php 7.2.19
smyfony 3.4
and my config_text.yml
imports:
- { resource: config_dev.yml }
framework:
test: ~
session:
storage_id: session.storage.mock_file
profiler:
collect: false
web_profiler:
toolbar: false
intercept_redirects: false
swiftmailer:
disable_delivery: true
How fix it?
EDIT :
postgres is not running :
netstat -plntu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN -

Run command after container start up

I'm trying to execute command after docker container start.
Here is my Dockerfile
FROM mysite:myport/mongo:3.6
EXPOSE 27017
ADD mongo.js /data/mongodb/scripts/
here is my docker-compose.yml
mongo:
image: test-mongo
container_name: test-mongo
hostname: mongo
expose:
- "27017"
ports:
- "27017:27017"
command: mongo --eval load("/data/mongodb/scripts/mongo.js")
docker-compose --no-ansi up returns me
Creating test-mongo ...
Creating test-mongo ... done
Attaching to test-mongo
test-mongo | MongoDB shell version v3.6.5
test-mongo | connecting to: mongodb://127.0.0.1:27017
test-mongo | 2018-06-09T15:46:50.229+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), rea
son: Connection refused
test-mongo | 2018-06-09T15:46:50.229+0000 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
test-mongo | connect#src/mongo/shell/mongo.js:251:13
test-mongo | #(connect):1:6
test-mongo | exception: connect failed
test-mongo exited with code 1
I don't uderstand how can I load my file after mongodb is started
By passing a command in your docker-compose.yaml, you are overriding the normal behavior of the mongo image -- so the mongo server is never starting. The way you would typically handle this is to put your script into a second image, and then start a second container as part of your docker-compose.yaml that makes a network connection to the mongo server. Something like:
version: "3"
services:
mongo:
image: mongo
client:
image: my-mongo-client
restart: on-failure
command: mongo --host mongo --eval load("/data/mongodb/scripts/mongo.js")
The restart directive will cause the client container to restart
automatically on failure, which is necessary because it may try to
connect before the mongo server is available to handle connections.
If all you're trying to do is initialize a new database, such as creating a new schema and adding a user, you can create an initialization script and load it into the container. From the Docker Hub documentation:
When a container is started for the first time it will execute files with extensions .sh and .js that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. .js files will be executed by mongo using the database specified by the MONGO_INITDB_DATABASE variable, if it is present, or test otherwise. You may also switch databases within the .js script.

Unable to set up default credentials for official Mongo docker image

I am using docker-compose to launch an official mongodb database container.
Reading the scripts and some StackOverflow questions looks like MONGO_INITDB_ environment variables can be used to set default credentials.
After running the following docker-compose.yml:
version: '2'
services:
mongodb:
image: mongo:3.6
environment:
- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=password
- MONGO_INITDB_DATABASE=collection
ports:
- 27017:27017
And trying to connect with mongo cli or mongoose Node.js ODM I get authentication error:
➔ mongo -u user -p password --verbose localhost:27017/collection
MongoDB shell version: 2.6.10
connecting to: localhost:27017/collection
2018-04-10T12:29:46.386+0200 creating new connection to:localhost:27017
2018-04-10T12:29:46.386+0200 [ConnectBG] BackgroundJob starting: ConnectBG
2018-04-10T12:29:46.387+0200 connected to server localhost:27017 (127.0.0.1)
2018-04-10T12:29:46.387+0200 connected connection!
2018-04-10T12:29:46.391+0200 User Assertion: 18:{ ok: 0.0, errmsg: "auth failed", code: 18, codeName: "AuthenticationFailed" }
2018-04-10T12:29:46.392+0200 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18, codeName: "AuthenticationFailed" } at src/mongo/shell/db.js:1287
2018-04-10T12:29:46.392+0200 User Assertion: 12514:login failed
2018-04-10T12:29:46.393+0200 freeing 1 uncollected N5mongo12DBClientBaseE objects
exception: login failed
Connecting to /admin collection as defined in the entrypoint script [1] gave the same result. I am unable to see what am I doing wrong.
EDIT: Derick, from irc.freenode.org #mongodb channel, suggested that it may be not picking the --auth flag. I tried to log in without credentials (mongo --verbose localhost:27017/admin) and it worked. I am still having the issue of establishing defaults credentials, so issue is not yet solved.
[1] https://github.com/docker-library/mongo/blob/b96fddd1e1a100c01f0ea6d28e1c7ccc750fd5c0/3.6/docker-entrypoint.sh#L292

how do I create a collection in mongoDB using an Ansible playbook?

I'm using Ansible to configure a Vagrant VM that, on vagrant up, starts mongoDB and creates a database and collection ready to receive data. Another process will write data to the mongoDB collection. Here's a snippet from the playbook:
- hosts: all
user: vagrant
sudo: True
tasks:
- name: install packages
action: apt update_cache=yes pkg={{item}} state=installed
with_items:
- mongodb
- service: name=mongodb state=started
- shell: mongo mydb --eval "db.createCollection('mycollection')"
The playbook installs and starts the mongoDB service. However, it doesn't create the database/collection. There's something wrong with the "- shell: mongo ..." line. It returns:
TASK: [shell mongo mydb --eval "db.createCollection('mycollection')"] ****
failed: [default] => {"changed": true, "cmd": "mongo mydb --eval\"db.createCollection('mycollection')\" ", "delta": "0:00:00.084196", "end": "2014-08-29 04:48:40.333460", "rc": 255, "start": "2014-08-29 04:48:40.249264"}
stderr: exception: connect failed
stdout: MongoDB shell version: 2.0.4
connecting to: mydb
Fri Aug 29 04:48:40 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
Can you see what I did wrong?
Your approach should be correct. I just tested your mongo line on a vagrant booted instance with mongo installed and it worked fine.
Can you check the mongo logs to see what it is complaining about? If you can't connect, it could be that the mongo service is binding to ipv6 addr, binding to wrong port (should be 27017), or their are some authentication issues.
After checking that, can you boot the machine, get on the shell, then execute this bit:
mongo mydb --eval "db.createCollection('mycollection')"
Does it work from the shell? (run as same user as Ansible).