Showing posts with label Connections. Show all posts
Showing posts with label Connections. Show all posts

Friday, January 18, 2008

Rails multi-database connections resolutions

当Rails项目中数据处理量比较大时就需要将数据库的读写操作分在不同的database server上,在网上GOOGLE了一下,主要通过Dr.Nic的magic_multi_connections来处理,需要安装这个GEM包,然后修改config/database.yml,config/environment.rb文件,具体可参考Dr.Nic的说明,还是比较方便的.
还有个是叫active_delegate的Rails plugin,使用比上面的gem更方便,但是实际使用后发现没有Dr.Nic的方法好用.

Update: Nic的方法使用起来修改配置的地方还是比较多,实际使用过程中发现还是有点问题的.建议还是按rails本身提供的一个多数据库连接机制:
Connection to multiple databases in different models
Connections are usually created through ActiveRecord::Base.establish_connection and retrieved by ActiveRecord::Base.connection. All classes inheriting from ActiveRecord::Base will use this connection. But you can also set a class-specific connection. For example, if Course is a ActiveRecord::Base, but resides in a different database you can just say Course.establish_connection and Course *and all its subclasses* will use this connection instead.

This feature is implemented by keeping a connection pool in ActiveRecord::Base that is a Hash indexed by the class. If a connection is requested, the retrieve_connection method will go up the class-hierarchy until a connection is found in the connection pool.

Saturday, September 29, 2007

[1129]MySQL::Error: Unblock with 'mysqladmin flush-hosts'

MYSQL出现此问题的原因是:
Error: Host '***.***.***.***' blocked because of many connection errors. Unblock with 'mysqladmin flush-hosts'
Errno.: 1129
Similar error report has beed dispatched to administrator before.
该IP因为有太多的错误连接已被锁定,请执行 mysqladmin flush-hosts 来解除锁定.

MySQL最大连接数根据my.cnf不同而不同,最小那个配置文件是100,my-large.cnf那个是256个连接,具体可以在mysql shell下用show variables like 'max_connections'; 查看,可以重新设置此数值。
不过一般发生上面的这个错误就不是靠设置这个数值能解决,主要还是程序本身有错误导致错误请求连接太多,导致后面正常请求也无法访问,在解决程序本身错误后还必须在主DB所在主机上执行mysqladmin -p flush-hosts后,才能重新连接上数据库。