MongoDB Docker container refuses connection when auth enabled - mongodb

If I set up my container without auth eg)
docker run --name some-mongo -d -p 27017:27017 mongo
I can connect to my mongoDB from a remote machine eg)
mongo [serverIP]
However, my issue is when using the --auth flag
Following the documentation:
docker run --name some-mongo -d -p 27017:27017 mongo --auth
> db.createUser({ user: 'foo', pwd: 'bar', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })
results in connections being refused:
$ mongo -u foo -p bar [serverIP]
MongoDB shell version v3.4.9
connecting to: [serverIP]
2017-12-09T14:01:41.114-0600 W NETWORK [thread1] Failed to connect to [serverIP]:27017, in(checking socket for error after poll), reason: Connection refused
2017-12-09T14:01:41.115-0600 E QUERY [thread1] Error: couldn't connect to server [serverIP]:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:237:13
#(connect):1:6
exception: connect failed
I double-checked and the port looks to be open on the server:
$ netstat -tuplen
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name
...
tcp6 0 0 :::27017 :::* LISTEN 0 53238875 -
...
(Docker is running on a debian Digital Ocean droplet)

You need to specify the --authenticationDatabase flag
mongo --port 27017 -u "myUserAdmin" -p "abc123" --authenticationDatabase "admin"
see the tutorial

Related

Can't connect to MongoDB running in docker container

I run MongoDB in a docker container like this
docker run --name mongo -d -p 27017:27107 mongo
Check with docker ps shows
77f1a11295c3 mongo "docker-entrypoint.s…" 20 minutes ago Up 20 minutes 27017/tcp, 0.0.0.0:27017->27107/tcp mongo
so it's running with the port mapped correctly.
When I try to setup a connection via Intellij
it fails with
com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Exception receiving message}, caused by {java.net.SocketException: Connection reset}}].
When I try to connect to connect from a golang web server
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, err := mongo.Connect(context.TODO(), clientOptions)
I get this error and it shuts down the web server:
server selection error: server selection timeout, current topology: { Type: Unknown, Servers: [{ Addr: localhost:27017, Type: Unknown, Average RTT: 0, Last error: connection() error occured during connection handshake: connection(localhost:27017[-64]) incomplete read of message header: read tcp 127.0.0.1:40700->127.0.0.1:27017: read: connection reset by peer }, ] }
Is this a bug in the MongoDB docker image, or is there something else I need to do?
I think you have a typo mistake while creating a container. 27017 and 27107
docker run --name mongo -d -p 27017:27017 mongo
This is clearly visible in docker ps command.
Can you try and see if it solves the problem ?
As #fly2matrix said you have a typo, and I believe you have another issue:
you forward the ports 27017:27107, in which the container port is incorrect. Check 27107 instead of 27017
It seems you're connecting to localhost instead of mongo as I see the phrase localhost in your code.

Can't login to Wekan DB CentOS console

I have recently installed Wekan on a CentOS (CentOS Linux release 7.6.1810) server and it works well. However I need to login to the MongoDB. I have followed the installation guide of MongoDB and set a password for 'admin' user. But when When the "mongo -u admin -p" command is entered it prompts for password and I get the following error. However I have another issue not starting "mongod", but it is enabled.
connecting to: test
2020-06-12T01:20:23.618-0400 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: errno:111 Connection refused
2020-06-12T01:20:23.618-0400 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:229:14
#(connect):1:6
exception: connect failed
I created this user in mongo which does not seem to work when trying to login to the DB.
$ mongo
db.createUser(
{
user: "admin",
pwd: "abcd1234",
roles: [ { role: "root", db: "admin" } ]
}
)
Firstly I installed the latest version of mongodb and then I downgraded to to 3.2 which works with wekan. However Wekan still works fine without any issue.
This is the error I get when starting the mongod.
$ systemctl start mongod
Job for mongod.service failed because the control process exited with error code.
See "systemctl status mongod.service" and "journalctl -xe" for details.
$ systemctl enable mongod
mongod.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig mongod on
The port mongod uses is 27019 which I found from the following command.
$ ss -nutlp
tcp LISTEN 0 128 127.0.0.1:27019 *:* users:(("mongod",pid=11578,fd=6))
What am I doing wrong here?
In the output of the mongo shell connection failure:
Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: errno:111 Connection refused
This tells you that it is attempting to connect to port 27017 on localhost, and that the operating system actively refused the connection because nothing is listening on that port.
The ss output shows that mongod is listening on port 27019.
Try adding --port 27019 to your mongo command line.

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 -

