Deploy Rails Application on Heroku : ERROR - mysql

I am trying to execute heroku run rake db:migrate, but that's the result :
rake aborted!
PG::Error: ERROR: invalid value for parameter "client_encoding": "utf8mb4"
I want to use mysql database in development and postgres in production, and so that's the way that I configured my database.yml :
postgres: &postgres
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
mysql: &mysql
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password:
socket: /var/run/mysqld/mysqld.sock
development:
<<: *mysql
database: event_development
test:
<<: *mysql
database: event_test
production:
<<: *postgres
database: event_production
username: event
password: <%= ENV['EVENT_DATABASE_PASSWORD'] %>
How can I solve this?

Just use url for it. If you have installed "Heroku Postgres" add-on. There supposed to be the DATABASE_URL in config vars.
database.yml:
...
production:
url: <%= ENV['DATABASE_URL'] %>

Related

Ruby on Rails 5.2.0 Mysql2::Error::ConnectionError SSL connection error: unknown error number

I am new to RoR, but not new to web development in general.
I am trying to switch a Rails app from sqlite3 to mysql2, it is api-only.
Added the gem to Gemfile, used bundle install, everything fine.
I run rails server and see the error bellow: https://imgur.com/c4Qqf1k
This is my Database.yml:
default: &default
adapter: mysql2
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
encoding: utf8
reconnect: true
host: 127.0.0.1
port: 3306
username: root
password: 123456
socket: /tmp/mysql.sock
development:
<<: *default
database: beginner_dev
test:
<<: *default
database: beginner_test
production:
<<: *default
database: beginner_prod
I succeeded to solve the problem by adding the following lines to database.yml, default section:
ssl_mode: :disabled
sslverify: false
The error isn't so descriptive, but apparently it was related to ssl.
Replace your database.yml content with the following configs
default: &default
adapter: mysql2
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
encoding: utf8
reconnect: true
host: 127.0.0.1
port: 3306
username: root
password: 123456
development:
<<: *default
database: beginner_dev
test:
<<: *default
database: beginner_test
production:
<<: *default
database: beginner_prod

How to change the database in a Rails application

The database of my Rails application is SQLite3, but I want to change it to MySQL. What do I need to do to change it in my application?
This is database.yml from my application:
# SQLite. Versions 3.8.0 and up are supported.
# 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
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
You need to update the adapter to MySQL2 and add your credentials. For instance:
development:
adapter: mysql2
encoding: utf8mb4
database: development
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: password
socket: /tmp/mysql.sock
Look at the offical documentation for more information.
Here is it with defaults:
default: &default
adapter: mysql2
timeout: 5000
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
socket: /tmp/mysql.sock
development:
<<: *default
database: development
username: <%= ENV.fetch("username") %>
password: <%= ENV.fetch("password") %>
test:
<<: *default
database: test
username: username
password: password
production:
<<: *default
database: production
username: <%= ENV.fetch("username") %>
password: <%= ENV.fetch("password") %>
You can also encode username and password in the database like this:
mysql://<username>:<password>#<host>:<port>/<db_name>
and then set host. Don't put username and password in your database.yml file.

"YAML syntax error occurred while parsing config/database.yml" Ruby on Rails

Everytime I run my rails application I get this error pointing to "encoding: utf8" in the production section of database.yml. If I reload the page it takes me to the app but I'm afraid something is wrong with the database.
Here is my database.yml folder:
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: b6f4e1d86a2a08
password: 25205573
host: us-cdbr-iron-east-05.cleardb.net
development:
<<: *default
database: DBProj_development
test:
<<: *default
database: DBProj_test
production:
<<: *default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: b6f4e1d86a2a08
password: 25205573
host: us-cdbr-iron-east-05.cleardb.net
database: #localhost
I completely understand that YAML must be consistently indented using spaces. Tabs are not allowed.I do not think this is the problem. I have not found one only source to remedy this problem.
You have 2 issues there. Wrong level of indentation below adapter in the production label. And the value of the database label can't start with an #, so you have to place it between quotation marks:
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: b6f4e1d86a2a08
password: 25205573
host: us-cdbr-iron-east-05.cleardb.net
development:
<<: *default
database: DBProj_development
test:
<<: *default
database: DBProj_test
production:
<<: *default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: b6f4e1d86a2a08
password: 25205573
host: us-cdbr-iron-east-05.cleardb.net
database: "#localhost"
That will fix your parsing issues.

