Rails app can't connect to DO MYSQL managed db

Posted February 28, 2020 4.3k views
DigitalOcean Managed MySQL Database

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

  <<: *default
  username: doadmin
  password: password
  port: 25060
  database: bw
  sslmode: REQUIRED
  sslca: /root/ca-certificate.crt
  url: mysql2://

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.

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.

Submit an Answer
1 answer

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!

  • Puma’s config had the env set to production and all migrate commands I wrote had the RAILS_ENV=production appended to it.

    I’ve managed to create a new database server using a regular droplet rather than the managed db service and I got it to work. What I’ve changed was to had mysql-client and mysql-server installed on both app and database droplets.

    Regarding the service, I stopped trying to make it work since I would be wasting my time. Ironically something that is supposed to make things easier and quicker was more complicated than settings everything yourself with default bare-bones droplets.