Unable to start MongoDB shell for mongos daemon

I am trying to setup a sharded mongodb cluster using 4 solaris-11 servers which are on the same rack. The following sharded config is being attempted:
Server A : Replica set 1(rs1)
Server B : Replica Set 2(rs2)
Server C : Configuration Server
Server D : Sharding Router
#
On Server A, rs1 has been set up as follows:
mongod --port 27017 --replSet rs1 --dbpath "/mongo/datadb"
Now the rs1 configuration in mongo shell:
config =
{
_id:"rs1",
members:
[
{ _id:0, host:"Server A IP:27017" },
]
}
rs.initiate( config )
rs.conf()
rs.status()
On Server B , rs2 has been similarly configured.
#
On Server C, the config server has been configured as follows:
mongod --configsvr --port 37017 --dbpath "/rpool/mongo/datadb"
#
Now on Server D, I am running mongos using the following :
mongos --port 27017 --configdb rs1/'Server C IP':37017
When I am trying to open mongo Shell on server D to add the shards using the mongos port and connect to mongos, I am unable to do so. The following error is thrown :
**./mongo --port 27017 --host 'Server C '
MongoDB shell version v3.4.1
connecting to: mongodb://'Server C ip':27017/
2017-11-09T11:21:51.666+0000 W NETWORK [main] Failed to connect to :27017, in(checking socket for error after poll), reason: Connection refused
2017-11-09T11:21:51.686+0000 E QUERY [main] Error: couldn't connect to server 'Server C ip':27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:234:13
#(connect):1:6
exception: connect failed**
When I tried to debug, I found the port 27017 on Server C to be in 'BOUND' state:
netstat -an |grep 27017
*.27017 . 0 0 128000 0 BOUND
'Server ip' stream-ord server ip' 0000000000000000 /tmp/mongodb-27017.sock
Shouldn't mongos occupy this port in Listening mode? Can someone help me in solving this?
Is there any issue with the configuration?

Mongo Shell only connects with prepended host

I have a new mongodb setup.
my running mongo process is this:
mtp 26776 0.0 0.0 4104 464 ? S Apr14 0:41 /command/svscan /home/mtp/service
mtp 26779 0.0 0.0 3932 392 ? S Apr14 0:01 \_ supervise mongodb
mtp 14413 0.5 0.2 337744 53660 ? Sl 14:57 0:04 | \_ mongod --dbpath /home/mtp/mongodb --bind_ip 127.0.0.1 --port 21111 --auth --nohttpinterface --smallfiles
I connect to it by main admin:
mongo admin --port 21111 -u mtp_mongoadmin -p
I create a db
$use errbit-production
> switched to db errbit-production
I create a user on that db which can read and write:
db.createUser({user: "mtp-production", pwd: "blah", roles:["readWrite"]})
But I cannot connect
$ mongo --port 2111 --host localhost -u mtp-production -p
MongoDB shell version: 2.6.12
Enter password:
connecting to: localhost:2111/test
2016-04-18T15:14:44.607+0200 warning: Failed to connect to 127.0.0.1:2111, reason: errno:111 Connection refused
2016-04-18T15:14:44.608+0200 Error: couldn't connect to server localhost:2111 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:148
exception: connect failed
The only way I can connect is by this
$ mongo localhost:2111/errbit-production -u mtp-production -p
MongoDB shell version: 2.6.12
Enter password:
connecting to: localhost:2111/errbit-production
>
Why is this? And how can I solve this?