I have a rails app with MySql database engine. I am able to run the application in development mode locally on a server(173.xxx.xxx.xxx) using
cd /var/www/demo
rails s -b 0.0.0.0 -p3001
the server started
=> 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
I have gem 'mysql2', '~> 0.5.1'
in Gemfile.
bundle installed successfully.
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
Everything is working absolutely fine locally on the server(I could access the application from the browser by
).The App is Working fine through IP. The Mysql Username and Password are correct(I checked with mysql -u root -p
).I Am able to successfully login to Mysql Prompt without any problem.
Now I want to deploy rails application on Passenger + Nginx.
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;
}
I restarted nginx
sudo service nginx restart
I am getting this error
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)
But it is very strange that the same code base is running fine locally(rails s -b 0.0.0.0 -p3001) with no MySql errors.
sudo systemctl status nginx
result
● 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.
While using SQLite3 database engine the aplication is working fine with passenger + nginx.I Guess the issue is only with Mysql database engine.
Thanks in Advance.Any Help is Highly appreciated.
I found a temporary fix. I Created a new user within the MySQL shell instead of using default root account.
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
Changing the user in MySQL and username in the database.yml finally fixed the problem. Still, don't know why using root account caused the problem.