有没有办法在不更改 max_allowed_packet 的情况下在 mysql DB 中插入大值?
我知道,为了将大于 max_allowed_packet 字节的值插入 MySQL 数据库,默认解决方案是确保客户端和服务器端 max_allowed_packet 变量都大于查询插入数据库的数据块。
但是,有什么方法可以不更改上面提到的服务器端变量吗?当我必须将数据插入到 ISP 托管的数据库中(该数据库不允许我更改 max_allowed_packet
限制)时,这会很有用。
另一个相关问题:MySql longblob
limit is 4GB, but max_allowed_packet
limit is 1GB。那么,是否可以在 longblob
表列中插入大于 1GB 的值?
I know that in order to insert values larger than max_allowed_packet
bytes into a MySQL database, the default solution would be to assure that both client and server side max_allowed_packet
variables are bigger than the chunk of data that a query inserts into the DB.
However, is there any way to do so without changing the server side variable mentioned above? This would be useful when I have to insert data into a database that is hosted in an ISP that doesn't allow me to change the max_allowed_packet
limit.
Another related question: MySql longblob
limit is 4GB, but max_allowed_packet
limit is 1GB. So, is it possible to insert values larger than 1GB in a longblob
table column?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最近偶然发现了这个问题。就我而言,服务器的
max_allowed_packet
为 1 MB,我无法对其进行任何更改。我插入了一些刚刚超过 1 MB 的数据。我找到了两个候选解决方案。1)首先,使用JDBC。
从 MySQL Connector/J v3.1.9 开始,您可以设置一些参数,这是我在 JDBC URL 中设置的参数:
附加这些:
生成 JDBC URL:
然后您必须使用
PreparedStatement
进行插入,并使用InputStream
将字节内容作为参数传递给setObject
。请注意,使用字节数组的setObject
不会启用 blob 分割。参数组合,最近的MySQL服务器(5.0.45或更高版本)和InputStream
将使用LONG DATA
机制发送blob数据,根据分割blob blobSendChunkSize
。JDBC 解决方案有效并且我已经对其进行了测试。
2) 现在,第二个候选者是使用 PHP 的 mysqli 驱动程序并使用
mysqli_send_long_data
。为了您的方便,复制自 PHP 手册示例:I recently stumbled upon this problem. In my case, the server's
max_allowed_packet
was 1 MB and I could do nothing to change it. And I was inserting some data just above 1 MBs. I found two solution candidates.1) First, using JDBC.
Since MySQL Connector/J v3.1.9, there are a few parameters that you could set, here's my set of parameters in the JDBC URL:
Append these:
Resulting in JDBC URL:
Then you must use
PreparedStatement
to do your inserts, and use theInputStream
to pass the byte content as a parameter tosetObject
. Note thatsetObject
using byte arrays won't enable the blob splitting. The combination of parameters, recent MySQL server (5.0.45 or later), andInputStream
will send the blob data usingLONG DATA
mechanism, splitting the blob according toblobSendChunkSize
.The JDBC solution works and I have tested it.
2) Now, the second candidate, is to use PHP's mysqli driver and use
mysqli_send_long_data
. For your convenience, copied from PHP manual example :我认为没有办法。也许分裂斑点就能解决问题。
但我认为阅读这篇文章 http://blogs Reymond Chen 的 .msdn.com/b/oldnewthing/archive/2007/03/01/1775759.aspx 是您问题的最佳答案。
关系数据库不是为这种用途而设计的。如果您需要在数据库中存储 GB+ Blob,那么通常最好将其存储在 NFS 上,并且在数据库中只包含一个短文件位置字符串。会为你省去很多以后的麻烦。
I don't think that there is a way. Maybe splitting the blob will do the trick.
But I think that reading this article http://blogs.msdn.com/b/oldnewthing/archive/2007/03/01/1775759.aspx from Reymond Chen is the best answer to your question.
Relational databases are not designed for that kind of use. If you need to store a gigabyte+ blob in a database, well it is better usually to store it on the NFS and just have a short file location string in your DB. Will save you much troubles down the road.