Ruby on Rails server cannot connect to mysql - mysql

everyone.
I am trying to set up rails 3 project with mysql as database on macOS X. However, I receive the next error:
/usr/local/bundle/gems/activerecord-3.2.22.5/lib/active_record/connection_adapters/abstract/connection_specification.rb:68:in connection_url_to_hash': undefined method `sub' for nil:NilClass (NoMethodError)
config/database.yml
default: &default
adapter: mysql2
pool: 5
encoding: utf8
min_messages: warning
timeout: 5000
username: root
password:
database: heart_development
host: localhost
Can anyone suggest how to solve this problem?

From the database.yml which you have shared and the logs posted it seems like it is unable to find the path where the database is located.
Change the database.yml to
default: &default
adapter: mysql2
pool: 5
encoding: utf8
min_messages: warning
timeout: 5000
username: root
password:
database: heart_development
host: localhost
Specified host for which the database has to connect to.

I commented environmental variable DATABASE_URL in my .env file. Also I configured my database.yml in the next way:
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: heart_development
pool: 5
username: root
password:
host: localhost
port: 3306
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: heart_test
pool: 5
username: root
password:
host: localhost
port: 3306
production:
adapter: mysql2
encoding: utf8cm
reconnect: false
database: heart_production
pool: 5
username: root
password:
host: localhost
port: 3306

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

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

"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"

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