MySQL中使用CONCAT函数后,一段时间后结果变为null

3

我正在将大数据放入表格中的LONGBLOB字段中,但随着表格的增长,该字段变为空。代码:

mysql_query ('CREATE TABLE IF NOT EXISTS testtable (content LONGBLOB NOT NULL) ENGINE = MyISAM');
mysql_query('TRUNCATE TABLE testtable');
mysql_query('REPLACE INTO testtable VALUES (".")');
$bigData = str_repeat('A', 1024*1024*2); // 2 MB!
foreach (str_split($bigData, 1024*64) as $item)
{
    mysql_query ('UPDATE testtable SET content = CONCAT(content, "'.mysql_real_escape_string($item).'")');
    $rec = mysql_fetch_row(mysql_query ('SELECT content FROM testtable'));
    echo 'Size of the content: '.strlen($rec[0]).'<br>';
}

输出结果:
Size of the content: 65537
Size of the content: 131073
Size of the content: 196609
Size of the content: 262145
Size of the content: 327681
Size of the content: 393217
Size of the content: 458753
Size of the content: 524289
Size of the content: 589825
Size of the content: 655361
Size of the content: 720897
Size of the content: 786433
Size of the content: 851969
Size of the content: 917505
Size of the content: 983041
Size of the content: 0
Size of the content: 65536
Size of the content: 131072
Size of the content: 196608

发生了什么?LONGBLOB应该可以支持更多的数据。


1
如果您想知道数据库中字段的长度,请使用 select length(content) from testtable - Gordon Linoff
1个回答

2

增加max_allowed_packet的大小。

看起来它在1MB时失败了,根据https://dev.mysql.com/doc/refman/5.5/en/packet-too-large.html,默认的最大数据包大小是1MB:

服务器的默认max_allowed_packet值为1MB。如果服务器需要处理大查询,您可以将其增加。

my.cnf文件中设置该值,例如:

[mysqld]
max_allowed_packet=16M

在PHP中

如果您无法访问MySQL配置文件,可以尝试通过查询进行设置(注意:我没有检查过这是否有效)。

$db->query( 'SET @@global.max_allowed_packet = 16777216' );

实际上,这个 CONCAT 方法只是为了避免这个错误,例如我无法一次性插入这 2MB 的数据,但是分开插入现在也失败了,*哭泣。 - John Smith
如果我无法编辑服务器设置,是否有可能插入那些大数据? - John Smith
1
@JohnSmith 尝试根据我的更新通过PHP更改设置 - 这可能会起作用。如果不行,你可能需要将数据存储为1MB的块,并在从数据库中检索后重新组合它们。 - Eborbob

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接