使用ruby连接到mysql时出现问题

时间:2020-12-01 21:52:41
require 'rubygems'
require 'mysql'
db = Mysql.connect('localhost', 'root', '', 'mohit')
//db.rb:4: undefined method `connect' for Mysql:Class (NoMethodError)
//undefined method `real_connect' for Mysql:Class (NoMethodError)
db.query("CREATE TABLE people ( id integer primary key, name varchar(50), age integer)")
db.query("INSERT INTO people (name, age) VALUES('Chris', 25)")
begin
 query = db.query('SELECT * FROM people')
 puts "There were #{query.num_rows} rows returned"
  query.each_hash do |h| 
     puts h.inspect
end
rescue
 puts db.errno
 puts db.error 
end

error i am geting is:

我得到的错误是:

    undefined method `connect' for Mysql:Class (NoMethodError)
                    OR 
   undefined method `real_connect' for Mysql:Class (NoMethodError)

EDIT return value of Mysql.methods

编辑Mysql.methods的返回值

      ["private_class_method", "inspect", "name", "tap", "clone", "public_methods", "object_id", "__send__", "method_defined?", "instance_variable_defined?", "equal?", "freeze", "extend", "send", "const_defined?", "methods", "ancestors", "module_eval", "instance_method", "hash", "autoload?", "dup", "to_enum", "instance_methods", "public_method_defined?", "instance_variables", "class_variable_defined?", "eql?", "constants", "id", "instance_eval", "singleton_methods", "module_exec", "const_missing", "taint", "instance_variable_get", "frozen?", "enum_for", "private_method_defined?", "public_instance_methods", "display", "instance_of?", "superclass", "method", "to_a", "included_modules", "const_get", "instance_exec", "type", "<", "protected_methods", "<=>", "class_eval", "==", "class_variables", ">", "===", "instance_variable_set", "protected_instance_methods", "protected_method_defined?", "respond_to?", "kind_of?", ">=", "public_class_method", "to_s", "<=", "const_set", "allocate", "class", "new", "private_methods", "=~", "tainted?", "__id__", "class_exec", "autoload", "untaint", "nil?", "private_instance_methods", "include?", "is_a?"]

return value of Mysql.methods(false)

的返回值Mysql.methods(假)

is []... blank array

[]…空数组

EDIT2

EDIT2

mysql.rb file

mysql。rb文件

 # support multiple ruby version (fat binaries under windows)
 begin
  require 'mysql_api'
  rescue LoadError
   if RUBY_PLATFORM =~ /mingw|mswin/ then
RUBY_VERSION =~ /(\d+.\d+)/
require "#{$1}/mysql_api"
 end
 end

 # define version string to be used internally for the Gem by Hoe.
  class Mysql
    module GemVersion
     VERSION = '2.8.1'
  end
  end

2 个解决方案

#1


7  

I had this same problem and solved this way:

我遇到了同样的问题,用这种方法解决了:

  1. make sure you have installed only the gem ruby-mysql and not the gem mysql. For me, now:

    确保您只安装了gem ruby-mysql,而没有安装gem mysql。对我来说,现在:

    $ gem list --local | grep mysql

    $ gem列表——本地| grep mysql

    ruby-mysql (2.9.2)

    ruby-mysql(2.9.2)

  2. If that is not the case, uninstall

    如果不是这样,卸载

    $ sudo gem uninstall mysql

    $ sudo gem卸载mysql

(I uninstalled every gem with mysql in its name) and then reinstalled ruby-mysql.

(我以mysql的名义卸载了每个gem),然后重新安装了ruby-mysql。

In my case, because I have mysql installed in a usb disk the installation command was:

在我的例子中,因为我在usb磁盘上安装了mysql,所以安装命令是:

sudo env ARCHFLAGS="-arch i386" gem install ruby-mysql   --  

--with-mysql-config=/Volumes/usb/opt/bin/osx/mysql/bin/mysql_config

--with-mysql-lib=/Volumes/usb/opt/bin/osx/mysql/lib/      

--with-mysql-dir=/Volumes/usb/opt/bin/osx/mysql

