views:

17

answers:

2

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.

A: 

(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"
)
hkda150
+2  A: 

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

Veger