Fixing a 'Connecting to database specified by database.yml' Loop
Occasionally an app is re-deployed but won't start. You open the Rails log (at log/production.log) and see 'Connecting to database specified by database.yml' being outputted, again and again, in a loop.
How to fix?
The good news is that seeing 'Connecting to database specified by database.yml' rarely has anything to do with the database at all! It's usually due to a syntax error in your app - in a model, initializer, or controller. SSH into the server, navigate to the root folder of the Rails app, and run
rails console production. Rails will attempt booting the console for the production environment. If there is a syntax error, this will be caught here and you will get an error message telling you exactly where to look to fix the error.
If there is no syntax error, you actually do have a connection problem. This can usually be fixed by updating your database.yml in the following ways.
- First, change
host: 127.0.0.1. Some servers don't interpret
- If that doesn't work, try adding the port number. For example, mysql generally runs on port 3306.
- If that doesn't work, try adding the socket path. For example, mysql's socket path is usually at: /var/run/mysqld/mysqld.sock
So your database.yml block should look like this:
production: adapter: mysql2 encoding: utf8 database: database_name username: database_user password: database_password host: 127.0.0.1 port: 3306 socket: /var/run/mysqld/mysqld.sock pool: 5 timeout: 5000
You can also check from the console if your connection is working running
Follow David on Twitter
It's what all the cool kids do.