代码之家  ›  专栏  ›  技术社区  ›  current_user

在passenger+nginx中部署时,在rails中出现mysql错误

  •  0
  • current_user  · 技术社区  · 7 年前

    我有一个带有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
    

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

    数据库.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 prompt,没有任何问题。

    现在我想在passenger+nginx上部署rails应用程序。

    我的网站.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
    

    我有这个错误

    enter image description here

    nginx错误.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数据库引擎时,应用程序可以与passenger+nginx一起正常工作。 。我想问题只出在MySQL数据库引擎上。

    事先谢谢。非常感谢您的帮助。

    1 回复  |  直到 7 年前
        1
  •  0
  •   current_user    7 年前

    我找到了一个临时的解决办法。我在mysql shell中创建了一个新用户,而不是使用默认的根帐户。

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

    数据库.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中的用户名最终解决了这个问题。不过,不知道为什么使用根帐户会导致这个问题。