简体   繁体   English

错误 1148 MySQL 该MySQL 版本不允许使用所使用的命令

[英]Error 1148 MySQL The used command is not allowed with this MySQL version

I am using MySQL LOAD DATA LOCAL INFILE command and I get this error:我正在使用 MySQL LOAD DATA LOCAL INFILE命令,但出现此错误:

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1148 The used 
command is not allowed with this MySQL version: LOAD DATA LOCAL INFILE 
'/tmp/phpI0ox54' INTO TABLE `dev_tmp` FIELDS TERMINATED BY ',' ENCLOSED 
BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES; Array ( ) in 
dc_real_estate_form_submit() (line 147 of /PATH/TO/PHP/SCRIPT).

What setting can we change to allow LOAD DATA LOCAL infile?我们可以更改哪些设置以允许 LOAD DATA LOCAL infile?

Here is the Drupal 7 code we are using:这是我们使用的 Drupal 7 代码:

$sql = "LOAD DATA LOCAL INFILE '".$file."'
    INTO TABLE `dev_tmp`
    FIELDS
        TERMINATED BY ','
        ENCLOSED BY '\"'
    LINES
    TERMINATED BY '\\r\\n'
    IGNORE 1 LINES";

db_query($sql);

Loading a local file in MySQL is a security hazard and is off by default, you want to leave it off if you can.在 MySQL 中加载本地文件存在安全隐患,默认情况下处于关闭状态,如果可以,您希望将其关闭。 When it is not permitted you get this error:如果不允许,您会收到此错误:

ERROR 1148 (42000): The used command is not allowed with this MySQL version

Solutions:解决方案:

  1. Use --local-infile=1 argument on the mysql commandline:在 mysql 命令行上使用--local-infile=1参数:

    When you start MySQL on the terminal, include --local-infile=1 argument, Something like this:在终端上启动 MySQL 时,包含--local-infile=1参数,如下所示:

     mysql --local-infile=1 -uroot -p mysql>LOAD DATA LOCAL INFILE '/tmp/foo.txt' INTO TABLE foo COLUMNS TERMINATED BY '\\t';

    Then the command is permitted:然后命令被允许:

     Query OK, 3 rows affected (0.00 sec) Records: 3 Deleted: 0 Skipped: 0 Warnings: 0
  2. Or send the parameter into the mysql daemon:或者将参数发送到mysql守护进程中:

     mysqld --local-infile=1
  3. Or set it in the my.cnf file (This is a security risk):或者在 my.cnf 文件中设置(这是一个安全风险):

    Find your mysql my.cnf file and edit it as root.找到您的 mysql my.cnf文件并以 root 身份编辑它。

    Add the local-infile line under the mysqld and mysql designators:在 mysqld 和 mysql 指示符下添加local-infile行:

     [mysqld] local-infile [mysql] local-infile

    Save the file, restart mysql.保存文件,重启mysql。 Try it again.再试一遍。

More info can be found here: http://dev.mysql.com/doc/refman/5.1/en/load-data-local.html更多信息可以在这里找到: http : //dev.mysql.com/doc/refman/5.1/en/load-data-local.html

In addition to using local-infile with the MySQL server (you can put this in the /etc/my.cnf file too), you also need to enable PDO to allow it:除了在 MySQL 服务器上使用local-infile (你也可以把它放在 /etc/my.cnf 文件中),你还需要启用 PDO 来允许它:

<?php
$pdo = new PDO($dsn, $user, $password, 
    array(PDO::MYSQL_ATTR_LOCAL_INFILE => true)
);

Otherwise it won't work, regardless of the value of local-infile on the MySQL server.否则它将无法工作,无论 MySQL 服务器上local-infile的值如何。

The legacy mysql_connect also has a parameter ' client_flag ' that can be used to set the mysql parameter.旧版 mysql_connect 也有一个参数“ client_flag ”,可用于设置 mysql 参数。

The client_flags parameter can be a combination of the following constants: 128 (enable LOAD DATA LOCAL handling), MYSQL_CLIENT_SSL, MYSQL_CLIENT_COMPRESS, MYSQL_CLIENT_IGNORE_SPACE or MYSQL_CLIENT_INTERACTIVE. client_flags 参数可以是以下常量的组合:128(启用 LOAD DATA LOCAL 处理)、MYSQL_CLIENT_SSL、MYSQL_CLIENT_COMPRESS、MYSQL_CLIENT_IGNORE_SPACE 或 MYSQL_CLIENT_INTERACTIVE。 Read the section about MySQL client constants for further information.阅读有关 MySQL 客户端常量的部分以获取更多信息。 In SQL safe mode, this parameter is ignored.在 SQL 安全模式下,该参数被忽略。 http://php.net/function.mysql-connect http://php.net/function.mysql-connect

