Mysql2::Error: Can't connect to MySQL server on '127.0.0.1' on heroku push - ruby-on-rails

When i do git push heroku master ,it fails at rake assets precompile and i get an error Mysql2::Error: Can't connect to MySQL server on '127.0.0.1' heroku
What i already know
Since i am using Rails 4,Definitely config.assets.initialize_on_precompile = false is out of the picture because is no longer required.
heroku labs:enable user-env-compile this feature is no longer supported by Heroku.
After doing research online, I learned that the solution might be here
heroku build pack
But i don't understand how to run those commands.
I keep getting bash: bin/compile: No such file or directory
How do i resolve this issue? I am using Rails 4.2.5 ,Ruby 2.3.0 and the Db is Mysql locally.Thank you in advance.
database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: password
host: localhost
development:
<<: *default
database: respect_development
test:
<<: *default
database: respect_test
production:
<<: *default
database: respect_production
username: respect
password: <%= ENV['RESPECT_DATABASE_PASSWORD'] %>

It sounds like your database.yml is configured to use a local database (127.0.0.1) even on Heroku. What is in your database.yml? I would remove that file from git and check your Heroku config vars for DATABASE_URL. You should see a reference to the database on Heroku, not 127.0.0.1.

I just had to precompile assets rake assets:precompile locally and then push it git push heroku master
and in my environments/production.rb
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
Also i added this to my Gemfile
group :production do
gem 'rails_12factor'
end

Related

Capistrano updating my app from Sqlite3 to Rails, Gem::LoadError

I am upgrading my rails app from Sqlite3 to Postgresql. I am using Ansible to setup my server and Capistrano to deploy. I was able to successfully run ansible and now I'm trying to deploy my branch to an existing server that had Sqlite3 installed on it.
On the capistrano step:
deploy:assets:precompile
I get this error
$HOME/.rbenv/bin/rbenv exec bundle exec rake assets:precompile
01 rake aborted!
01 Gem::LoadError: Specified 'sqlite3' for database adapter, but the gem is not loaded. Add `gem 'sqlite3'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
But I don't understand. I have Postgresql set up in my branch I'm trying to deploy.
Here is my database yml file.
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: {appname}_development
test:
<<: *default
database: {appname}_test
staging:
<<: *default
database: {appname}_staging
username: <%= Rails.application.secrets[:db_username] %>
password: <%= Rails.application.secrets[:db_password] %>
Obviously changed {appname} with my actual application's name. I didn't include a production key cause this app does not have a production website only a staging server. It is more of a playground I use for rails.
Does anyone have any experience with fixing this issue? I'm really not sure if there is an issue with the Capfile or if I'm missing something. Thanks (and let me know if you need more of my code)
Worth noting this app is still running Rails 5.1 (I'm going to update this next I just wanted to get it on a Postgres db first)
EDIT: OK I am onto something. i discovered that this capistrano step:
04 ln -s /var/www/{appname}/shared/config/database.yml /var/www/{appname}/releases/20190923224949/config/database.yml
Is not updating my database yml to be the updated postgres db. I'm guessing I'm missing something somewhere. :cry:
I GOT IT. I'M A GENIUS.
I had to rereun the webserver playbook to get my database.yml file onto the server.

Rails : How to configure database.yml for Heroku?

I'm new on RoR. I build a little app using ActiveAdmin and Devise and I wish to deploy it on Heroku.
When I had push my app on Heroku it run properly but the db seems to be empty ! In effect, my local login dont match when I try to log in my ActiveAdmin administration panel...
In addition, the others db of my app are totaly empty...
I guess that i had not fill the database.yml correctly but I dont find how I'm suppose to do it... :/
Database.yml :
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
I would be grateful if you could help me or direct me to a solution!
Thank you for your attention ! ^^
production:
<<: *default
database: db/production.sqlite3
You can't use sqlite3 on heroku, because it is filesystem-based and on each restart a new dyno is created, with a completely new filesystem. The old dyno is killed and your sqlite3 data file with it.
Use a client-server DB on heroku, like Postgresql. This is well covered in heroku guides.
These steps is how to push the local database to heroku and to create database
heroku pg:backups:restore 'https://s3.amazonaws.com/me/items/3H0q/mydb.dump' DATABASE_URL
Get database info
heroku pg
-> Gave me the name of the database
eg: HEROKU_POSTGRESQL_CYAN
Reset the database on heroku
heroku pg:reset HEROKU_POSTGRESQL_CYAN
Look for the name of the local database
Opened config/database.yml and found the database
name for the development environment.
eg: fashions_development => put the name that the terminal give it to you
Run push the local database to Heroku
Opened config/database.yml and found the database
heroku pg:push fashions_development HEROKU_POSTGRESQL_CYAN

Testing whether my database connection is working with Heroku

The app on Heroku was displaying the error "We're sorry, but something went wrong" up until I ran heroku run rake db:migrate, at which point the app didn't display any errors and worked fine besides not displaying any records from the database like it's supposed to.
I'm not sure whether the connection to the database on Heroku isn't working or whether the database on Heroku just has nothing in it, in which case I don't know why I can't push the contents on my local database to Heroku. heroku db:push didn't return any errors.
No errors from Rails or Heroku Toolbelt in the terminal, no errors on my local version, no errors on the version on Heroku, no indication of what isn't working.
Not sure what else to say.
This is what database.yml looks like:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
<<: *default
database: # heroku database
username: # heroku username
password: # heroku password
Update: I inserted a record into the database on Heroku manually and it threw up the "We're sorry, but something went wrong error". So I guess the connection isn't working.
Push your local database to heroku using heroku pg:push. For example:
heroku pg:push mylocaldb HEROKU_POSTGRESQL_MAGENTA --app sushi
Check issues with your Heroku postgres database with heroku pg:diagnose
See the Heroku Postgres guide for more information

Heroku Push Error Assets Precompiling Port Error

I am having trouble pushing to heroku.
Here is my heroku log http://pastebin.com/M7q7qJ3x
The error occurred around here
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
and here is my database.yml file
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: postgresql
encoding: unicode
database: restaurant_development
pool: 5
username: judyngai
password:
host: localhost
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: postgresql
encoding: unicode
database: restaurant_test
pool: 5
username: judyngai
password:
host: localhost
production:
adapter: postgresql
encoding: unicode
database: restaurant_production
pool: 5
username: judyngai
password:
host: localhost
should I change the host to "127.0.0.1" and add in a port ?
This is odd. I don't understand this.
and here is another error
Precompiling assets failed, enabling runtime asset compilation
I did not touch the js,css files at all.
Should I be "compiling assets" locally before I push? I am not quite familiar with assets.
In your config in application.rb or production.rb add
config.assets.initialize_on_precompile = true
Gemfile
Add this
gem 'sass', '3.2.13'
its work for me.

"rails server" using the wrong database

I recently switched a relatively new rails app from sqlite3 to Amazon RDS and configured my database.yml file to use the RDS database in the production environment only.
But now, whenever I try to do any local action on my database (e.g. rails server, rails console, rake db:migrate, etc.) it does that action to the production DB on Amazon's servers rather than my local sqlite3 DB, which is my development DB.
# database.yml
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: mysql2
host: mydb.mydbhost.us-east-1.rds.amazonaws.com
reconnect: false
database: mydb
username: myusername
password: mypassword
What am I doing wrong?
UPDATE: Here's my environment.rb file:
# environment.rb
# Load the rails application
require File.expand_path('../application', __FILE__)
# Heroku environment variables for local use
heroku_env = File.join(Rails.root, 'config', 'heroku_env.rb')
load(heroku_env) if File.exists?(heroku_env)
# Initialize the rails application
Myapp::Application.initialize!
your not using productions settings
try
rails s -e production
or
RAILS_ENV=production rails s
RAILS_ENV=production rake db:migrate
Figured out the problem after taking a day to get away from it. It's a silly mistake on my part, but I thought I'd post the solution in case someone else encounters a similar issue.
As you can see from my environment.rb file above, I have a heroku_env.rb file, which contains all of my heroku-specific environment variables on my local machine for development purposes. In that file, I declared a ENV['DATABASE_URL'] variable, which links to my Amazon RDS database. Deleting this from the file solved the problem!
Thanks to everyone who offered answers to help!

Resources