mysql max_allowed_packet 設置

mysql根據my.cnf中max_allowed_packet的大小來限制接收到的數據包大小。

官網描述,如下圖。 數據包的值範圍爲1k~1G, 只能是1024的倍數,不能整除1024的,會向下取整。 若設置成1025,最終結果爲1024。

 

查看方法:

1.在mysql配置文件中my.cnf中查看。cat my.cnf

my.cnf可通過 mysql --help|grep my.cnf查找,一般在/etc/mysql下。

2.登陸mysql(mysql -h ip -u userName -ppasswd databaseName -A ),通過命令行查看。show variables like "max_allowed_packet";

 此時顯示大小爲16M

 

修改方法:

1.直接在my.cnf中修改。

2.在命令行中用 set global max_allowed_packet = 1;設置。 

max_allowed_packet最小值爲1024, 如設置的值<1024, 則默認爲1024。

執行成功後,退出登陸,再重新登錄。用show variables like "max_allowed_packet"查看即可發現修改成功。

此時大小顯示爲1kb

 

若數據包大小> max_allowed_packet,則會有錯誤信息:

ERROR 1153 (08S01): Got a packet bigger than 'max_allowed_packet' bytes

測試用例

 將max_allowed_packet設置爲1024即1kb

建表語句:

CREATE TABLE `max_allowed_test` (
  `msg` varchar(256) NOT NULL DEFAULT '',
  `info` varchar(1024) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8 

插入語句:

insert into max_allowed_test values("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");

數據包的大小,是指什麼呢? sql語句的長度,還是插入數據的大小。下面來驗證一下。

插入的數量大小剛好是1025,即xxx+yyy的字符個數=1025;

結果:

成功了。

 

原因:

數據包其實是網絡包緩衝區的大小。包會進行層層封裝,並且還會進行壓縮,並不是單純的只sql語句或者插入語句的大小,詳情參考:http://blog.itpub.net/7728585/viewspace-2138631/

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章