2015-06-13 36 views
0

我在一个链接到运行MySQL服务器实例的另一个容器的docker容器中有一个rails应用程序(运行在生产模式下)。该应用程序本身作为“app”用户在nginx/passenger下运行。我已将生产数据库配置在rails下,如下所示:Docker容器中的Rails应用程序中的套接字错误连接到远程MySQL数据库

production: 
    adapter: mysql 
    database: <%= ENV['MYSQL_PRODUCTION_DATABASE'] %> 
    pool: 5 
    username: <%= ENV['MYSQL_USER'] %> 
    password: <%= ENV['MYSQL_PASS'] %> 
    host: <%= ENV['MYSQL_HOST'] %> 
    port: <%= ENV['MYSQL_PORT'] %> 

与容器运行时设置的环境变量。当连接到容器(以root身份运行)时,我可以成功执行与数据库相关的rake命令(迁移,重置等)。然而,当我尝试加载应用程序在浏览器中,我得到了以下错误:

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) 

activerecord (4.2.0) lib/active_record/connection_adapters/mysql_adapter.rb:456:in `real_connect' 
activerecord (4.2.0) lib/active_record/connection_adapters/mysql_adapter.rb:456:in `connect' 
activerecord (4.2.0) lib/active_record/connection_adapters/mysql_adapter.rb:108:in `initialize' 
activerecord (4.2.0) lib/active_record/connection_adapters/mysql_adapter.rb:36:in `new' 
activerecord (4.2.0) lib/active_record/connection_adapters/mysql_adapter.rb:36:in `mysql_connection' 

有谁知道什么可能导致MySQL适配器在运行时错误地对待在这种情况下连接的地方,但远程耙指令?

+0

请注意,我也试过这与postgres具有相同的行为:一切工作在根用耙命令,但实际的应用程序无法连接到远程服务器。 – Brocky

回答

0

这实际上是由设置为运行时参数的环境变量没有在rails进程中设置引起的。我的网络服务器(nginx)在运行之前清除所有环境变量,从而刷新由docker设置的环境变量。

相关问题