You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Travis构建报错:LOAD DATA LOCAL INFILE请求因访问限制被拒绝

Travis构建中MySQL LOAD DATA LOCAL INFILE权限问题解决

问题背景

本地环境配置local_infile后可正常使用LOAD DATA LOCAL INFILE,但在Travis CI构建中,通过编程方式连接MySQL(无法设置客户端选项)时,出现以下错误:

MySQLdb.OperationalError: (2068, 'LOAD DATA LOCAL INFILE file request rejected due to restrictions on access.')

已尝试在Travis的before_install和install阶段执行命令开启全局local_infile:

mysql -u root -h localhost -e 'SET GLOBAL local_infile=1'

但问题仍未解决,Travis配置如下:

language: python
os: linux
dist: jammy

services:
  - mysql

python:
  - "3.10.5"

env:
  - TESTENV=test

before_install:
  - mysql -u root -h localhost -e 'GRANT ALL PRIVILEGES ON *.* TO "travis"@"%"'
install:
  - pip3 install --upgrade pip
  - pip3 install --upgrade setuptools wheel
  - mysql -u root -h localhost -e 'SET GLOBAL local_infile=1'
  - pip3 install -r requirements.txt
  - if [[ "$TESTENV" != "docs" ]]; then pip3 install -r requirements-test.txt; fi
  - pip3 install -e .

script:
  - if [[ "$TESTENV" == "test" ]]; then coverage run -m pytest --server=mysql://travis@127.0.0.1:3306/ src/tests; fi
  - if [[ "$TESTENV" == "test" ]]; then coverage report -m; fi

解决方案

1. 修改MySQL配置文件永久开启local_infile

Travis默认的MySQL配置可能未启用local_infile,临时设置全局变量可能因连接复用或配置重载失效,需直接修改配置文件:
在before_install阶段添加:

# 在mysqld配置块下添加local_infile=1
sudo sed -i '/\[mysqld\]/a local_infile=1' /etc/mysql/mysql.conf.d/mysqld.cnf
# 重启MySQL服务生效
sudo service mysql restart

2. 给连接URL添加local_infile参数

部分MySQL连接库支持通过URL参数传递配置,修改pytest连接URL:

mysql://travis@127.0.0.1:3306/?local_infile=1

3. 验证配置是否生效

在install阶段添加验证命令,确认全局和会话级别的local_infile状态:

mysql -u root -h localhost -e 'SHOW GLOBAL VARIABLES LIKE "local_infile";'
mysql -u travis -h localhost -e 'SHOW SESSION VARIABLES LIKE "local_infile";'

若会话级别仍为OFF,说明连接未继承全局设置,需检查连接逻辑是否强制关闭该选项。

4. 直接指定启动参数开启local_infile

若修改配置文件无效,可在启动MySQL时直接指定参数:
在before_install阶段添加:

sudo service mysql stop
sudo mysqld_safe --local-infile=1 &
sleep 5 # 等待服务启动完成

完整修改后的Travis配置示例

language: python
os: linux
dist: jammy

services:
  - mysql

python:
  - "3.10.5"

env:
  - TESTENV=test

before_install:
  - sudo sed -i '/\[mysqld\]/a local_infile=1' /etc/mysql/mysql.conf.d/mysqld.cnf
  - sudo service mysql restart
  - mysql -u root -h localhost -e 'GRANT ALL PRIVILEGES ON *.* TO "travis"@"%"'
  # 验证配置是否生效
  - mysql -u root -h localhost -e 'SHOW GLOBAL VARIABLES LIKE "local_infile";'
install:
  - pip3 install --upgrade pip
  - pip3 install --upgrade setuptools wheel
  - pip3 install -r requirements.txt
  - if [[ "$TESTENV" != "docs" ]]; then pip3 install -r requirements-test.txt; fi
  - pip3 install -e .

script:
  - if [[ "$TESTENV" == "test" ]]; then coverage run -m pytest --server=mysql://travis@127.0.0.1:3306/?local_infile=1 src/tests; fi
  - if [[ "$TESTENV" == "test" ]]; then coverage report -m; fi

内容的提问来源于stack exchange,提问作者user453575457

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 02:00:32