Example:例子:

$db = mysql_connect($host, $user, $pass, FALSE, 128);

However, you may also encounter the following error:但是,您也可能会遇到以下错误:

ERROR 29 (HY000): File '/var/www/.../mysql_import.csv' not found (Errcode: 13)

In which case, you may need to check your App Armor settings to allow MySQL access to the import files on the filesystem.在这种情况下,您可能需要检查您的 App Armor 设置以允许 MySQL 访问文件系统上的导入文件。

In particular I added:我特别补充说:

  /import/ r,
  /import/* rw,

To give MySQL read/write access to /import授予 MySQL 对 /import 的读/写访问权限

eg: Sample App Armor profile例如:示例 App Armor 配置文件

cat /etc/apparmor.d/usr.sbin.mysqld 

# vim:syntax=apparmor
# Last Modified: Tue Jun 19 17:37:30 2007
#include <tunables/global>

/usr/sbin/mysqld {
  #include <abstractions/base>
  #include <abstractions/nameservice>
  #include <abstractions/user-tmp>
  #include <abstractions/mysql>
  #include <abstractions/winbind>

  capability dac_override,
  capability sys_resource,
  capability setgid,
  capability setuid,

  network tcp,

  /etc/hosts.allow r,
  /etc/hosts.deny r,

  /etc/mysql/*.pem r,
  /etc/mysql/conf.d/ r,
  /etc/mysql/conf.d/* r,
  /etc/mysql/*.cnf r,
  /usr/lib/mysql/plugin/ r,
  /usr/lib/mysql/plugin/*.so* mr,
  /usr/sbin/mysqld mr,
  /usr/share/mysql/** r,
  /var/log/mysql.log rw,
  /var/log/mysql.err rw,
  /var/lib/mysql/ r,
  /var/lib/mysql/** rwk,
  /var/log/mysql/ r,
  /var/log/mysql/* rw,
  /var/run/mysqld/mysqld.pid w,
  /var/run/mysqld/mysqld.sock w,
  /run/mysqld/mysqld.pid w,
  /run/mysqld/mysqld.sock w,

  # Custom import folders start
  # These folders will also be read/writeable by mysql.
  /import/ r,
  /import/* rw,
  # Custom import folders end

  /sys/devices/system/cpu/ r,

  # Site-specific additions and overrides. See local/README for details.
  #include <local/usr.sbin.mysqld>
}

After that MySQL could read files from the /import directory.之后,MySQL 可以从/import目录中读取文件。

The main reason why we are using the LOCAL keyword is explained in the MySQL manual:我们使用 LOCAL 关键字的主要原因在 MySQL 手册中有解释:

On the other hand, you do not need the FILE privilege to load local files.另一方面,您不需要 FILE 权限来加载本地文件。

So if you in fact do have file access to the server then try to skip using the word "LOCAL" in SQL query, and instead copy the file to the server and the directory mysql/data/[tablename].因此,如果您确实拥有对服务器的文件访问权限,请尝试在 SQL 查询中跳过使用“LOCAL”一词,而是将文件复制到服务器和目录 mysql/data/[tablename]。

More about this LOCAL/non-LOCAL here: PHPMyAdmin Bug有关此本地/非本地的更多信息: PHPMyAdmin Bug

You don't have to worry any longer about making changes to /etc/mysql/my.cnf您不必再担心对 /etc/mysql/my.cnf 进行更改

Just follow the following process to solve the problem:只需按照以下过程来解决问题:

  1. Login to mysql using command line.使用命令行登录mysql。

    mysql -u root -p mysql -u 根 -p

  2. Check the local infile status:检查本地 infile 状态:

    SHOW VARIABLES LIKE 'local_infile';显示变量如'local_infile';

  3. If the output is Off, run this command:如果输出为关闭,请运行以下命令:

    SET GLOBAL local_infile = 1;设置全局 local_infile = 1;

  4. Check the status again:再次检查状态:

    SHOW VARIABLES LIKE 'local_infile';显示变量如'local_infile';

  5. The output will be: see now输出将是:立即查看

  6. Finally restart mysql:最后重启mysql:

    service mysql restart服务mysql重启

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM