Rails Database connection issue when deployed in production mode with puma + Nginx

Viewed 295

The error that I am getting is

Mysql2::Error::ConnectionError in BlogController#home

Access denied for user 'abhips'@'localhost' (using password: NO)

The stack is Rails-6 + MySQL-8 deployed with Nginx + puma-4.

I am getting the connection error when I deploy the code in production mode. It is working perfectly on my local machine.

Weird thing is that I am not getting that error when I stop puma and run the code with RAILS_ENV=production rails s on the server.

And I tested the connection in rails console in production mode, which is also working fine. So the config/database.yml has no problem.

No errors were showing up in the logs so I had to enable config.consider_all_requests_local = true in production.rb file. After that, I got the error details.

So my question is

  1. Why am I getting this error? How can I fix this?
  2. Why is the error log file not capturing the error?

I will list the configuration files below

config/puma/production.rb

    # Change to match your CPU core count
    workers 1

    # Min and Max threads per worker
    threads 1, 6

    app_dir = File.expand_path("../../..", __FILE__)
    shared_dir = "#{app_dir}/shared"

    # Default to production
    rails_env = ENV['RAILS_ENV'] || "production"
    environment rails_env

    # Set up socket location
    bind "unix://#{shared_dir}/sockets/puma.sock"

    # Logging
    stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true

    # Set master PID and state locations
    pidfile "#{shared_dir}/pids/puma.pid"
    state_path "#{shared_dir}/pids/puma.state"
    activate_control_app

/etc/nginx/sites-enabled/myapp

    upstream my_app {
        server unix:///var/www/myapp.me/shared/sockets/puma.sock fail_timeout=100;
    }

    server {
        if ($host = xxx.xx.xxx.x){
           return 301 https://myapp.me$request_uri;
        }
    }

    server {

        listen 443 ssl http2; # managed by Certbot
        listen [::]:443 ssl http2; # add http2 to enable http2

        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

        if ($host = www.myapp.me) {
            return 301 https://myapp.me$request_uri;
        } # managed by Certbot

       server_name  myapp.me www.myapp.me;
       root         /var/www/myapp.me;

       access_log /var/www/myapp.me/shared/log/nginx.stdout.log;
       error_log  /var/www/myapp.me/shared/log/nginx.stderr.log;

       # try_files $uri @my_app;

       location / {
           try_files $uri @my_app;
       }

       location @my_app {
           proxy_set_header X-Real-IP        $remote_addr;
           proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
           proxy_set_header Host             $http_host;
           proxy_redirect off;
           proxy_pass http://ny_app;
       }

       #    error_page 500 502 503 504 /500.html;
       #    location = /500.html {
       #        root /var/www/myapp.me/public;
       #    }

       ssl_certificate /etc/letsencrypt/live/myapp.me/fullchain.pem; # managed by Certbot
       ssl_certificate_key /etc/letsencrypt/live/myapp.me/privkey.pem; # managed by Certbot
       include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
       ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    }

    server {

        listen 80;
        listen [::]:80;

        if ($host = www.myapp.me) {
            return 301 https://myapp.me$request_uri;
        } # managed by Certbot


       if ($host = myapp.me) {
           return 301 https://myapp.me$request_uri;
       } # managed by Certbot

       server_name  myapp.me www.myapp.me;
       return 404; # managed by Certbot
    }

/etc/systemd/system/puma.service

    [Unit]
    Description=Puma HTTP Server
    After=network.target

    # Uncomment for socket activation (see below)
    # Requires=puma.socket

    [Service]
    # Foreground process (do not use --daemon in ExecStart or config.rb)
    Type=simple

    # Preferably configure a non-privileged user
    User=abhips
    Group=abhips

    # The path to the your application code root directory.
    # Also replace the "<YOUR_APP_PATH>" place holders below with this path.
    # Example /home/username/myapp
    WorkingDirectory=/var/www/myapp.me

    # Helpful for debugging socket activation, etc.
    Environment=RAILS_ENV=production

    # SystemD will not run puma even if it is in your path. You must specify
    # an absolute URL to puma. For example /usr/local/bin/puma
    # Alternatively, create a binstub with `bundle binstubs puma --path ./sbin` in the WorkingDirectory
    ExecStart=/bin/bash -lc "/home/abhips/.rbenv/shims/puma -C /var/www/myapp.me/config/puma/production.rb"

    ExecStop=/bin/bash -lc "/home/abhips/.rbenv/shims/puma -S /var/www/myapp.me/shared/pids/puma.state"

    # Path to PID file so that systemd knows which is the master process
    PIDFile=/var/www/myapp.me/shared/pids/puma.pid

    Restart=always
    # decides when to restart after a crash 
    RestartSec=10

    [Install]
    WantedBy=multi-user.target

Full trace

            Rails.root: /var/www/myapp.me
            
            Application Trace | Framework Trace | Full Trace
            mysql2 (0.5.3) lib/mysql2/client.rb:90:in `connect'
            mysql2 (0.5.3) lib/mysql2/client.rb:90:in `initialize'
            activerecord (6.0.3.1) lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `new'
            activerecord (6.0.3.1) lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `mysql2_connection'
            activerecord (6.0.3.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:887:in `new_connection'
            activerecord (6.0.3.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `checkout_new_connection'
            activerecord (6.0.3.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:910:in `try_to_checkout_new_connection'
            activerecord (6.0.3.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:871:in `acquire_connection'
            activerecord (6.0.3.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:593:in `checkout'
            activerecord (6.0.3.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:437:in `connection'
            activerecord (6.0.3.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:1119:in `retrieve_connection'
            activerecord (6.0.3.1) lib/active_record/connection_handling.rb:221:in `retrieve_connection'
            activerecord (6.0.3.1) lib/active_record/connection_handling.rb:189:in `connection'
            activerecord (6.0.3.1) lib/active_record/model_schema.rb:488:in `load_schema!'
            activerecord (6.0.3.1) lib/active_record/attributes.rb:247:in `load_schema!'
            activerecord (6.0.3.1) lib/active_record/attribute_decorators.rb:50:in `load_schema!'
            activerecord (6.0.3.1) lib/active_record/model_schema.rb:478:in `block in load_schema'
            activerecord (6.0.3.1) lib/active_record/model_schema.rb:475:in `synchronize'
            activerecord (6.0.3.1) lib/active_record/model_schema.rb:475:in `load_schema'
            activerecord (6.0.3.1) lib/active_record/model_schema.rb:362:in `attribute_types'
            activerecord (6.0.3.1) lib/active_record/model_schema.rb:386:in `type_for_attribute'
            activerecord (6.0.3.1) lib/active_record/table_metadata.rb:33:in `type'
            activerecord (6.0.3.1) lib/active_record/relation/predicate_builder.rb:51:in `build'
            activerecord (6.0.3.1) lib/active_record/relation/predicate_builder.rb:109:in `block in expand_from_hash'
            activerecord (6.0.3.1) lib/active_record/relation/predicate_builder.rb:68:in `each'
            activerecord (6.0.3.1) lib/active_record/relation/predicate_builder.rb:68:in `flat_map'
            activerecord (6.0.3.1) lib/active_record/relation/predicate_builder.rb:68:in `expand_from_hash'
            activerecord (6.0.3.1) lib/active_record/relation/predicate_builder.rb:21:in `build_from_hash'
            activerecord (6.0.3.1) lib/active_record/relation/where_clause_factory.rb:19:in `build'
            activerecord (6.0.3.1) lib/active_record/relation/query_methods.rb:656:in `where!'
            activerecord (6.0.3.1) lib/active_record/relation/query_methods.rb:649:in `where'
            activerecord (6.0.3.1) lib/active_record/querying.rb:21:in `where' paranoia (2.4.2) lib/paranoia.rb:247:in `paranoia_scope'
            activerecord (6.0.3.1) lib/active_record/relation/delegation.rb:66:in `block in paranoia_scope'
            activerecord (6.0.3.1) lib/active_record/relation.rb:407:in `block in scoping'
            activerecord (6.0.3.1) lib/active_record/relation.rb:784:in `_scoping'
            activerecord (6.0.3.1) lib/active_record/relation.rb:407:in `scoping'
            activerecord (6.0.3.1) lib/active_record/relation/delegation.rb:66:in `paranoia_scope' 
            paranoia (2.4.2) lib/paranoia.rb:252:in `block in acts_as_paranoid'
            activerecord (6.0.3.1) lib/active_record/scoping/default.rb:120:in `instance_exec'
            activerecord (6.0.3.1) lib/active_record/scoping/default.rb:120:in `block (2 levels) in build_default_scope'
            activerecord (6.0.3.1) lib/active_record/scoping/default.rb:118:in `each'
            activerecord (6.0.3.1) lib/active_record/scoping/default.rb:118:in `inject'
            activerecord (6.0.3.1) lib/active_record/scoping/default.rb:118:in `block in build_default_scope'
            activerecord (6.0.3.1) lib/active_record/scoping/default.rb:142:in `evaluate_default_scope'
            activerecord (6.0.3.1) lib/active_record/scoping/default.rb:117:in `build_default_scope'
            activerecord (6.0.3.1) lib/active_record/scoping/named.rb:58:in `default_scoped'
            activerecord (6.0.3.1) lib/active_record/scoping/named.rb:44:in `all'
            activerecord (6.0.3.1) lib/active_record/querying.rb:21:in `order' app/controllers/blog_controller.rb:9:in `home'

            actionpack (6.0.3.1) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
            actionpack (6.0.3.1) lib/abstract_controller/base.rb:195:in `process_action'
            actionpack (6.0.3.1) lib/action_controller/metal/rendering.rb:30:in `process_action'
            actionpack (6.0.3.1) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
            activesupport (6.0.3.1) lib/active_support/callbacks.rb:135:in `run_callbacks'
            actionpack (6.0.3.1) lib/abstract_controller/callbacks.rb:41:in `process_action'
            actionpack (6.0.3.1) lib/action_controller/metal/rescue.rb:22:in `process_action'
            actionpack (6.0.3.1) lib/action_controller/metal/instrumentation.rb:33:in `block in process_action'
            activesupport (6.0.3.1) lib/active_support/notifications.rb:180:in `block in instrument'
            activesupport (6.0.3.1) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
            activesupport (6.0.3.1) lib/active_support/notifications.rb:180:in `instrument'
            actionpack (6.0.3.1) lib/action_controller/metal/instrumentation.rb:32:in `process_action'
            actionpack (6.0.3.1) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
            activerecord (6.0.3.1) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
            actionpack (6.0.3.1) lib/abstract_controller/base.rb:136:in `process'
            actionview (6.0.3.1) lib/action_view/rendering.rb:39:in `process'
            actionpack (6.0.3.1) lib/action_controller/metal.rb:190:in `dispatch'
            actionpack (6.0.3.1) lib/action_controller/metal.rb:254:in `dispatch'
            actionpack (6.0.3.1) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
            actionpack (6.0.3.1) lib/action_dispatch/routing/route_set.rb:33:in `serve'
            actionpack (6.0.3.1) lib/action_dispatch/journey/router.rb:49:in `block in serve'
            actionpack (6.0.3.1) lib/action_dispatch/journey/router.rb:32:in `each'
            actionpack (6.0.3.1) lib/action_dispatch/journey/router.rb:32:in `serve'
            actionpack (6.0.3.1) lib/action_dispatch/routing/route_set.rb:834:in `call'
            warden (1.2.8) lib/warden/manager.rb:36:in `block in call'
            warden (1.2.8) lib/warden/manager.rb:34:in `catch'
            warden (1.2.8) lib/warden/manager.rb:34:in `call'
            rack (2.2.3) lib/rack/tempfile_reaper.rb:15:in `call'
            rack (2.2.3) lib/rack/etag.rb:27:in `call'
            rack (2.2.3) lib/rack/conditional_get.rb:27:in `call'
            rack (2.2.3) lib/rack/head.rb:12:in `call'
            actionpack (6.0.3.1) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
            rack (2.2.3) lib/rack/session/abstract/id.rb:266:in `context'
            rack (2.2.3) lib/rack/session/abstract/id.rb:260:in `call'
            actionpack (6.0.3.1) lib/action_dispatch/middleware/cookies.rb:648:in `call'
            actionpack (6.0.3.1) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
            activesupport (6.0.3.1) lib/active_support/callbacks.rb:101:in `run_callbacks'
            actionpack (6.0.3.1) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
            actionpack (6.0.3.1) lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
            actionpack (6.0.3.1) lib/action_dispatch/middleware/debug_exceptions.rb:32:in `call'
            actionpack (6.0.3.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
            railties (6.0.3.1) lib/rails/rack/logger.rb:37:in `call_app'
            railties (6.0.3.1) lib/rails/rack/logger.rb:26:in `block in call'
            activesupport (6.0.3.1) lib/active_support/tagged_logging.rb:80:in `block in tagged'
            activesupport (6.0.3.1) lib/active_support/tagged_logging.rb:28:in `tagged'
            activesupport (6.0.3.1) lib/active_support/tagged_logging.rb:80:in `tagged'
            railties (6.0.3.1) lib/rails/rack/logger.rb:26:in `call'
            actionpack (6.0.3.1) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
            actionpack (6.0.3.1) lib/action_dispatch/middleware/request_id.rb:27:in `call'
            rack (2.2.3) lib/rack/method_override.rb:24:in `call'
            rack (2.2.3) lib/rack/runtime.rb:22:in `call'
            activesupport (6.0.3.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
            actionpack (6.0.3.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
            rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
            actionpack (6.0.3.1) lib/action_dispatch/middleware/host_authorization.rb:76:in `call'
            railties (6.0.3.1) lib/rails/engine.rb:527:in `call'
            puma (4.3.5) lib/puma/configuration.rb:228:in `call'
            puma (4.3.5) lib/puma/server.rb:713:in `handle_request'
            puma (4.3.5) lib/puma/server.rb:472:in `process_client'
            puma (4.3.5) lib/puma/server.rb:328:in `block in run'
            puma (4.3.5) lib/puma/thread_pool.rb:134:in `block in spawn_thread' 

Please let me know if anything else to be posted here

0 Answers
Related