MySql 在 Passenger + Nginx 中部署时 Rails 出错

MySql Error in Rails while deployed in Passenger + Nginx

我有一个带有 MySql 数据库引擎的 rails 应用程序。我能够 运行 使用

在服务器 (173.xxx.xxx.xxx) 上本地开发模式下的应用程序
     cd /var/www/demo


     rails s -b 0.0.0.0 -p3001

服务器已启动

        => Booting Puma
        => Rails 5.0.7 application starting in development on http://0.0.0.0:3001
        => Run `rails server -h` for more startup options
        Puma starting in single mode...
        * Version 3.11.4 (ruby 2.5.1-p57), codename: Love Song
        * Min threads: 5, max threads: 5
        * Environment: development
        * Listening on tcp://0.0.0.0:3001
        Use Ctrl-C to stop

我在 Gemfile 中有 gem 'mysql2', '~> 0.5.1'。 捆绑包安装成功。

database.yml

            development:
              adapter: mysql2
              host: localhost
              encoding: utf8
              reconnect: false
              database: demo_development
              pool: 5
              username: root
              password: password
              socket: /var/run/mysqld/mysqld.sock




            test:
              adapter: mysql2
              encoding: utf8
              reconnect: false
              database: demo_development
              pool: 5
              username: root
              password: password


            production:
              adapter: mysql2
              encoding: utf8
              reconnect: false
              database: demo_development
              pool: 5
              username: root
              password: password

服务器本地一切正常(我可以通过

从浏览器访问应用程序

http://173.xxx.xxx.xxx:3001/

)。该应用程序通过 IP 运行良好。 Mysql 用户名和密码 正确(我用 mysql -u root -p 检查过)。我能够 成功登录到 Mysql 提示没问题。

现在我想在 Passenger + Nginx 上部署 rails 应用程序。

mysite.conf

server {
            listen 80;
            server_name www.mysite.com mysite.com;
            # Tell Nginx and Passenger where your app's 'public' directory is
            root /var/www/demo/public;
            # Turn on Passenger
            passenger_enabled on;
            rails_env development;
            passenger_ruby /usr/local/rvm/gems/ruby-2.5.1/wrappers/ruby;


        }

我重启了nginx

   sudo service nginx restart

我遇到了这个错误

