MySQL Ruby、Rails:mysql2 gem,有人使用这个 gem 吗?它稳定吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3001243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby, Rails: mysql2 gem, does somebody use this gem? Is it stable?
提问by petRUShka
I found mysql2 gem. It works fine with ruby 1.9 and utf8 encoding (as opposed to mysql gem).
我找到了mysql2 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? Is it stable?
但我有疑问。有人在生产中使用这个 gem 吗?性能测试呢?它比mysql gem 快吗?它稳定吗?
回答by brianmario
mysql2
is meant as a more modern replacementfor the existing mysql
gem, which has been stale for a while now. 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 muchslower).
mysql2
旨在作为现有gem的更现代替代品,该mysql
gem 已经陈旧了一段时间。我还听说作者不再支持它,而是建议每个人使用他的纯 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. 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. So it's best to avoid it and do as much in pure C as you can.
mysql
gem的第一个问题是它不会在 C 中进行任何类型转换,它会返回 ruby 字符串,然后您必须将其转换为正确的 ruby 类型。在纯 ruby 中这样做非常慢,并且会在堆上创建最初不需要存在的对象。众所周知,Ruby 的 GC 是其缓慢的主要原因。所以最好避免它并尽可能多地使用纯 C 语言。
Second is that it blocks the entireruby VM while connecting, sending queries and waiting for responses, and even closing the connection. mysqlplus
definitely helps with this issue, but only for sending queries as far as I know.
其次,它会在连接、发送查询和等待响应,甚至关闭连接时阻塞整个ruby VM。mysqlplus
绝对有助于解决这个问题,但仅用于发送我所知道的查询。
mysql2
aims to solve these problems while keeping the API extremely simple. Eric Wong
(author of Unicorn) has contributed some awesome patches that make nearly everything non-blocking and/or release the GVL in Ruby. The Mysql2::Result
class implements Enumerable so if you know how to use an Array, you know how to use it.
mysql2
旨在解决这些问题,同时保持 API 极其简单。Eric Wong
(Unicorn 的作者)贡献了一些很棒的补丁,使几乎所有内容都成为非阻塞和/或在 Ruby 中发布 GVL。在Mysql2::Result
类实现可枚举,所以如果你知道如何使用数组,你知道如何使用它。
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. Some of its techniques and optimizations are also going to be brought into DataObjects' do_mysql
driver soon as well.
我还在讨论Yehuda
它是 Rails 3 发布时的推荐/默认设置。它的一些技术和优化也将do_mysql
很快被引入到 DataObjects 的驱动程序中。
The ActiveRecord driver should be pretty solid at the moment. All you should need to do is have the gem installed, and change your adapter name in database.yml to mysql2
.
ActiveRecord 驱动程序目前应该非常可靠。您需要做的就是安装 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 ;)
如果您有兴趣使用它,请尝试一下。如果您发现任何问题,我会很快推动修复;)
回答by Loren
mysql2 is now the default in rails 3
mysql2 现在是 rails 3 中的默认值
回答by Nick Ragaz
A bit late - but I'm using mysql2 in production on a couple of sites, and have found it very stable since some issues with connections getting closed were resolved a few weeks ago.
有点晚了 - 但我在几个站点的生产中使用 mysql2,并且发现它非常稳定,因为一些连接关闭的问题在几周前得到解决。
回答by aercolino
If anyone wants to use the mysql gem instead of the mysql2 gem in Rails 3.0.0 for Windows, I wrote a short article that explains how to tweak the rails app generator.
如果有人想在 Rails 3.0.0 for Windows 中使用 mysql gem 而不是 mysql2 gem,我写了一篇简短的文章来解释如何调整 rails 应用程序生成器。
回答by Nigel Sheridan-Smith
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
回答by James Adam
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. Otherwise Passenger barfs...
仅供参考,当在 Rails 3.0.11 中使用 mysql2 (0.2.x) 时,我不得不将我的 RubyGems 版本从 1.8.15 降级到 1.8.10。否则乘客会呕吐...