无法识别通过 Symfony2+nginx ENV 变量传递的学说凭据;数据库 'connection refused'。 cmd 行没问题。怎么修?
doctrine credentials passed via Symfony2+nginx ENV vars aren't recognized; DB 'connection refused'. cmd line is ok. how to fix?
在新的 symfony2 安装中,我通过 systemd 启动 Nginx
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
定义 ENV 变量的地方
cat /etc/sysconfig/nginx
SYMFONY__DATABASE__USER="root"
SYMFONY__DATABASE__PASSWORD="testtesttest"
用过
cat /etc/nginx/nginx.conf
...
env SYMFONY__DATABASE__USER;
env SYMFONY__DATABASE__PASSWORD;
...
http {
...
重启后验证
php -i | grep SYMFONY__DATABASE
SYMFONY__DATABASE__PASSWORD => "testtesttest"
SYMFONY__DATABASE__USER => "root"
_SERVER["SYMFONY__DATABASE__PASSWORD"] => "testtesttest"
_SERVER["SYMFONY__DATABASE__USER"] => "root"
根据“How to Set external Parameters in the Service Container”:
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.
因此,更改
中的凭据
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
使用默认学说配置
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%"
...
应该继承 ENV 定义的值
user <= "%database_user%" <= "%database.user%" <= SYMFONY__DATABASE__USER <= "root"
password <= "%database_password%" <= "%database.password%" <= SYMFONY__DATABASE__PASSWORD <= "testtesttest"
但是,从站点根目录,@DB create with doctrine,连接被拒绝
php app/console doctrine:database:create
Could not create database for connection named `test_01`
SQLSTATE[HY000] [2002] Connection refused
从shell开始没问题
mysqladmin -u root -p create test_01
Enter password:
=> "testtesttest"
mysqladmin -u root -p
...
+------------------------------+
| Databases |
+------------------------------+
...
| test_01 |
+------------------------------+
上面的配置中需要更改哪些内容才能使 doctrine exec 正确使用 ENV 提供的凭据?
在新的 symfony2 安装中,我通过 systemd 启动 Nginx
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
定义 ENV 变量的地方
cat /etc/sysconfig/nginx
SYMFONY__DATABASE__USER="root"
SYMFONY__DATABASE__PASSWORD="testtesttest"
用过
cat /etc/nginx/nginx.conf
...
env SYMFONY__DATABASE__USER;
env SYMFONY__DATABASE__PASSWORD;
...
http {
...
重启后验证
php -i | grep SYMFONY__DATABASE
SYMFONY__DATABASE__PASSWORD => "testtesttest"
SYMFONY__DATABASE__USER => "root"
_SERVER["SYMFONY__DATABASE__PASSWORD"] => "testtesttest"
_SERVER["SYMFONY__DATABASE__USER"] => "root"
根据“How to Set external Parameters in the Service Container”:
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 withSYMFONY__
as parameters in the service container.You can now reference these parameters wherever you need them.
因此,更改
中的凭据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
使用默认学说配置
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%"
...
应该继承 ENV 定义的值
user <= "%database_user%" <= "%database.user%" <= SYMFONY__DATABASE__USER <= "root"
password <= "%database_password%" <= "%database.password%" <= SYMFONY__DATABASE__PASSWORD <= "testtesttest"
但是,从站点根目录,@DB create with doctrine,连接被拒绝
php app/console doctrine:database:create
Could not create database for connection named `test_01`
SQLSTATE[HY000] [2002] Connection refused
从shell开始没问题
mysqladmin -u root -p create test_01
Enter password:
=> "testtesttest"
mysqladmin -u root -p
...
+------------------------------+
| Databases |
+------------------------------+
...
| test_01 |
+------------------------------+
上面的配置中需要更改哪些内容才能使 doctrine exec 正确使用 ENV 提供的凭据?