我最近偶然发现了这个问题。在我的情况下,服务器的大小
max_allowed_packet为1 MB,我无能为力。我插入的数据刚好超过1
MB。我找到了两个解决方案候选人。
1)首先,使用JDBC。自从 MySQL Connector / J v3.1.9 以来,您可以设置一些参数,这是我在JDBC URL中设置的参数:
附加以下内容:
blobSendChunkSize=50000&useServerPrepStmts=true&emulateUnsupportedPstmts=false&maxAllowedPacket=20000000
产生的JDBC URL:
jdbc:mysql://serverip:3306/databasename?noDatetimeStringSync=true&blobSendChunkSize=50000&useServerPrepStmts=true&emulateUnsupportedPstmts=false&maxAllowedPacket=20000000
然后,您必须使用
PreparedStatement进行插入,并使用
InputStream将字节内容作为参数传递给
setObject。请注意,
setObject使用字节数组不会启用Blob拆分。参数的组合(最新的MySQL服务器(5.0.45或更高版本))
InputStream将使用
LONGDATA机制发送Blob数据,并根据分割Blob
blobSendChunkSize。
JDBC解决方案有效,我已经对其进行了测试。
2)现在,第二个候选方法是使用PHP的 mysqli 驱动程序并使用
mysqli_send_long_data。为了方便起见,从PHP手册示例复制:
<?php$stmt = $mysqli->prepare("INSERT INTO messages (message) VALUES (?)");$null = NULL;$stmt->bind_param("b", $null);$fp = fopen("messages.txt", "r");while (!feof($fp)) { $stmt->send_long_data(0, fread($fp, 8192));}fclose($fp);$stmt->execute();?>


