简体   繁体   English

Ruby,Rails:mysql2 gem,有人使用这个gem吗? 稳定吗?

[英]Ruby, Rails: mysql2 gem, does somebody use this gem? Is it stable?

I found mysql2 gem . 我发现mysql2 gem It works fine with ruby 1.9 and utf8 encoding (as opposed to mysql gem). 它与ruby 1.9和utf8编码(与mysql gem相对)工作正常。

But I have doubts. 但是我有疑问。 Does somebody use this gem in production? 有人在生产中使用这种宝石吗? What about the performance test? 性能测试呢? Is it faster than mysql gem? 它比mysql gem快吗? Is it stable? 稳定吗?

mysql2 is meant as a more modern replacement for the existing mysql gem, which has been stale for a while now. mysql2是对现有mysql gem的更现代的替代 ,而mysql gem已经过时了。 I also heard that the author isn't supporting it anymore and instead recommends everyone use his pure-ruby version since it's compatible with more Ruby implementations (but is much slower). 我还听说笔者是不支持它了,而是建议大家用他的纯Ruby版本,因为它有更多的Ruby实现兼容(但慢)。

The first issue with the mysql gem is it doesn't do any type casting in C, it gives you back ruby strings which you then have to convert into proper ruby types. mysql gem的第一个问题是它在C中不进行任何类型转换,它为您提供了红宝石字符串,然后您必须将其转换为正确的红宝石类型。 Doing that in pure-ruby is extremely slow, and creates objects on the heap that never needed to existing in the first place. 在纯红宝石中执行此操作非常慢,并且会在堆上创建最初不需要的对象。 And as we all know, Ruby's GC is it's primary reason for it's slowness. 众所周知,Ruby的GC是它运行缓慢的主要原因。 So it's best to avoid it and do as much in pure C as you can. 因此,最好避免使用它,并尽可能在纯C语言中执行更多操作。

Second is that it blocks the entire ruby VM while connecting, sending queries and waiting for responses, and even closing the connection. 其次,它在连接,发送查询和等待响应甚至关闭连接时会阻塞整个 ruby VM。 mysqlplus definitely helps with this issue, but only for sending queries as far as I know. mysqlplus绝对可以解决此问题,但据我所知仅用于发送查询。

mysql2 aims to solve these problems while keeping the API extremely simple. mysql2旨在解决这些问题,同时保持API非常简单。 Eric Wong (author of Unicorn) has contributed some awesome patches that make nearly everything non-blocking and/or release the GVL in Ruby. Eric Wong (《独角兽》的作者)贡献了一些很棒的补丁程序,这些补丁程序几乎使所有内容都不阻塞和/或在Ruby中发布了GVL。 The Mysql2::Result class implements Enumerable so if you know how to use an Array, you know how to use it. Mysql2::Result类实现Enumerable,因此,如果您知道如何使用数组,就知道如何使用它。

I'm only aware of a few people using it in production right now but it is being evaluated at Twitter, WorkingPoint and UserVoice too. 我现在只知道有几个人在生产中使用它,但是它也在Twitter,WorkingPoint和UserVoice进行评估。

I'm also in talks with Yehuda about it being the recommended/default for Rails 3 when it ships. 我还与Yehuda进行了对话,讨论它是Rails 3出厂时的推荐/默认设置。 Some of its techniques and optimizations are also going to be brought into DataObjects' do_mysql driver soon as well. 它的一些技术和优化也将很快引入到DataObjects的do_mysql驱动程序中。

The ActiveRecord driver should be pretty solid at the moment. ActiveRecord驱动程序目前应该非常稳定。 All you should need to do is have the gem installed, and change your adapter name in database.yml to mysql2 . 您需要做的就是安装gem,然后将database.yml中的适配器名称更改为mysql2

If you're interested in using it, give it a try. 如果您有兴趣使用它,请尝试一下。 I'm quick to push fixes if you find any issues ;) 如果您发现任何问题,我会迅速进行修复;)

mysql2现在是Rails 3中的默认设置

有点晚了-但是我在几个站点的生产环境中使用了mysql2,并且发现它非常稳定,因为几周前解决了一些关闭连接的问题。

如果有人想在Windows的Rails 3.0.0中使用mysql gem而不是mysql2 gem,我写了一篇简短的文章,解释了如何调整Rails应用程序生成器

Make sure you convert your 'latin1' characters to 'utf8', following the answer here: 确保按照以下答案将“ latin1”字符转换为“ utf8”:

UTF8 MySQL problems on Rails - encoding issues with utf8_general_ci Rails上的UTF8 MySQL问题-utf8_general_ci的编码问题

FYI, When using mysql2 (0.2.x) with Rails 3.0.11 I had to downgrade my version of RubyGems from 1.8.15 to 1.8.10. 仅供参考,当在Rails 3.0.11中使用mysql2(0.2.x)时,我不得不将RubyGems的版本从1.8.15降级到1.8.10。 Otherwise Passenger barfs... 否则,乘客的...叫...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM