How to connect to remote mysql server from Rails application? - ruby-on-rails

I am trying to connect to remote mysql server. This is how I set up my connection:-
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: 13.58.156.50
port: 3306
# username: <%= ENV['FOODCLUBE_DATABASE_USER_DEVELOPMENT'] %>
# password: <%= ENV['FOODCLUBE_DATABASE_PASSWORD_DEVELOPMENT'] %>
username: root
password: Admin123#
socket: /var/run/mysqld/mysqld.sock
variables:
sql_mode: "TRADITIONAL"
development:
<<: *default
database: timer
# food_delivery_development
test:
<<: *default
database: food_club_test
production:
<<: *default
database: food_clube_production
username: <%= ENV['FOODCLUBE_DATABASE_USER_PRODUCTION'] %>
password: <%= ENV['FOODCLUBE_DATABASE_PASSWORD_PRODUCTION'] %>
When I am trying to hit a query from the Rails console, I am getting this error message:-
Mysql2::Error::ConnectionError: Lost connection to MySQL server at 'reading initial communication packet', system error: 0
How can I fix this?

Related

Can't run rails db:create for postgres RDS on AWS

I created an postgres RDS instance,** which I can connect to from my EC2 instance and my local shell**.
But for some reason when I run rails db:create I get the following error:
FATAL: password authentication failed for user "my_user"
Any pointers?
I can connect to the RDS instance from EC2 instance and local machine.
I expect rails to be able to run migrations as well, but it throws an error.
db.yml:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: use
password: gresgres
development:
<<: *default
database: myapp_dev
test:
<<: *default
database: myapp_test
production:
<<: *default
database: <%= ENV['PG_RDS_DBNAME'] %>
username: <%= ENV['PG_RDS_USERNAME'] %>
password: <%= ENV['PG_RDS_PASSWORD'] %>
host: <%= ENV['PG_RDS_ENDPOINT'] %>
port: <%= ENV['PG_RDS_PORT'] %>

Postgis is not setting up in Rails application

I am integrating PostGIS in a rails application and following their documentation https://github.com/rgeo/activerecord-postgis-adapter.
At this step
rake db:create
I get the following error.
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near "SUPPORT"
: CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public
My database.yml
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
adapter: postgis
encoding: unicode
postgis_extension: postgis # default is postgis
postgis_schema: public # default is public
schema_search_path: public,postgis
pool: 5
database: my_app_development # your database name
test:
<<: *default
database: my_app_test
production:
<<: *default
database: my_app_production
username: my_app
password: <%= ENV['MY_APP_DATABASE_PASSWORD'] %>
Just remove all options, that you don't really need in you database.yml.
default: &default
adapter: postgis
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: my_app_development # your database name
test:
<<: *default
database: my_app_test
production:
<<: *default
database: my_app_production
username: my_app
password: <%= ENV['MY_APP_DATABASE_PASSWORD'] %>

rails SQlite 3: Cant open the connection

I have existing rails project with PostgreSQL DB. When I am running the same in my local machine I am receiving SQLite exception.
But my database.yml configured with postgres adapter only.
Here is the exception:
SQLite3::CantOpenException: unable to open database file
database.yml
postgres: &postgres
adapter: postgresql
encoding: unicode
host: localhost
pool: 5
username: postgres
password: postgres
min_messages: warning
development:
<<: *postgres
database: dev_development
username: <%= ENV['PG_USER'] || 'postgres' %>
password: <%= ENV['PG_PASSWORD'] || 'postgres' %>
test:
<<: *postgres
database: dev_test
username: <%= ENV['PG_USER'] || 'postgres' %>
password: <%= ENV['PG_PASSWORD'] %>
Perhaps you have an environment variable set (DATABASE_URL) that is overriding your database.yml file? Look at this for more info: http://edgeguides.rubyonrails.org/configuring.html#configuring-a-database
If that's not it, is it possible you recently changed your database.yml file and haven't restarted the server?

Rails Console, Password needed to create new record

I am currently trying to complete a tutorial on ruby on rails and have had this error within IRB when I enter "subject = Subject.new"
Mysql2::Error: Access denied for user 'simple_cms'#'localhost' (using password: YES)
is there a way to supply this to irb when i run
rails console
What is the best way to resolve this?
database.yml is below:
default: &default
adapter: mysql2
encoding: utf8
reconnect: false
host: localhost
pool: 5
username: simple_cms
password: mypasshere
socket: /tmp/mysql.sock
development:
<<: *default
database: simple_cms_development
<<: *default
database: simple_cms_test
production:
<<: *default
database: simple_cms_production
username: simple_cms
password: <%= ENV['SIMPLE_CMS_DATABASE_PASSWORD'] %>
Much appreciated for any help!

Ruby on Rails: How can I edit database.yml for postgresql?

Rails new app.
The current database.yml is like that:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# 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: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
I need to edit this for postgresql database.
How can I do this?
Simply:
development:
adapter: postgresql
encoding: unicode
database: blog_development
pool: 5
username: blog
password:
host: localhost
Source: Configuring Rails Applications
development:
adapter: postgresql
encoding: utf8
database: name
username: hading
password: my_db_password
pool: 5 # not mandatory
timeout: 5000 # not mandatory
host: localhost
port: your postgresql port number (5432 or 5433)
As Zabba said it's
development:
adapter: postgresql
encoding: unicode
database: blog_development
pool: 5
username: blog
password:
As mentioned in the Configuring Rails Applications. But you might want an additional min_messages: WARNING, to get rid of the nasty NOTICE messages postgresql gives you during a migration. So my database.yml entry looks like this
development:
adapter: postgresql
encoding: unicode
database: blog_development
pool: 5
username: blog
password:
min_messages: WARNING
You might be interested in generating new app with postgres default:
rails new myapp --database=postgresql
as mentioned here: https://devcenter.heroku.com/articles/getting-started-with-rails4
development:
adapter: postgresql
encoding: utf8
database: name
username: hading
password: my_db_password
host: localhost # not mandatory
pool: 5 # not mandatory
timeout: 5000 # not mandatory
Simply use
rails new app_name --database=postgresql
or if existing application try
development:
adapter: postgresql
encoding: unicode
database: app_dev
pool: 5
username: username
password: password
host: localhost
Another way is to have the common values in &default and then have individual values for the other environments, which can be based on environment variables:
default: &default
adapter: postgresql
encoding: unicode
port: <%= ENV.fetch("POSTGRESQL_PORT", "5432") %>
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: <%= ENV['POSTGRESQL_USER_NAME'] %>
password: <%= ENV.fetch("POSTGRESQL_PASSWORD", "myS3cr3tP4ssw0rd") %>
host: <%= ENV['POSTGRESQL_HOST'] %>
development:
<<: *default
database: <%= ENV['POSTGRESQL_DB'] %>-development
host: db
test:
<<: *default
database: <%= ENV['POSTGRESQL_DB'] %>-test
host: db
production:
<<: *default
database: <%= ENV['POSTGRESQL_DB'] %>
Here all the values can come from environment variables (if you use Docker or Bitbucket Pipelines) or you can have them in your .env files.

Resources