【问题标题】:While using ActiveRecord why is Mysql2::Error raised and not an Activerecord error?在使用 ActiveRecord 时,为什么会引发 Mysql2::Error 而不是 Activerecord 错误?
【发布时间】:2012-09-06 09:41:18
【问题描述】:

我正在使用 ActiveRecord 在 thor 脚本中创建与数据库的连接。我在这种情况下使用的适配器是 mysql2。我想在我的代码中添加一些救援块,以便向用户提供一些特定的错误消息。

当我关闭本地主机上的 mysql 服务器并尝试连接(使用建立连接)时,我收到以下错误(如预期的那样)

Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) (Mysql2::Error)

为什么我没有收到 ActiveRecord::ConnectionNotEstablished 错误而不是 Mysql2::Error?我问的原因是 Mysql2 没有特定的错误类型(基于https://github.com/brianmario/mysql2/blob/master/lib/mysql2/error.rb)而活动记录有(http://api.rubyonrails.org/files/activerecord/lib/active_record/errors_rb.html

在 mysql2 0.3.11 中使用 ruby​​ 1.9.3 和 activerecord 3.2.8

谢谢!

【问题讨论】:

    标签: ruby activerecord mysql2


    【解决方案1】:

    找mysql的socket好像有问题

    这个链接可以帮助你,

    Ruby on Rails 3 Can't connect to local MySQL server through socket '/tmp/mysql.sock' on OSX

    【讨论】:

    • 对不起,您可能误解了这个问题。发生错误是因为我有意关闭了服务器。我要问的是,当我使用 ActiveRecord 建立连接时,为什么错误不是 ActiveRecord 错误 而是 Mysql2 错误。
    猜你喜欢
    • 2018-08-05
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多