我正在使用Docker来运行我的Ruby应用程序,并使用MySQL作为数据库。我需要等待我的Ruby应用程序,直到MySQL完成加载并且可以建立连接。
我使用了以下代码:
def connect_to_db
begin
puts "Trying to connect to Mysql"
Sequel::Model.db = Sequel.connect( // Connection stuff in here )
rescue Sequel::Error => e
puts "Mysql connection failed #{e.message}: Retrying."
retry
end
end
connect_to_db()它运行一次,然后我得到一个错误- Sequel::DatabaseConnectionError: Mysql2::Error: Unknown MySQL server host (25) -它没有进入rescue块,也不会重试。
我尝试过rescue Sequel::DatabaseConnectionError,但结果是一样的。
我需要在这里拯救什么?
发布于 2015-11-11 15:04:17
让它与db.test_connection一起工作
def connect_to_db_with_mysql_driver
begin
puts "Trying to connect to Mysql"
db = Sequel.connect(
// Connection stuff
)
if db.test_connection == true
Sequel::Model.db = db
else
raise "Mysql connection failed."
end
rescue => ex
puts ex
puts "Retrying..."
retry
end
endhttps://stackoverflow.com/questions/33644234
复制相似问题