Ruby on Rails MySQL #08S01握手错误 - 降级 MySQL?

2024-01-12

我们最近在 Ubuntu 10.04LTS 服务器上从 MySQL 5.1.41 升级到 5.1.61。我们有一个古老的 RoR Web 应用程序,现在出现了错误的握手错误:

Mysql::Error in MainController#index

#08S01Bad handshake

/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/vendor/mysql.rb:523:in `read'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/vendor/mysql.rb:153:in `real_connect'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:389:in `connect'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:152:in `initialize'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:82:in `new'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:82:in `mysql_connection'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:262:in `send'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:262:in `connection_without_query_cache='
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/query_cache.rb:54:in `connection='
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:230:in `retrieve_connection'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:78:in `connection'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:763:in `columns'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:2060:in `attributes_from_column_definition_without_lock'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/locking/optimistic.rb:45:in `attributes_from_column_definition'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1502:in `initialize_without_callbacks'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/callbacks.rb:225:in `initialize'
#{RAILS_ROOT}/app/controllers/application.rb:48:in `new'
#{RAILS_ROOT}/app/controllers/application.rb:48:in `log_info'
/usr/local/bin/mongrel_rails:19:in `load'
/usr/local/bin/mongrel_rails:19

我用谷歌搜索并偶然发现http://bugs.ruby-lang.org/issues/5017 http://bugs.ruby-lang.org/issues/5017这告诉我这是一个 Ruby MySQL 扩展错误。我们没有使用 MySQL gem。我们的 Web 应用程序非常古老且脆弱(Ruby v1.8.7、Rails v1.2.3、Mongrel 1.1.5)。我们正在用 Django 重写来替换它,所以我们只需要在接下来的几周内实现此功能,直到我们将其替换为新站点。

我们怎样才能克服这个错误呢?我认为降级到 MySQL 5.1.41 是处理这个问题的最佳方法,然后当我们在几周后开始使用新站点时,我们可以重新升级到 5.1.61。但是,我在降级 mysql 时遇到问题。这是我正在使用的命令:

sudo aptitude install mysql-server-5.1=5.1.41-3ubuntu12.10

然而,这告诉我Unable to find a version "5.1.41-3ubuntu12.10" for the package "mysql-server-5.1"。我试过了sudo aptitude install mysql-server-5.1=5.1.41也,但这也不起作用。如何让 aptitude 安装正确版本的 MySQL?


可以修复数据库名称参数来修复 MySQL gem,而不是降级 MySQL gem"bad handshake"问题。

我找到了这个:https://github.com/rubygems/rubygems/issues/423 https://github.com/rubygems/rubygems/issues/423运作良好。

而不是进行黑客入侵real_connect可以添加"\0" in config/database.yml

production:
  database: "itsalive_production\0"
  adapter: mysql
  host: localhost
  encoding: UTF8
  ...

EDIT
如果您使用该解决方案\0在数据库名称的末尾。你可能会发现这个并自己解决它,但我还是提到它:
(至少在我的 Rails 版本中)
使用数据库字符串\0最后在做时出现问题rake test。首先删除测试数据库,然后复制开发数据库定义,然后使用包含测试数据库名称的 SQL 命令字符串。这将导致error因为\0在字符串的中间。

就我而言,我使用的本地开发数据库不会出现任何问题,因此我不需要\0以那个名字。
这是解决这个问题的替代方法(原始代码在mysql_adapter.rb):

module ActiveRecord
  module ConnectionAdapters
    class MysqlAdapter

      alias_method :old_execute, :execute

      def execute(sql, name = nil) #:nodoc:
        # This is needed because database names can end with "\0" to fix
        # the issue with "handshake" when mysql server is newer than the gem
        # requires. E.g. called when loading the new test db when doing "rake test".
        sql = sql.delete("\0")

        old_execute(sql, name)
      end
    end
  end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Ruby on Rails MySQL #08S01握手错误 - 降级 MySQL? 的相关文章

随机推荐