2 hangxiang147 hangxiang147 于 2015.07.27 23:30 提问

windows环境下,ruby 连接mysql 报错 5C

require "D:/ruby-mysql/ruby-mysql-0.2.6/mysql"
begin
#尝试连接mysql数据库
my = Mysql.connect("localhost","root","123456","test")
#连接成功,显示Mysql数据库版本
puts my.get_server_info
rescue Mysql::Error => err
#如果失败,显示错误信息
puts "错误代码:#{err.errno}"
puts "错误信息:#{err.error}"
ensure
my.close if my
end

运行以上代码,报这样的错误:
D:/ruby-mysql/ruby-mysql-0.2.6/mysql.rb:1019:in read': Packets out of order: 0<> (RuntimeError)
from D:/ruby-mysql/ruby-mysql-0.2.6/mysql.rb:444:in
read'
from D:/ruby-mysql/ruby-mysql-0.2.6/mysql.rb:110:in real_connect'
from D:/ruby-mysql/ruby-mysql-0.2.6/mysql.rb:91:in
initialize'
from D:/ruby-mysql/ruby-mysql-0.2.6/mysql.rb:1085:in new'
from D:/ruby-mysql/ruby-mysql-0.2.6/mysql.rb:1085:in
real_connect'
from D:/软件/eclipse-standard-kepler-SR2-win32/hello/testDb.rb:3:in `'

项目要用到ruby,新人,求大神帮忙,搞了一天了,没连上

3个回答

caozhy
caozhy   Ds   Rxr 2015.07.27 23:32

目测是你的musql端口没有指定对,或者mysql没有启动正确。或者被防火墙拦住了。

save4me
save4me   Ds   Rxr 2015.07.28 12:47

参考brianmario/mysql2,你的MySQL的版本是多少?从MySQL 5.6.5开始,默认启用secure_auth

Secure auth

Starting wih MySQL 5.6.5, secure_auth is enabled by default on servers (it was disabled by default prior to this). When secure_auth is enabled, the server will refuse a connection if the account password is stored in old pre-MySQL 4.1 format. The MySQL 5.6.5 client library may also refuse to attempt a connection if provided an older format password. To bypass this restriction in the client, pass the option :secure_auth => false to Mysql2::Client.new(). If using ActiveRecord, your database.yml might look something like this:

development:
adapter: mysql2
encoding: utf8
database: my_db_name
username: root
password: my_password
host: 127.0.0.1
port: 3306
secure_auth: false

oyljerry
oyljerry   Ds   Rxr 2015.07.28 12:50

不要用mysql ,用mysql2 adapter 试试.

 gem install mysql2
 require "rubygems"
require "active_record"
class Source < ActiveRecord::Base
    set_table_name "sources"
    ActiveRecord::Base.establish_connection(
        :adapter => "mysql2",
        :host => "localhost",
        :username => "test",
        :password => "test",
        :port => 3306,
        :database => "TestDB",
        :socket => "mysql"
    )
end
source = Source.find(:first, :conditions => [ "source_id = ?", 2 ])
puts source.source_name
hangxiang147
hangxiang147 回复oyljerry: 我估计我下的mysql2不对,我在网上找不到资源,麻烦你给我个链接,谢谢啊
2 年多之前 回复
hangxiang147
hangxiang147 我估计我下的mysql2不对,我在网上找不到资源,麻烦你给我个链接,谢谢啊
2 年多之前 回复
oyljerry
oyljerry 回复hangxiang147: gem install了?
2 年多之前 回复
hangxiang147
hangxiang147 回复hangxiang147: 说漏了,require "active_record",是这句话,报错
2 年多之前 回复
hangxiang147
hangxiang147 需要安装rail吗,我只装了ruby,将你的代码复制上去时,报‘in `require': cannot load such file -- active_record (LoadError)’,找不到这个文件,是不是还需要下载什么啊,我只是下载了mysql2,我只是单纯解压了,不知道放到哪里
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片