首页 > 解决方案 > 错误:加载本地数据被禁用 - 这必须在客户端和服务器端都启用

问题描述

我不明白其他人对类似问题的回答,除了最明显的问题,例如以下问题:

mysql> SET GLOBAL local_infile=1;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW GLOBAL VARIABLES LIKE 'local_infile';

+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| local_infile  | ON    |
+---------------+-------+
1 row in set (0.01 sec)

我的意思是提供了确切的代码。如果有人可以逐步指导我,我将不胜感激,我需要做什么才能在“客户端”端和“服务器”端启用本地数据。好像我已经在客户端启用了本地数据,但我不知道我需要给我的电脑什么指令来启用“服务器端”。我一点也不精通技术,我只想能够达到将数据上传到 MySQL 工作台的地步。

ERROR 3948 (42000): Loading local data is disabled; this must be enabled on both the client and server sides
CREATE TABLE toys (
uniq_id VARCHAR(1000),
product_name VARCHAR(1000),
manufacturer VARCHAR(1000),
price VARCHAR(1000),
number_available_in_stock VARCHAR (1000),
number_of_reviews INT,
number_of_answered_questions INT,
average_review_rating VARCHAR(1000),
amazon_category_and_sub_category VARCHAR(1000),
customers_who_bought_this_item_also_bought VARCHAR(1000),
description VARCHAR(1000),
product_information VARCHAR(1000),
product_description VARCHAR(1000),
items_customers_buy_after_viewing_this_item VARCHAR(1000),
customer_questions_and_answers VARCHAR(1000),
customer_reviews VARCHAR(1000),
sellers VARCHAR(1000)
);

LOAD DATA LOCAL INFILE ‘/Users/BruddaDave/Desktop/amazonsample.csv’ INTO TABLE toys
FIELDS TERMINATED BY ‘,’
LINES TERMINATED BY ‘\n’
IGNORE 1 LINES
(uniq_id, product_name, manufacturer, price, number_available_in_stock, number_of_reviews, number_of_answered_questions, average_review_rating, amazon_category_and_sub_category, customers_who_bought_this_item_also_bought, description, product_information, product_description, items_customers_buy_after_viewing_this_item, customer_questions_and_answers, customer_reviews, sellers)
;

我只想能够使用命令行 shell 将 .csv 文件导入 MySQL。

标签: mysqlclientlocal

解决方案


如果 LOCAL 功能被禁用,无论是在服务器端还是客户端,尝试发出 LOAD DATA LOCAL 语句的客户端都会收到以下错误消息:

ERROR 3950 (42000): Loading local data is disabled; this must be
enabled on both the client and server side

当我想按照 Mysql 的教程将文本文件 pet.txt 加载到 pet 表中时遇到了同样的问题:https ://dev.mysql.com/doc/refman/8.0/en/loading-tables.html

在网上搜索后,我通过以下步骤修复了它:

  1. 使用以下命令设置全局变量:
mysql> SET GLOBAL local_infile=1;
Query OK, 0 rows affected (0.00 sec)
  1. 退出当前服务器:
mysql> quit
Bye
  1. 使用 local-infile 系统变量连接到服务器:
mysql --local-infile=1 -u root -p1

此变量控制 LOAD DATA 语句的服务器端 LOCAL 功能。根据 local_infile 设置,服务器拒绝或允许在客户端启用 LOCAL 的客户端加载本地数据。要显式导致服务器拒绝或允许 LOAD DATA LOCAL 语句(无论客户端程序和库在构建时或运行时如何配置),请分别禁用或启用 local_infile 启动 mysqld。local_infile 也可以在运行时设置。

  1. 使用您的数据库并将文件加载到表中:
mysql> use menagerie
Database changed
mysql> load data local infile '/path/pet.txt' into table pet;
Query OK, 8 rows affected, 7 warnings (0.00 sec)

它有效吗?

参考:

https://dev.mysql.com/doc/refman/8.0/en/load-data-local-security.html https://dev.mysql.com/doc/refman/8.0/en/source-configuration-options .html#option_cmake_enabled_local_infile https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_local_infile


推荐阅读