首先登录MySQL
mysql -u root -p
创建数据表,保证字段和csv文件中的列一致
mysql> show DATABASES;
mysql> use TEST;
mysql> show TABLES;
mysql> CREATE TABLE mytable(
-> id INT NOT NULL AUTO_INCREMENT,
-> Qtime DATETIME NOT NULL,
-> Tprice FLOAT NOT NULL,
-> PRIMARY KEY ( id )
-> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
查看安全路径,也就是要将上传文件放到安全路径下,才能成功上传
mysql> show variables like '%secure%';
+--------------------------+-----------------------+
| Variable_name | Value |
+--------------------------+-----------------------+
| require_secure_transport | OFF |
| secure_auth | ON |
| secure_file_priv | /var/lib/mysql-files/ |
+--------------------------+-----------------------+
如果secure_file_priv显示的参数为NULL,
表示mysql不允许上传,那么就需要修改文件my.cnf,此文件路径大多为:
/etc/my.cnf
在 my.cnf 末尾加入一行
secure_file_priv="filepath" #filepath处填入想要设置的mysql文件上传安全路径
在终端中复制文件到安全路径
cp /home/hadoop/文档/文件.csv /var/lib/mysql-files #这里应该用有权限的用户进行操作
然后在到mysql下执行
mysql> load data infile '/var/lib/mysql-files/文件.csv' into table mytable fields terminated by ',' optionally enclosed by '"' escaped by '"' lines terminated by '\n';
导出文件类似
mysql> select * from `table` load data infile '/文档/test.csv' fields terminated by ',' optionally enclosed by '"' escaped by '"' lines terminated by '\n';