Symfony – Doctrine (with Symfony2) only tries connection to DB using root@localhost

authenticationdoctrine-ormlocalhostrootsymfony

The error:(occurring in the prod env)

request.CRITICAL: PDOException: SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: YES) (uncaught exception) at /srv/inta/current/vendor/doctrine-dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php line 36 [] []

What I've tried so far

The weird thing is that I actually have access using the root user, and the provided password. Logging in as root via the console works great.

I'm using the following parameters.yml file located in app/config/

parameters:
    database_driver:   pdo_mysql
    database_host:     localhost
    database_port:     ~
    database_name:     int_apartments
    database_user:     root
    database_password: pw goes here

    mailer_transport:  smtp
    mailer_host:       localhost
    mailer_user:       ~
    mailer_password:   ~

    locale:            en
    secret:            ThisTokenIsNotSoSecretChangeIt

As you can see, it is quite standard with only the name of the db, user and password changed.

In my config.yml located in app/config (the relevant portions)

imports:
- { resource: security.yml }
- { resource: parameters.yml }

...

doctrine:
   dbal:
      driver:   %database_driver%
      host:     %database_host%
      port:     %database_port%
      dbname:   %database_name%
      user:     %database_user%
      password: %database_password%
      charset:  UTF8
      dbname:   int_apartments

  orm:
      auto_generate_proxy_classes: %kernel.debug%
      auto_mapping: true
      mappings:
          StofDoctrineExtensionsBundle: false

Now, I wanted to start at "step 1" and verify that the parameters.yml file is actually being imported, so I changed the host to "localhos" or the user to "tom" or whatever and the error message located in app/logs/prod.log stays exact as is – the location doesn't change and the user doesn't change.

So I checked my config_prod.yml located in app/config

imports:
- { resource: config.yml }


#doctrine:
#    metadata_cache_driver: apc
#    result_cache_driver: apc
#    query_cache_driver: apc

monolog:
    handlers:
        main:
            type:         fingers_crossed
            action_level: error
            handler:      nested
        nested:
            type:  stream
            path:  %kernel.logs_dir%/%kernel.environment%.log
            level: debug

…and everything seems standard!

Summary of what's going on

So here is the quick version.

  • Authentication error exists for root@localhost
  • Verified my authentication creditials by logging in as that user via the console
  • Want to check if the parameters.yml file is being loaded
  • Changed some values – none affected the error message

(small)Edit:
What I actually want to do is to connect to the DB as a completely different user with a different password. Even when I enter different credentials into my parameters.yml file, doctrine still spits out the "root@localhost" error.

Ideas?

Best Answer

Silly mistake, seems due to a bad user/group/owner configuration on the server.

the app/cache directory is owned by "root", but when I run app/console cache:clear --env=prod --no-debug I am running as another user (not root). So there were issues in clearing the cache and doctrine seems to have been using a very old configuration located in the cache files.

Lessons learned:

  • Always try running as root (as a last resort)
  • Use a properly configured web server to avoid ownership issues
Related Topic