nginx error.log

                        App 8312 output: Started GET "/" for 49.206.150.241 at 2018-05-23 16:03:48 +0200
            App 8312 output: Cannot render console from 49.206.150.241! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
            App 8312 output:
            App 8312 output: Mysql2::Error (Access denied for user 'root'@'localhost'):
            App 8312 output:
            App 8312 output: mysql2 (0.5.1) lib/mysql2/client.rb:90:in `connect'
            App 8312 output: mysql2 (0.5.1) lib/mysql2/client.rb:90:in `initialize'
            App 8312 output: activerecord (5.0.7) lib/active_record/connection_adapters/mysql2_adapter.rb:25:in `new'
            App 8312 output: activerecord (5.0.7) lib/active_record/connection_adapters/mysql2_adapter.rb:25:in `mysql2_connection'
            App 8312 output: activerecord (5.0.7) lib/active_record/connection_adapters/abstract/connection_pool.rb:729:in `new_connection'
            App 8312 output: activerecord (5.0.7) lib/active_record/connection_adapters/abstract/connection_pool.rb:773:in `checkout_new_connection'
            App 8312 output: activerecord (5.0.7) lib/active_record/connection_adapters/abstract/connection_pool.rb:752:in `try_to_checkout_new_connection'
            App 8312 output: activerecord (5.0.7) lib/active_record/connection_adapters/abstract/connection_pool.rb:713:in `acquire_connection'
            App 8312 output: activerecord (5.0.7) lib/active_record/connection_adapters/abstract/connection_pool.rb:490:in `checkout'
            App 8312 output: activerecord (5.0.7) lib/active_record/connection_adapters/abstract/connection_pool.rb:364:in `connection'
            App 8312 output: activerecord (5.0.7) lib/active_record/connection_adapters/abstract/connection_pool.rb:883:in `retrieve_connection'
            App 8312 output: activerecord (5.0.7) lib/active_record/connection_handling.rb:128:in `retrieve_connection'
            App 8312 output: activerecord (5.0.7) lib/active_record/connection_handling.rb:91:in `connection'
            App 8312 output: activerecord (5.0.7) lib/active_record/migration.rb:559:in `connection'
            App 8312 output: activerecord (5.0.7) lib/active_record/migration.rb:546:in `call'
            App 8312 output: actionpack (5.0.7) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
            App 8312 output: activesupport (5.0.7) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
            App 8312 output: activesupport (5.0.7) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
            App 8312 output: activesupport (5.0.7) lib/active_support/callbacks.rb:90:in `run_callbacks'
            App 8312 output: actionpack (5.0.7) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
            App 8312 output: actionpack (5.0.7) lib/action_dispatch/middleware/executor.rb:12:in `call'
            App 8312 output: actionpack (5.0.7) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
            App 8312 output: actionpack (5.0.7) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
            App 8312 output: web-console (3.6.2) lib/web_console/middleware.rb:135:in `call_app'
            App 8312 output: web-console (3.6.2) lib/web_console/middleware.rb:22:in `block in call'
            App 8312 output: web-console (3.6.2) lib/web_console/middleware.rb:20:in `catch'
            App 8312 output: web-console (3.6.2) lib/web_console/middleware.rb:20:in `call'
            App 8312 output: actionpack (5.0.7) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
            App 8312 output: railties (5.0.7) lib/rails/rack/logger.rb:36:in `call_app'
            App 8312 output: railties (5.0.7) lib/rails/rack/logger.rb:24:in `block in call'
            App 8312 output: activesupport (5.0.7) lib/active_support/tagged_logging.rb:69:in `block in tagged'
            App 8312 output: activesupport (5.0.7) lib/active_support/tagged_logging.rb:26:in `tagged'
            App 8312 output: activesupport (5.0.7) lib/active_support/tagged_logging.rb:69:in `tagged'
            App 8312 output: railties (5.0.7) lib/rails/rack/logger.rb:24:in `call'
            App 8312 output: sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
            App 8312 output: actionpack (5.0.7) lib/action_dispatch/middleware/request_id.rb:24:in `call'
            App 8312 output: rack (2.0.5) lib/rack/method_override.rb:22:in `call'
            App 8312 output: rack (2.0.5) lib/rack/runtime.rb:22:in `call'
            App 8312 output: activesupport (5.0.7) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
            App 8312 output: actionpack (5.0.7) lib/action_dispatch/middleware/executor.rb:12:in `call'
            App 8312 output: actionpack (5.0.7) lib/action_dispatch/middleware/static.rb:136:in `call'
            App 8312 output: rack (2.0.5) lib/rack/sendfile.rb:111:in `call'
            App 8312 output: railties (5.0.7) lib/rails/engine.rb:522:in `call'
            App 8312 output: /usr/lib/ruby/vendor_ruby/phusion_passenger/rack/thread_handler_extension.rb:97:in `process_request'
            App 8312 output: /usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:149:in `accept_and_process_next_request'
            App 8312 output: /usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:110:in `main_loop'
            App 8312 output: /usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler.rb:415:in `block (3 levels) in start_threads'
            App 8312 output: /usr/lib/ruby/vendor_ruby/phusion_passenger/utils.rb:113:in `block in create_thread_and_abort_on_exception'
            App 8312 output:   Rendering /usr/local/rvm/gems/ruby-2.5.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
            App 8312 output:   Rendering /usr/local/rvm/gems/ruby-2.5.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
            App 8312 output:   Rendered /usr/local/rvm/gems/ruby-2.5.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (2.1ms)
            App 8312 output:   Rendering /usr/local/rvm/gems/ruby-2.5.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
            App 8312 output:   Rendered /usr/local/rvm/gems/ruby-2.5.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.2ms)
            App 8312 output:   Rendering /usr/local/rvm/gems/ruby-2.5.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
            App 8312 output:   Rendered /usr/local/rvm/gems/ruby-2.5.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.7ms)
            App 8312 output:   Rendered /usr/local/rvm/gems/ruby-2.5.1/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (18.9ms)

但是非常奇怪的是,相同的代码库在本地 运行ning 很好(rails s -b 0.0.0.0 -p3001)没有 MySql 错误。

    sudo systemctl status nginx

结果

        ● nginx.service - A high performance web server and a reverse proxy server
           Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
           Active: active (running) since Wed 2018-05-23 15:58:17 CEST; 16min ago
             Docs: man:nginx(8)
          Process: 8204 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0
          Process: 30081 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS)
          Process: 8209 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
          Process: 8207 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
         Main PID: 8243 (nginx)
            Tasks: 52 (limit: 4915)
           CGroup: /system.slice/nginx.service
                   ├─8210 Passenger watchdog
                   ├─8213 Passenger core
                   ├─8243 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
                   ├─8245 nginx: worker process
                   ├─8246 nginx: worker process
                   ├─8247 nginx: worker process
                   ├─8248 nginx: worker process
                   ├─8250 nginx: worker process
                   ├─8251 nginx: worker process
                   ├─8252 nginx: worker process
                   ├─8253 nginx: worker process
                   ├─8254 nginx: worker process
                   ├─8255 nginx: worker process
                   └─8312 Passenger AppPreloader: /var/www/nizanto (forking...)

        May 23 15:58:16 vmi182971.contaboserver.net systemd[1]: Starting A high performance web server and a reverse proxy server...
        May 23 15:58:17 vmi182971.contaboserver.net systemd[1]: Started A high performance web server and a reverse proxy server.
        ● nginx.service - A high performance web server and a reverse proxy server
           Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
           Active: active (running) since Wed 2018-05-23 15:58:17 CEST; 16min ago
             Docs: man:nginx(8)
          Process: 8204 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0
          Process: 30081 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS)
          Process: 8209 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
          Process: 8207 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
         Main PID: 8243 (nginx)
            Tasks: 52 (limit: 4915)
           CGroup: /system.slice/nginx.service
                   ├─8210 Passenger watchdog
                   ├─8213 Passenger core
                   ├─8243 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
                   ├─8245 nginx: worker process
                   ├─8246 nginx: worker process
                   ├─8247 nginx: worker process
                   ├─8248 nginx: worker process
                   ├─8250 nginx: worker process
                   ├─8251 nginx: worker process
                   ├─8252 nginx: worker process
                   ├─8253 nginx: worker process
                   ├─8254 nginx: worker process
                   ├─8255 nginx: worker process
                   └─8312 Passenger AppPreloader: /var/www/nizanto (forking...)

        May 23 15:58:16 vmi182971.contaboserver.net systemd[1]: Starting A high performance web server and a reverse proxy server...
        May 23 15:58:17 vmi182971.contaboserver.net systemd[1]: Started A high performance web server and a reverse proxy server.

The username and password of Mysql are correct.

在使用 SQLite3 数据库引擎时,应用程序在 passenger + nginx 上运行良好。我猜问题只出在 Mysql 数据库引擎上。

感谢 Advance.Any 非常感谢您的帮助。

我找到了一个临时修复方法。我在 MySQL shell 中创建了一个新用户,而不是使用默认的根帐户。

  CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
  GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
  FLUSH PRIVILEGES;

database.yml

      development:
          adapter: mysql2
          host: localhost
          encoding: utf8
          reconnect: false
          database: demo_development
          pool: 5
          username: newuser
          password: password
          socket: /var/run/mysqld/mysqld.sock




        test:
          adapter: mysql2
          encoding: utf8
          reconnect: false
          database: demo_development
          pool: 5
          username: newuser
          password: password


        production:
          adapter: mysql2
          encoding: utf8
          reconnect: false
          database: demo_development
          pool: 5
          username: newuser
          password: password

更改 MySQL 中的用户和 database.yml 中的用户名最终解决了问题。还是不知道为什么用root账号会出现问题