开发者

how to check the mysql connectivity in ruby on rails

开发者 https://www.devze.com 2022-12-23 11:36 出处:网络
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, l开发者_JAVA百科ike the app is connecte

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, l开发者_JAVA百科ike 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.


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消