惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
博客园 - 叶小钗
Last Week in AI
Last Week in AI
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
P
Proofpoint News Feed
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
aimingoo的专栏
aimingoo的专栏
月光博客
月光博客
量子位
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta
Apple Machine Learning Research
Apple Machine Learning Research
Jina AI
Jina AI
博客园 - Franky
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net
博客园_首页
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
How to Read Local CSV File to Table in MySQL
2022-10-15 · via jdhao's digital space

I am using MySQL server version 8.0.30 on my macOS. I try to read a local csv file into a mysql table using LOAD DATA LOCAL INFILE following some post1, but unfortunately I get the following error:

Error Code: 3948. Loading local data is disabled; this must be enabled on both the client and server sides

Following the guide of some posts on the Internet, I tried to change the variable local_infile to 1:

SET GLOBAL local_infile=1;

Then I got the following error when loading csv file:

Error Code: 2068. LOAD DATA LOCAL INFILE file request rejected due to restrictions on access.

The right way to load local csv file#

Here I will summarize what is working for me on macOS.

change the variable local_infile#

First we need to run the following command when we are still connecting to the server:

SET GLOBAL local_infile=1;

Then use SHOW GLOBAL VARIABLES LIKE '%local_infile%'; to check if the change has taken effect. You should see the following output:

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

Restart the server and establish new client connection#

Then stop the mysql server and restart the server. If you are using macOS and install mysql via HomeBrew, this is easy to do

brew services stop mysql
brew services start mysql

Then stop the client connection.

Reconnect client to server#

If you are using mysql command line client to connect to the server, run this:

mysql --local-infile=1 -u root -p

If you are using other SQL client to connect to the mysql server, the settings may vary. For MySQL Workbench, go to Database --> Manage Connections, select the connection, under Connection --> Advanced, there is a Others text box, add the following conf (in new line):

Then click the button Reconnect to DBMS in the toolbar. You should be able to load csv files into a table.

Load the csv file#

Finally, we can load the local csv file. The csv file looks like this:

field1,field2
foobar,1
hello,2
great,3

First, we need to create a table to load the csv:

CREATE TABLE my_table (
  col1 VARCHAR(255),
  col2 INT
);

The SQL query I use to load the data is:

LOAD DATA LOCAL INFILE '/path/to/my_csv'
INTO TABLE my_table  -- load the csv data to my_table
FIELDS TERMINATED BY ','  -- field separator
LINES TERMINATED BY '\n'  -- line ending
IGNORE 1 ROWS;  -- ignore the header line

References#