部署在Passenger + Nginx中的Rails中出现MySql错误

问题描述 投票:0回答:1

我有一个带有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 gem 'mysql2', '~> 0.5.1' 。 捆绑软件安装成功。

数据库

            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 Prompt,没有任何问题。

现在,我想在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)

但是很奇怪的是,同一代码库在本地运行良好(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.

Mysql的用户名和密码正确。

在使用SQLite3数据库引擎时,apassion + nginx的应用程序工作正常 。我猜问题仅在于Mysql数据库引擎。

在此先感谢您,我们非常感谢您的帮助。

mysql ruby-on-rails ubuntu nginx mysql2
1个回答
0
投票

我找到了一个临时解决方案。 我在MySQL Shell中创建了一个新用户,而不是使用默认的根帐户。

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

数据库

      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帐户会导致此问题。

© www.soinside.com 2019 - 2024. All rights reserved.