anusuya
anusuya

Reputation: 653

how to check the mysql connectivity in ruby on rails

In my rails app i need to write a ruby script which the admins use to check the status of the application. i need to check the connectivity of mysql connection, like the app is connected to the DB or not.. How do i do this . this ruby script i ll place in the script directory.. Thanks in advance.

Upvotes: 0

Views: 4717

Answers (3)

Thomas Chafiol
Thomas Chafiol

Reputation: 2613

Maybe this could help:

ActiveRecord::Base.connected?

Also you can get the current configuration like this:

ActiveRecord::Base.connection_config()

All the related commands are available here

Upvotes: 0

road242
road242

Reputation: 2532

(1) You can try to establish a manual connection by using Mysql.real_connect:

Mysql.real_connect("localhost", "testuser", "testpass", "test")

See this link for further information.

(2) Another possibility (maybe the better one) is using ActiveRecord directly:

ActiveRecord::Base.establish_connection(
  :adapter  => "mysql",
  :host     => "localhost",
  :username => "myuser",
  :password => "mypass",
  :database => "somedatabase"
)

Upvotes: 1

Veger
Veger

Reputation: 37905

You could use the connected? method to see whether an ActiveRecord has a connection to the database.

Upvotes: 3

Related Questions