Siddhant
Siddhant

Reputation: 313

Rails : Heroku CI : Mysql2::Error: Can't connect to MySQL server on '127.0.0.1' (111) for test env

I have deployed a rails app to heroku, using mysql database hosted on a remote server. For this I have added the heroku addon 'cleardb' and setup correct ENV Config vars specifying:

DATABASE_URL = mysql2://user:password@host?reconnect=true

I have also setup Heroku continuous deployment for three branches, (branch -> environment)

develop -> devint,
staging -> staging,
master -> production

Continuous deployment works fine, and the database is setup fine too (because when I create a record in rails app, I can see the data in remote mysql host).

My issue is that when I enable HEROKU CI, the build fails every time with an error saying Mysql2::Error: Can't connect to MySQL server on '127.0.0.1' (111)

Full error here: https://gist.github.com/siddhantbhardwaj/dab7c435815e7729d0f70081449f21ff#file-gistfile1-txt

This occurs when Heroku CI tries to perform rake db:schema:load_if_ruby for the :test env

My database.yml looks like:

default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: what_course_development
  username: user
  password: password
  socket: /tmp/mysql.sock

test:
  <<: *default
  database: what_course_test

devint:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>

staging:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>

production:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>

Upvotes: 2

Views: 771

Answers (1)

Darpan Chhatravala
Darpan Chhatravala

Reputation: 520

Please review below suggestion for configure your database.yml from DATABASE_URL:

DatabaseUrl.to_active_record_hash('postgres://uuu:[email protected]:1234/abc')

This will provide the below result:

{:adapter=>"postgres", :host=>"127.0.0.1", :port=>1234, :database=>"abc", :user=>"uuu", :password=>"xxx"}

Please refer seamusabshere database_url for more information.

Upvotes: 1

Related Questions