MySQL - 本地加载数据

在 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 服务,但都无济于事,服务器也是如此。不知所措。任何帮助将非常感激。


哈士奇WWW
浏览 175回答 2
2回答

幕布斯6054654

我调查了 php.ini 文件并取消了加载数据行的注释,但仍然没有。结果是 mysqld 的变量之一 secure_file_priv 指向一个空/默认目录。我所要做的就是将目录更改为我的文件所在的位置。现在都在工作。

慕婉清6462132

如果我没记错的话,php 有自己的配置文件,你必须在其中启用加载数据本地 infile
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python