crontab shows the mysql error

I am creating the rails app with 'whenever' gem.
And I defined the function in the User model and I want to execute the function every 7am.
But my function is not executing correctly and it shows the error.
schedule.rb
set :output, 'log/crontab.log'
set :environment, :production
every 1.day, at: '7:00 am' do
runner 'User.create_group'
end
crontab.log
/Users/michel/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/mysql2-0.4.8/lib/mysql2/client.rb:89:in `connect': Access denied for user ''#'localhost' to database 'appnorth_production' (Mysql2::Error)
here is my database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password:
socket: /tmp/mysql.sock
development:
<<: *default
database: appnorth_development
test:
<<: *default
database: appnorth_test
production:
<<: *default
database: appnorth_production
username: appnorth
password: <%= ENV['APPNORTH_DATABASE_PASSWORD'] %>
Does anyone help me?
Please insert the following line to schedule.rb.
And, Could you try?
job_type :runner, "export :environment_variable=:environment && cd :path && bin/rails runner ':task' :output"

convert database of ruby on rails app from sql server to mysql

i had clone a ruby on rails app with sql server, with some code like :
# Gemfile
gem 'tiny_tds'
gem 'activerecord-sqlserver-adapter', '~> 4.2.0'
and database.yml
default: &default
adapter: sqlserver
azure: true
development: &development
<<: *default
host: <%= ENV['DATABASE_HOST'] %>
database: <%= ENV['DATABASE_NAME'] %>
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASS'] %>
development_vehicle: &development_vehicle
<<: *default
host: <%= ENV['DATABASE_VEHICLE_HOST'] || ENV['DATABASE_HOST'] %>
database: <%= ENV['DATABASE_VEHICLE_NAME'] || ENV['DATABASE_NAME'] %>
username: <%= ENV['DATABASE_VEHICLE_USER'] || ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_VEHICLE_PASS'] || ENV['DATABASE_PASS'] %>
development_aspnet: &development_aspnet
<<: *default
host: <%= ENV['DATABASE_ASPNETDB_HOST'] || ENV['DATABASE_HOST'] %>
database: <%= ENV['DATABASE_ASPNETDB_NAME'] || ENV['DATABASE_NAME'] %>
username: <%= ENV['DATABASE_ASPNETDB_USER'] || ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_ASPNETDB_PASS'] || ENV['DATABASE_PASS'] %>
db folder like :
but my local use mysql, so i want to use mysql instead to sql server to buil this app
can you help me to convert it ?
thanks!
Gemfile
# gem 'tiny_tds'
# gem 'activerecord-sqlserver-adapter', '~> 4.2.0'
gem 'mysql2'
config/database.yml
default: &default
adapter: mysql2
pool: 5
timeout: 5000
username : root #mysql username
password : root #mysql password
development:
<<: *default
database: db_name
test:
<<: *default
database: db_name
production:
<<: *default
database: db_name
Add mysql2 gem and remove other connection gem required for sql server.
# Gemfile
# gem 'tiny_tds'
# gem 'activerecord-sqlserver-adapter', '~> 4.2.0'
gem 'mysql2'
database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: 5
socket: /tmp/mysql.sock
development: &development
<<: *default
host: yourhostfordevelopement # commonly localhost
database: yourdb # for development
username: dbusername # commonly root
password: dbpasseord # password you configured
development_vehicle: &development_vehicle
<<: *default
host: yourhostfordevelopement # commonly localhost
database: yourdb # for development_vehicle
username: dbusername # commonly root
password: dbpasseord # password you configured
development_aspnet: &development_aspnet
<<: *default
host: yourhostfordevelopement # commonly localhost
database: yourdb # for development_aspnet
username: dbusername # commonly root
password: dbpasseord # password you configured