【问题标题】:MySQL - LOAD DATA LOCALMySQL - 本地加载数据
【发布时间】:2020-07-20 13:34:20
【问题描述】:

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

dbupdate.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 服务,以及服务器。在这里不知所措。任何帮助将不胜感激。

【问题讨论】:

    标签: python mysql linux ubuntu


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2011-05-25
        • 1970-01-01
        • 1970-01-01
        • 2012-10-26
        • 2014-10-23
        • 1970-01-01
        • 2015-03-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多