Skip to content

Authentication Failure on database #184

Description

@dkman123

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

I'm trying to get this going for the first time, so I might be doing something wrong.

The init-mango.js script looks like it's missing parenthesis.
if which mongosh > /dev/null 2>&1; then
I believe should be
if (which mongosh > /dev/null 2>&1;) then

I was getting a lot of Authentication Failed on unifi-network-application and unifi-db so I interactive shelled into unifi-db and ran the script manually.

After that I would get "User "unifi@admin" already exists" when running it again, so it was creating the user, but I would still get Authentication Failed.

While entering this ticket I found bug #172

With the base script I got
Could not find role: clusterMonitor@unifi
So I commented that line (and noticed that is was not in rkone's script
(I also noticed that their script doesn't have the parenthesis, but mine wanted them)

With the user created in unifi rather than admin the Authentication Errors went away.

Expected Behavior

The containers should create what they need and start up

Steps To Reproduce

Docker compose on Linux
===start on dc.yml snippet

unifi db

unifi-db:
image: mongo:7.0
container_name: unifi-db
networks:
# - unifi-tier
proxy:
ipv4_address: 192.168.99.226
environment:
MONGO_INITDB_ROOT_USERNAME: $UNIFI_ROOT_USER
MONGO_INITDB_ROOT_PASSWORD: $UNIFI_ROOT_PASSWORD
MONGO_DBNAME: $UNIFI_DB
MONGO_USER: $UNIFI_DB_USER
MONGO_PASS: $UNIFI_DB_PASSWORD
MONGO_AUTHSOURCE: 'admin'
volumes:
- $DOCKERDIR/data/unifi/db:/data/db
- $DOCKERDIR/data/unifi/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
restart: unless-stopped

unifi controller

unifi:
image: lscr.io/linuxserver/unifi-network-application:latest
#container_name: unifi-network-application
container_name: unifi
depends_on:
- unifi-db
networks:
# - unifi-tier
proxy:
ipv4_address: 192.168.99.225
environment:
PID: $PUID
GID: $PGID
TZ: $TZ
MONGO_DBNAME: $UNIFI_DB
MONGO_USER: $UNIFI_DB_USER
MONGO_PASS: $UNIFI_DB_PASSWORD
MONGO_AUTHSOURCE: 'admin'
MONGO_HOST: 'unifi-db'
MONGO_PORT: 27017
#MEM_LIMIT: 1024 #optional
#MEM_STARTUP: 1024 #optional
#MONGO_TLS: #something? #optional
ports:
- "8443:8443" # Web GUI / HTTPS
- "8080:8080" # Device inform port
- "3478:3478/udp" # STUN port
- "10001:10001/udp" # Device discovery
- "8843:8843" # HTTPS portal redirection (optional)
- "6789:6789" # Speed test (optional)
volumes:
- $DOCKERDIR/appdata/unifi/config:/config
restart: unless-stopped
===end

variables pulling from a .env file

===start of init-mongo.js
#!/bin/bash

// parenthesis added because my docker base complained
if (which mongosh > /dev/null 2>&1;) then
mongo_init_bin='mongosh'
else
mongo_init_bin='mongo'
fi
"${mongo_init_bin}" <<EOF
use ${MONGO_AUTHSOURCE}
db.auth("${MONGO_INITDB_ROOT_USERNAME}", "${MONGO_INITDB_ROOT_PASSWORD}")
// added per rkone's script
use ${MONGO_DBNAME}
db.createUser({
user: "${MONGO_USER}",
pwd: "${MONGO_PASS}",
roles: [
// clusterMonitor commented because my container said it wan't found. and it wasn't in rkone's script
// "clusterMonitor",
{ db: "${MONGO_DBNAME}", role: "dbOwner" },
{ db: "${MONGO_DBNAME}_stat", role: "dbOwner" },
{ db: "${MONGO_DBNAME}_audit", role: "dbOwner" },
{ db: "${MONGO_DBNAME}_restore", role: "dbOwner" }
]
})
EOF
===end

clear the db so it has to start fresh (commented so someone doesn't run it accidentally)

rm -R data/unifi/db

remake the directory, so it exists

mkdir data/unifi/db

start the containers

sudo docker compose -f dc.yml up -d

check the log

docker compose -f dc.yml logs -tf --tail="60" unifi-db

With the script above it should be fine. with the main one on the site you'll get the Authentication errors.
To see what's going on with the script:
sudo docker exec -it unifi-db bash
cd /docker-entrypoint-initdb.d/

note, for this to execute you need to "chmod +x init-mongo.db" on the host, outside of the container

./init-mongo.js

Environment

- OS:Linux (Kubuntu 24.04)
- How docker service was installed:locally

CPU architecture

x86-64

Docker creation

see Steps to Reproduce

Container logs

I don't have the failed ones any more, but while I was trying to debug I copied bits into a document.

unifi-network-application log:
unifi  | 2026-06-18T22:58:12.130588510Z Caused by: com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server unifi-db:27017. The full response is {"ok": 0.0, "errmsg": "Authentication failed.", "code": 18, "codeName": "AuthenticationFailed"}
unifi  | 2026-06-18T22:58:12.130601455Z         at com.mongo

unifi-db log:
...9141266Z {"t":{"$date":"2026-06-18T22:59:05.369+00:00"},"s":"I",  "c":"ACCESS",   "id":20251,   "ctx":"conn252","msg":"Supported SASL mechanisms requested for unknown user","attr":{"user":{"user":"unifi","db":"unifi"}}}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status
    Issues

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions