简体   繁体   中英

When using rails/activerecord to talk to a MySQL database, can I set max_allowed_packet?

MySQL has a setting called 'max_allowed_packet' which is set separately for clients and servers. On the server side it's in a config file, is it possible to set this to a non-default value on the client side when the client is rails ActiveRecord?

From the documnentation

On the client side, max_allowed_packet has a default of 1GB.

So, you just have to change it on the server side. Depending what version of MySQL you are using, you can do this on the fly on a global basis by running

SET GLOBAL max_allowed_packet=16777216

or

SET max_allowed_packet=16777216

where I assumed a 16MB max size. As you noted, you can also set the global variable in your my.cnf .

Currently, you cannot change this on a per-session basis.

mysql> SET SESSION max_allowed_packet=102400000; 

ERROR 1621 (HY000): SESSION variable 'max_allowed_packet' is read-only. Use SET GLOBAL to assign the value

我不知道是否可以提供帮助,但您可以在运行时设置mysql变量。

ActiveRecord::Base.connection.execute("set global max_allowed_packet=1000000000;")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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