Connection problems with Rails and MySQL - mysql

So I was making some tests here on Rails and for first time I decide to use MySQL, I've setting up the gem mysql2 and the mysql server.
When I try to access the database I receive this:
ActiveRecord::ConnectionNotEstablished
The strange thing is I'm able to run the rake db:create and the migrations without any problems.
thanks in advance,
Renan

ActiveRecord::ConnectionNotEstablished
Raised when connection to the database could not been established (for example when connection= is given a nil object).
Try checking your db configurations in the config/database.yml file if everything is correct or not!!

Related

Rails integration test: Mysql2::Error: Can't connect to MySQL server

Lately I am working on my beautiful tests, but suddenly, after a 2 days rest, I ran again my integration tests (rake test:integration) and they don't work anymore. I really didn't do anything.
What comes is the following error:
Mysql2::Error: Can't connect to MySQL server on '192.168.0.2'
The nice thing is that the server runs fine. It is just the integration test that is broken. And they use the same mysql database.
Any idea on how to solve the problem?
Luca
check your database.yml settings about test environment and then make in terminal
rake db:test:load
Check the DB using mysql workbench if you not find errors as user permissions... delete and create again DB for app. rake db:drop && rake db:setup

Force to reconnect MySQL in Rails

How to force MySQL reconnect at my will in Rails application? I would like to do this either periodically or on DB exceptions like "MySQL server has gone away".
I found ActiveRecord::Base.remove_connection but as it is written, it should be called for some model, not the whole application.
It's a huge pain to restart the Rails console when I'm running it via Heroku with a bunch of objects in variables and then lose my database connection.
The following is code I would not consider "good" to put in your actual application but it temporarily gets over the oft encountered Mysql2::Error: closed MySQL connection in a console:
ActiveRecord::Base.connection.reconnect!
How about using reconnect = true in your database.yml as described here?

Rails with MySQL v.4.1

Is there any chance to make Rails application connect to old MySQL v4.1.20 server?
rails db
command works fine, but I cannot run the application.
When I generate new app with
rails new app_name -d mysql
I see the following in database.yml file:
MySQL. Versions 4.1 and 5.0 are recommended.
Which should mean I am able to use it.
However Rails Active Record uses MySQL v.5.0.2+ feature (https://github.com/rails/rails/blob/4-0-stable/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb):
# Make MySQL reject illegal values rather than truncating or blanking them, see
# http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_strict_all_tables
# If the user has provided another value for sql_mode, don't replace it.
if strict_mode? && !variables.has_key?(:sql_mode)
variables[:sql_mode] = 'STRICT_ALL_TABLES'
end
Unfortunately I have nothing to do with old MySQL server version. I have to use it.
Adding
strict: false
to the connector settings in database.yml solved the problem.

closed MySQL connection in Rake task

I have a problem similar to the one described in Resque is returning Mysql2::Error: closed MySQL connection: SHOW FIELDS FROM `users`.
In my case, I see it during a long-running Rake task (3+ hours). Basically the task runs along importing data from csvs, then suddenly starts spitting "closed MySQL connection" errors. This only happens in our staging env with a remote MySql, can't repro locally.
So my question is, why can the Rails app (using the same connection properties) run for days without issue, but my data import task bombs after a few hours?
Your connections are timing out. This happens all the time on long running rake tasks with the mysql driver. Try setting the reconnect flag in your database.yml
http://guides.rubyonrails.org/2_3_release_notes.html#reconnecting-mysql-connections
Basically just add:
reconnect: true
I am surprised this is not documented more as I run into it all the time. If that doesnt work, try explicitly calling
ActiveRecord::ConnectionAdapters::ConnectionHandler#verify_active_connections!

ConnectionNotEstablished error when loading any page

I am getting this ConnectionNotEstablished error whenever I try to access any page on my rails app. The error only started to occur when I installed phpMyAdmin and changed my database's password. I updated my database.yml file to reflect the change but am still getting this error.
Does anyone have more details about what this error is or how I can go about fixing it?
I am running Ubuntu 10.10, Ruby 1.9.2, Rails 3.0.1, and connecting with the mysql2 gem.
Make sure that the database.yml file is properly formed