Using authentication with DoctrineMongoDBBundle
authentication, mongodb, symfony
Solution
Instead of:
server: mongodb://localhost:27017
The following should do the trick:
server: mongodb://localhost:27017/louisv
Apparently, the Doctrine ODM bundle also has a "db" option now, so you now have the following options:
connections:
default:
server: mongodb://louisv:password@localhost:27017/louisv
or
connections:
default:
server: mongodb://localhost:27017
options:
username: louisv
password: password
db: louisv
The first option is preferred, as it allows for automatic re-authentication in case a connection drops.
See also:
- https://github.com/doctrine/DoctrineMongoDBBundle/issues/154
Problem
I'm currently working on a project where I use mongodb as database, and I began with no security in my mongo configuration of the symfony config.yml. This is what my config.yml looked like since the beginning of the project. ``` doctrine_mongodb: connections: default: server: mongodb://localhost:27017 options: {} default_database: louisv document_managers: default: auto_mapping: true ``` With this parameters the project works very well. But I want to add security to mongodb database. So I tried to follow documentation on mongodb to create users. I created an "admin" user for the admin database, and a "louisv" user for the louisv database (the admin database is the database existing by default, and louisv is mine). Now I want to Symfony to use this new user. I changed my the option parameters from my conf.yml file and now this is what I have : ``` doctrine_mongodb: connections: default: server: mongodb://localhost:27017 options: username: louisv password: password default_database: louisv document_managers: default: auto_mapping: true ``` Now when I try to enter my application in Symfony Failed to connect to: localhost:27017: Authentication failed on database 'admin' with username 'louisv': auth fails Which is weird cause my default_database is "louisv' and not 'admin'. And if I change the credentials to admin's ones, I have another error: localhost:27017: not authorized for query on louisv.User Which seems quite good as it seems to search for users, but I can't do queries.