前言
使用mysql导入csv文件数据时遇到了很多问题
导入方法
LOAD DATA INFILE '/Users/boy.lee/Downloads/D Box/title.akas.tsv' INTO TABLE imdb_title;
ERROR
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
编辑 my.cnf, 添加如下命令后重启MySQL服务即可
[mysqld_safe]
[mysqld]
secure_file_priv=""
查找配置文件
在mac 上使用 brew 安装 mysql 后,发现 /etc/my.cnf 并不存在。
于是使用命令查找 MySQL 的配置启动文件列表:
$ mysqld --help --verbose | less
...
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/etc/my.cnf ~/.my.cnf
...
依次查找,发现文件:/usr/local/etc/my.cnf
存在!
即 通过 brew 安装的 MySQL 的配置文件为:/usr/local/etc/my.cnf
网上说的使用 ls $(brew --prefix mysql)/support-files/my-*
作为配置文件的,经查,目前我使用的版本没有对应的文件
报错:ERROR: Loading local data is disabled - this must be enabled on both the client and server sides
mysql> SET GLOBAL local_infile=1;
Query OK, 0 rows affected (0.00 sec)
quit current server:
mysql> quit
Bye
mysql --local-infile=1 -u root -p
导入csv文件
在mysql命令行工具下输入:
LOAD DATA LOCAL INFILE 'filepath/filename.tsv'
INTO TABLE DatabaseName.TableName;
前提是保证mysql安装正确,并且已经创建了数据库DatabaseName以及对应的表TableName,并且根据.tsv文件的内容插入字段