(and I was using the 32bits binary for MacOs, don't know if that applies for you)

(我在MacOs中使用了32位二进制,不知道你们是否适用)

Finally, my ruby test program was

最后,我的ruby测试程序是。

require 'rubygems'
require 'mysql' 


dbh = Mysql.real_connect('localhost', 'root', 'your password', 'TEST')


res =  dbh.query("select * from Persons;");

puts res.class

res.each do |row|
    puts row.join(" ")
end

#2


1  

Short answer:

简短的回答:

  1. Remove mysql-ruby
  2. 删除mysql-ruby
  3. Rebuild mysql-ruby
  4. 重建mysql-ruby
  5. Reinstall mysql-ruby.
  6. 重新安装mysql-ruby。

Alternative answer

选择答案

  1. Remove mysql-ruby
  2. 删除mysql-ruby
  3. Install ruby-mysql
    The pure ruby MySQL protocol client.
  4. 安装ruby- MySQL协议客户端。

Longer Explanation:

更详细的解释:

This just happened to me. My 2.8.1 mysql-ruby bindings had been built against libmysqlclient.so.15, and worked fine until I upgraded my MySQL installation and replaced that client library with .so.16. Rebuild resolved this issue.

这就发生在我身上。我的2.8.1 sql-ruby绑定是针对libmysqlclient构建的。直到我升级了MySQL安装,并替换了那个客户端库。重建解决这个问题。

The 3rd-party gem you used (I used it, too) introduces faulty logic in the mysql.rb file it supplies to trap an error on Windows systems. Notice that in the excerpt you posted, that this mysql.rb file does not re-raise the LoadError on non-Windows platforms. Bummer.

您使用的第三方gem(我也使用了它)在mysql中引入了错误的逻辑。rb文件它提供了在Windows系统上捕获错误。注意,在你发布的摘录中,这是mysql。rb文件不会在非windows平台上重新引发LoadError。游手好闲的人。

Edit

编辑

I contacted the gemspec author, and he has corrected the error! (2010-05-25) With luck no one else will be baffled by this silent failure.

我联系了gemspec的作者,他纠正了错误!如果运气好的话,没有人会对这种无声的失败感到困惑。

#1


7  

I had this same problem and solved this way:

我遇到了同样的问题,用这种方法解决了:

  1. make sure you have installed only the gem ruby-mysql and not the gem mysql. For me, now:

    确保您只安装了gem ruby-mysql,而没有安装gem mysql。对我来说,现在:

    $ gem list --local | grep mysql

    $ gem列表——本地| grep mysql

    ruby-mysql (2.9.2)

    ruby-mysql(2.9.2)

  2. If that is not the case, uninstall

    如果不是这样,卸载

    $ sudo gem uninstall mysql

    $ sudo gem卸载mysql

(I uninstalled every gem with mysql in its name) and then reinstalled ruby-mysql.

(我以mysql的名义卸载了每个gem),然后重新安装了ruby-mysql。

In my case, because I have mysql installed in a usb disk the installation command was:

在我的例子中,因为我在usb磁盘上安装了mysql,所以安装命令是:

sudo env ARCHFLAGS="-arch i386" gem install ruby-mysql   --  

--with-mysql-config=/Volumes/usb/opt/bin/osx/mysql/bin/mysql_config

--with-mysql-lib=/Volumes/usb/opt/bin/osx/mysql/lib/      

--with-mysql-dir=/Volumes/usb/opt/bin/osx/mysql

(and I was using the 32bits binary for MacOs, don't know if that applies for you)

(我在MacOs中使用了32位二进制,不知道你们是否适用)

Finally, my ruby test program was

最后,我的ruby测试程序是。

require 'rubygems'
require 'mysql' 


dbh = Mysql.real_connect('localhost', 'root', 'your password', 'TEST')


res =  dbh.query("select * from Persons;");

puts res.class

res.each do |row|
    puts row.join(" ")
end

#2


1  

Short answer:

简短的回答:

  1. Remove mysql-ruby
  2. 删除mysql-ruby
  3. Rebuild mysql-ruby
  4. 重建mysql-ruby
  5. Reinstall mysql-ruby.
  6. 重新安装mysql-ruby。

Alternative answer

选择答案

  1. Remove mysql-ruby
  2. 删除mysql-ruby
  3. Install ruby-mysql
    The pure ruby MySQL protocol client.
  4. 安装ruby- MySQL协议客户端。

Longer Explanation:

更详细的解释:

This just happened to me. My 2.8.1 mysql-ruby bindings had been built against libmysqlclient.so.15, and worked fine until I upgraded my MySQL installation and replaced that client library with .so.16. Rebuild resolved this issue.

这就发生在我身上。我的2.8.1 sql-ruby绑定是针对libmysqlclient构建的。直到我升级了MySQL安装,并替换了那个客户端库。重建解决这个问题。

The 3rd-party gem you used (I used it, too) introduces faulty logic in the mysql.rb file it supplies to trap an error on Windows systems. Notice that in the excerpt you posted, that this mysql.rb file does not re-raise the LoadError on non-Windows platforms. Bummer.

您使用的第三方gem(我也使用了它)在mysql中引入了错误的逻辑。rb文件它提供了在Windows系统上捕获错误。注意,在你发布的摘录中,这是mysql。rb文件不会在非windows平台上重新引发LoadError。游手好闲的人。

Edit

编辑

I contacted the gemspec author, and he has corrected the error! (2010-05-25) With luck no one else will be baffled by this silent failure.

我联系了gemspec的作者,他纠正了错误!如果运气好的话,没有人会对这种无声的失败感到困惑。