-1

在 Ubuntu (20.04) VPS 上运行 MySQL (8.0) 数据库。我目前的目标是尝试通过 Python 脚本将 .CSV 自动加载到表中。该脚本在理论上是正确的并且应该可以工作,它能够将数据从 CSV 处理到表中。

数据库更新.py:

import mysql.connector
import os
import string


db = mysql.connector.connect (
    host="localhost",
    user="root",
    passwd="********",
    db="Rack_Info"
)

sqlLoadData = "LOAD DATA LOCAL INFILE '/home/OSA_ADVA_Dashboard/Processed_CSV/DownloadedCSV.csv' INTO TABLE BerT FIELDS TERMINATED BY ',' ENCLOSED BY '*' IGNORE 1 LINES;"

try:
    curs = db.cursor()
    curs.execute(sqlLoadData)
    db.commit()
    print ("SQL execution complete")
    resultSet = curs.fetchall()
except IOError:
    print ("Error incurred: ")
    db.rollback()
    db.close()

print ("Data loading complete.\n")

我查阅了官方文档并在服务器和客户端上启用了 local_infile,配置了 my.cnf 和 SQL。

my.cnf 文件:

#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

[client]
local_infile=1

[mysql]
local_infile=1

[mysqld]
local_infile=1

我重新启动了 php 和 MySQL 服务都无济于事,以及服务器。在这里不知所措。任何帮助将非常感激。

4

2 回答 2

0

如果我没记错的话 php 有自己的配置文件,你必须在其中启用加载数据本地 infile

于 2020-07-20T14:14:24.503 回答
0

我调查了 php.ini 文件并取消了加载数据行的注释,仍然没有。

原来 mysqld 的变量之一,secure_file_priv,指向一个空/默认目录。我所要做的就是将目录更改为我的文件所在的位置。现在都在工作。

于 2020-07-22T14:27:52.277 回答