crontab shows the mysql error - mysql

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"

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.

Deploy Rails Application on Heroku : ERROR

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'] %>

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

Correct MySQL configuration for Ruby on Rails Database.yml file

I have this configuration:
development:
adapter: mysql2
encoding: utf8
database: my_db_name
username: root
password: my_password
host: mysql://127.0.0.1:3306
And I am getting this error:
Unknown MySQL server host 'mysql://127.0.0.1:3306' (1)
Is there something obvious that I am doing incorrectly?
You should separate the host from the port number.
You could have something, like:
development:
adapter: mysql2
encoding: utf8
database: my_db_name
username: root
password: my_password
host: 127.0.0.1
port: 3306
You also can do like this:
default: &default
adapter: mysql2
encoding: utf8
username: root
password:
host: 127.0.0.1
port: 3306
development:
<<: *default
database: development_db_name
test:
<<: *default
database: test_db_name
production:
<<: *default
database: production_db_name
Use 'utf8mb4' as encoding to cover all unicode (including emojis)
default: &default
adapter: mysql2
encoding: utf8mb4
collation: utf8mb4_bin
username: <%= ENV.fetch("MYSQL_USERNAME") %>
password: <%= ENV.fetch("MYSQL_PASSWORD") %>
host: <%= ENV.fetch("MYSQL_HOST") %>
(Reference1)
(Reference2)
If you can have an empty config/database.yml file then define ENV['DATABASE_URL'] variable, then It will work
$ cat config/database.yml
 
$ echo $DATABASE_URL
mysql://root:my_password#127.0.0.1:3306/my_db_name
for Heroku:
heroku config:set DATABASE_URL='mysql://root:my_password#host.com/my_db_name'
If you have multiple databases for testing and development this might help
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: DBNAME
pool: 5
username: usr
password: paswd
shost: localhost
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: DBNAME
pool: 5
username: usr
password: paswd
shost: localhost
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: DBNAME
pool: 5
username: usr
password: paswd
shost: localhost
None of these anwers worked for me, I found Werner Bihl's answer that fixed the problem.
Getting "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'" error when setting up mysql database for Ruby on Rails app