Saltar al contenido
Clodo

MongoDB 7: Authentication, Connection Strings, and mongodump Backups

Enable authentication on your MongoDB service, create an application user, build the right connection string, and set up regular backups.

Intermedio7 minActualizado:13 de septiembre de 2026
Verificado: MongoDB 7.0Shell mongosh 2.x

MongoDB's most dangerous default is that authentication can be off. An internet-facing MongoDB with no password is found by scanning bots within minutes; this has been one of the most common data-breach scenarios for years. So the first thing to do is create an admin user and require authentication.

Turning on authentication#

mongosh
// admin veritabanina gec
use admin

// Yonetici kullanicisi
db.createUser({
  user: "yonetici",
  pwd: passwordPrompt(),
  roles: [{ role: "root", db: "admin" }]
})

// Uygulama icin ayri kullanici: yalnizca kendi veritabaninda yetkili
use uygulama_db
db.createUser({
  user: "uygulama",
  pwd: passwordPrompt(),
  roles: [{ role: "readWrite", db: "uygulama_db" }]
})

Building the right connection string#

Bağlantı dizesi
# authSource, kullanicinin TANIMLANDIGI veritabanidir;
# baglanacaginiz veritabani ile ayni olmak zorunda degil.
mongodb://uygulama:[email protected]:27017/uygulama_db?authSource=uygulama_db

# Yonetici kullanicisiyla baglanirken:
mongodb://yonetici:[email protected]:27017/?authSource=admin

Backing up with mongodump#

Yerel terminal
mongodump \
  --uri="mongodb://uygulama:[email protected]:27017/uygulama_db?authSource=uygulama_db" \
  --gzip --archive=yedek_$(date +%F).gz

Solución de problemas

Síntoma

Authentication failed

Causa

authSource is missing, or the user was created in a different database.

Solución

Append ?authSource=<db where the user was created> to the connection string.

Síntoma

Server starts but I cannot connect remotely

Causa

bindIp is set to 127.0.0.1 only.

Solución

Set bindIp to 0.0.0.0 in the service configuration and make sure authentication is enabled.

Preguntas frecuentes

I get an Authentication failed error even though my password is correct, why?

A missing or incorrect authSource parameter in the connection string is the most common cause. You must set authSource to the name of the database where the user was actually created.

Why is using the admin user for applications not recommended?

The admin user holds root privileges across the entire instance. To prevent an exploit from affecting all your databases, create a dedicated user for your application with only readWrite permissions on its target database.

Can I restore a backup taken with mongodump into a different database name?

Yes. By passing the --nsFrom and --nsTo flags to mongorestore, you can remap collections to a new target database.

Can I connect to the MongoDB service using a GUI tool on my local machine?

Yes. You can connect remotely by entering the connection string shown in this guide into MongoDB Compass or similar clients.

¿Te ha resultado útil esta guía?

Los comentarios van directamente al equipo que mantiene esta guía.

¿No encontró una solución?

Puede abrir una solicitud al equipo de soporte desde el panel.

Abrir una solicitud