This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

doctrine does not see credentials passed via ENV vars

Discuss installing Symfony 2 here

doctrine does not see credentials passed via ENV vars

by hanl » Tue Apr 21, 2015 1:40 am

In a new symfony2 install, I launch Nginx via systemd with

Code: Select all
    cat /etc/systemd/system/nginx.service
        [Unit]
        Description=nginx
        After=syslog.target network.target

        [Service]
        Type=forking
        EnvironmentFile=-/etc/sysconfig/nginx
        ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
        ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
        ExecReload=/bin/kill -s HUP $MAINPID
        ExecStop=/bin/kill -s QUIT $MAINPID
        PrivateTmp=true

where ENV vars are defined

Code: Select all
    cat /etc/sysconfig/nginx
        SYMFONY__DATABASE__USER="root"
        SYMFONY__DATABASE__PASSWORD="testtesttest"

used

Code: Select all
    cat /etc/nginx/nginx.conf
        ...
        env SYMFONY__DATABASE__USER;
        env SYMFONY__DATABASE__PASSWORD;
        ...
        http {
            ...

and verified after restart

Code: Select all
    php -i | grep SYMFONY__DATABASE
        SYMFONY__DATABASE__PASSWORD => "testtesttest"
        SYMFONY__DATABASE__USER => "root"
        _SERVER["SYMFONY__DATABASE__PASSWORD"] => "testtesttest"
        _SERVER["SYMFONY__DATABASE__USER"] => "root"

According to

"How to Set external Parameters in the Service Container"
http://symfony.com/doc/current/cookbook/configuration/external_parameters.html

"Now that you have declared an environment variable, it will be present in the PHP $_SERVER global variable. Symfony then automatically sets all $_SERVER variables prefixed with SYMFONY__ as parameters in the service container.

You can now reference these parameters wherever you need them."

So, changing credentials in

Code: Select all
    cat ./app/config/parameters.yml
        parameters:
            database_driver:   pdo_mysql
            database_host:     127.0.0.1
            database_port:     3306
            database_name:     "test_01"
            database_user:     "%database.user%"
            database_password: "%database.password%"
            mailer_transport:  smtp
            mailer_host:       ~
            mailer_user:       ~
            mailer_password:   ~
            locale:            en
            secret:            zzzz

with the default doctrine config

Code: Select all
    cat ./app/config/config.yml
        ...
        # Doctrine Configuration
        doctrine:
            dbal:
                driver:   "%database_driver%"
                host:     "%database_host%"
                port:     "%database_port%"
                dbname:   "%database_name%"
                user:     "%database_user%"
                password: "%database_password%"
        ...

should inherit the ENV-defined values

Code: Select all
    user <= "%database_user%" <= "%database.user%" <= SYMFONY__DATABASE__USER <= "root"
    password <= "%database_password%" <= "%database.password%" <= SYMFONY__DATABASE__PASSWORD <= "testtesttest"


but, from site root, @ DB create with doctrine, connection is refused

Code: Select all
    php app/console doctrine:database:create
        Could not create database for connection named `test_01`
        SQLSTATE[HY000] [2002] Connection refused

From shell, there's no problem

Code: Select all
   mysqladmin -u root -p create test_01
    Enter password:
    => "testtesttest"

    mysqladmin -u root -p
        ...
        +------------------------------+
        |          Databases           |
        +------------------------------+
        ...
        | test_01                      |
        +------------------------------+

What needs to changed in the config above so that the doctrine execs correctly use the ENV-provided credentials?
hanl
Junior Member
 
Posts: 1
Joined: Tue Apr 21, 2015 1:08 am