MariaDB插入BLOB图像 [英] MariaDB Insert BLOB Image

查看:549
本文介绍了MariaDB插入BLOB图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做什么?


  • 我想使用以下内容将图片插入MariaDB数据库命令行,使用LOAD_FILE功能。

出了什么问题?


  • 我总是得到一个NULL返回。

  • 想要一个像这样的解决方案:这很糟糕风格,我到目前为止还没有看到 - 尝试存储完整的路径!我想要将此图片存储在此数据库中,路径。

  • I always get a NULL return.
  • I don't want a solution like: This is bad style and I haven't seen this so far - try to store the full path! I want to store this picture in this database and not the path.

系统


  • mysql Ver 15.1 Distrib 10.1.17-MariaDB ,对于使用readline 5.1的Linux(x86_64)

  • mysql Ver 15.1 Distrib 10.1.17-MariaDB, for Linux (x86_64) using readline 5.1

ArchLinux 4.7.2-1-ARCH

ArchLinux 4.7.2-1-ARCH

/home/user/Blob/Test.jpg 下的 Test.jpg (817KB)图片, /tmp/Test.jpg ,甚至在 / var / lib下(mysql / images / Test.jpg

A picture called Test.jpg (817KB) under /home/user/Blob/Test.jpg, /tmp/Test.jpg and even under /var/lib(mysql/images/Test.jpg

图片属于用户和组mysql,拥有我可以想象的所有权限

The picture belongs to the user and group mysql and has every permission I could imagine

-rwxrwxrwx  1 mysql mysql 836508 20. Feb 2016  Test.jpg


  • 我测试过几个用户,即 mysql root

  • I tested several users i.e. mysql and root
  • 数据库

    我创建了一个名为 Blobtest 的数据库,其中包含一个名为 Test 的表,其中包含Blob和Longblob变量BLE。

    I have created a database called Blobtest with a table called Test with a Blob and a Longblob variable.

    CREATE TABLE Test (id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,longblobimg LONGBLOB NOT NULL, blobimg BLOB NOT NULL, PRIMARY KEY(id));
    
    +-------------+------------------+------+-----+---------+----------------+
    | Field       | Type             | Null | Key | Default | Extra          |
    +-------------+------------------+------+-----+---------+----------------+
    | id          | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
    | longblobimg | longblob         | NO   |     | NULL    |                |
    | blobimg     | blob             | NO   |     | NULL    |                |
    +-------------+------------------+------+-----+---------+----------------+
    

    插入声明

    (在这种情况下使用/ home / user /目录)

    (in this case with the /home/user/directory)

    INSERT INTO Test VALUES (1, LOAD_FILE('/home/user/Blob/Test.jpg'), LOAD_FILE('/home/user/Blob/Test.jpg'));
    

    解决此问题的方法

    我按照此链接中的说明 MySQL LOAD_FILE()加载空值

    I followed the instructions at this link MySQL LOAD_FILE() loads null values


    • 我对父目录具有执行权限

    • FILE权限明确表示理所当然的。 (授予文件。对于mysql @ localhost)

    • 我已刷新权限

    • 我已退出并重新登录

    • 我已经通过 chmod 和<$ c $测试了几个目录,属于 mysql 用户 c> chown 命令

    • SHOW VARIABLES LIKE'max_allowed_pa​​cket'; 设置为16 MB或16777216,图片是817KB

    • 选择HEX(LOAD_FILE('/ home / user / Blob / Test.jpg')); 返回NULL

    • I have execute permission on the parent directory
    • The FILE privilege is explicily granted. (GRANT FILE on . TO mysql@localhost)
    • I have flushed privileges
    • I have logged out and logged back in
    • I have tested several directories, belonging to mysql or user via chmod and chown command
    • SHOW VARIABLES LIKE 'max_allowed_packet'; is set to 16 MB or 16777216, picture is 817KB big
    • select HEX(LOAD_FILE('/home/user/Blob/Test.jpg')); returns NULL

    解决方案?

    我不知道这是MariaDB中的错误,还是我是唯一一个遇到此问题的人。
    再次指出:我想要将图片存储在此数据库中。我想要存储路径。这是一个实验,我必须将图片存储在数据库中。

    I don't know if this is a bug in MariaDB or if I'm the only one who has this problem. To point this out again: I want to store the picture within this database. I don't want to store the path. This is an experiment, I have to store the picture in the database.

    如果有人可以帮我解决这个问题,那真是太棒了!

    It would be awesome if somebody can help me with this problem!

    推荐答案

    LOAD_FILE() 仅在文件位于数据库服务器上时才有效。如果服务器与文件位于同一系统上,则加载它应该没有问题。如果是这样,我建议打开关于错误报告不足的错误报告。

    LOAD_FILE() only works if the file is located on the database server. If the server is on the same system as the file, then there should be no problems loading it. If so, I'd recommend opening a bug report about inadequate error reporting.

    通常,连接器提供将二进制数据加载到数据库的方法。例如,MariaDB C连接器提供 mysql_stmt_send_long_data

    Usually connectors provide methods for loading binary data into the database. For example the MariaDB C connector provides mysql_stmt_send_long_data.

    另一种选择是在客户端将数据转换为非二进制编码(例如base64或hex)并在服务器上对其进行解码( base64与 FROM_BASE64 和hex with UNHEX ) 。

    Another option is to convert the data into a non-binary encoding (e.g. base64 or hex) on the client side and decode it on the server (base64 with FROM_BASE64 and hex with UNHEX).

    这篇关于MariaDB插入BLOB图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