Report this

What is the reason for this report?

Rails app can't connect to DO MYSQL managed db

Posted on February 28, 2020

I can connect to the DO’s db from the app server through the flag command just fine, that’s how I exported the database .sql there successfully.

But the rails app is another story, it’s like it’s trying to look for a local mysql server even though I believe I’ve specified everything in the database.yml file. The error:

Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

The database.yml file:

default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: password
  socket: /var/run/mysqld/mysqld.sock

production:
  <<: *default
  username: doadmin
  password: password
  host: clients1-do-user-2960833-0.db.ondigitalocean.com
  port: 25060
  database: bw
  sslmode: REQUIRED
  sslca: /root/ca-certificate.crt
  url: mysql2://doadmin:password@clients1-do-user-2960833-0.db.ondigitalocean.com:25060/bw?ssl-mode=REQUIRED

Running ‘ufw status’:

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                  
3306                       ALLOW       Anywhere                  
Nginx Full                 ALLOW       Anywhere                  
25060                      ALLOW       Anywhere                  
OpenSSH (v6)               ALLOW       Anywhere (v6)             
3306 (v6)                  ALLOW       Anywhere (v6)             
Nginx Full (v6)            ALLOW       Anywhere (v6)             
25060 (v6)                 ALLOW       Anywhere (v6)  

I only have mysql-client installed, version 8 same as the DO server.

mysql  Ver 8.0.19 for Linux on x86_64 (MySQL Community Server - GPL)

Puma is starting rails with production as environment, as it should.

IDK what else to put in the database.yml to tell rails to stop looking into a local server.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there @hiagos93,

According to the error that you are getting I think that your Rails App is trying to connect to the default profile as in there you’ve specified the /var/run/mysqld/mysqld.sock socket.

Besides that, your production configuration looks correct.

I think that you just need to set your Rails env to production and it should pick up the correct database config.

Hope that this helps! Regards, Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.