mysql2和ruby的问题

时间:2012-03-02 10:37:35

标签: ruby ubuntu rubygems

尝试在ubuntu上安装gem mysql2,我已经尝试了所有的建议,但我无法让它运行。这是我的申请中的错误。

./bla.rb:65:in `post_init': undefined method `query' for nil:NilClass (NoMethodError)
    from /var/lib/gems/1.8/gems/eventmachine-0.12.10/lib/em/timers.rb:51:in `call'
    from /var/lib/gems/1.8/gems/eventmachine-0.12.10/lib/em/timers.rb:51:in `fire'
    from /var/lib/gems/1.8/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `call'
    from /var/lib/gems/1.8/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine'
    from /var/lib/gems/1.8/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run'
    from ./bla.rb:234:in `start_server'
    from ./bin/minibardaemon:15
    from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/application.rb:254:in `call'
    from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/application.rb:254:in `start_proc'
    from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/application.rb:263:in `call'
    from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/application.rb:263:in `start_proc'
    from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/application.rb:295:in `start'
    from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/controller.rb:73:in `run'
    from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons.rb:197:in `run_proc'
    from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/cmdline.rb:109:in `call'
    from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons/cmdline.rb:109:in `catch_exceptions'
    from /var/lib/gems/1.8/gems/daemons-1.1.8/lib/daemons.rb:196:in `run_proc'

我已经安装了所有推荐的软件包并通过gem安装了mysql2但仍然没有运气。

libmysqlclient-dev

已安装。

我在Ubuntu上。

# gem -v
1.3.7

# ruby -v
ruby 1.8.7 (2010-08-16 patchlevel 302) [x86_64-linux]

安装宝石

# gem install mysql
Building native extensions.  This could take a while...
Successfully installed mysql-2.8.1
1 gem installed
Installing ri documentation for mysql-2.8.1...
Installing RDoc documentation for mysql-2.8.1...



# gem install mysql2
Building native extensions.  This could take a while...
Successfully installed mysql2-0.3.11
1 gem installed
Installing ri documentation for mysql2-0.3.11...
Installing RDoc documentation for mysql2-0.3.11...


  def create_mysql2
    begin
      mysql2 = Mysql2::EM::Client.new(
        :username => DBUSER,
        :password => DBPASS,
        :host     => DBHOST,
        :port     => DBPORT,
        :socket   => DBSOCKET,
        :database => DBNAME
      )
      return mysql2
    rescue Mysql2::Error => exception
      $stderr.puts "Mysql Error: #{ exception.message }"
      EventMachine::stop_event_loop
    end
  end

...

begin
    mysql2 = create_mysql2
rescue Exception => ex
    p ex
end

    # query the db every x seconds.
    EventMachine::add_periodic_timer(QUERY_INTERVAL) do
      defer1 = mysql2.query "SELECT * FROM table LIMIT #{QUERY_LIMIT}"

1 个答案:

答案 0 :(得分:1)

看起来并不像mysql2 gem问题,请问您显示第65行bla.rb文件?可能还有一些围绕它的线条。

实际上看起来你的mysql2客户端由于某种原因未初始化。 我建议检查这样的事情是否有效:

require 'mysql2'
mysql_client = Mysql2::Client.new(:host => "localhost", :username => "root")
mysql_client.query('sql .. ')

如果是这样,您的脚本中显然存在问题