我在加载geonomes数据集时遇到问题。继承我的过程:
首先我创建我的数据库 GEONAMES
然后我创建我的表:
CREATE TABLE `geoname` (
`geonameid` int(11) NOT NULL,
`name` varchar(200) DEFAULT NULL,
`asciiname` varchar(200) DEFAULT NULL,
`alternatenames` varchar(4000) DEFAULT NULL,
`latitude` decimal(10,7) DEFAULT NULL,
`longitude` decimal(10,7) DEFAULT NULL,
`fclass` char(1) DEFAULT NULL,
`fcode` varchar(10) DEFAULT NULL,
`country` varchar(2) DEFAULT NULL,
`cc2` varchar(60) DEFAULT NULL,
`admin1` varchar(20) DEFAULT NULL,
`admin2` varchar(80) DEFAULT NULL,
`admin3` varchar(20) DEFAULT NULL,
`admin4` varchar(20) DEFAULT NULL,
`population` int(11) DEFAULT NULL,
`elevation` int(11) DEFAULT NULL,
`gtopo30` int(11) DEFAULT NULL,
`timezone` varchar(40) DEFAULT NULL,
`moddate` date DEFAULT NULL,
PRIMARY KEY (`geonameid`),
KEY `name` (`name`),
KEY `asciiname` (`asciiname`),
KEY `latitude` (`latitude`),
KEY `longitude` (`longitude`),
KEY `fclass` (`fclass`),
KEY `fcode` (`fcode`),
KEY `country` (`country`),
KEY `cc2` (`cc2`),
KEY `admin1` (`admin1`),
KEY `population` (`population`),
KEY `elevation` (`elevation`),
KEY `timezone` (`timezone`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_unicode_ci;
接下来是我的问题所在,我尝试将数据加载到get:
上一条错误消息
无效的utf8字符串:''Afikanisitani,'Apekanikana,A Phu Han(阿富汗),APhúHãn'
继承我的加载数据的查询:
LOAD DATA LOCAL INFILE 'allCountries.txt'
INTO TABLE geoname
CHARACTER SET 'UTF8'
(geonameid, name, asciiname, alternatenames, latitude, longitude, fclass, fcode, country, cc2, admin1, admin2, admin3, admin4, population, elevation, gtopo30, timezone, moddate);
我在sequel pro命令行中运行我的查询。
答案 0 :(得分:1)
我得到了the same error on MySQL 5.7.13 - 这很奇怪,因为导入allCountries.txt
文件似乎在MySQL 5.6中运行良好
不确定what exactly changed between MySQL 5.6 and 5.7但是它与5.7中的默认character_set
和collation
有关
基于this Q&A我将默认字符集更新为utf8mb4
,然后我就可以加载allCountries.txt
文件而不会出现问题
使用mysql -u root -p
登录终端中的mysql并运行以下查询:
SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
默认的MySQL 5.7.13响应如下:
+--------------------------+-------------------+
| Variable_name | Value |
+--------------------------+-------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+--------------------------+-------------------+
(在OS X上工作)然后我需要将my.cnf
文件添加到/etc/my.cnf
- 你可以复制它:
sudo cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
- 将my.cnf
分配给_mysql
或者它无法读取它(如果my.cnf
文件存在但是由root拥有或具有过于宽松的权限,则会出现常见问题)<登记/>
sudo chown _mysql /etc/my.cnf
- 更新配置文件以包含以下内容(在相关部分中 - 如果他们不在那里就添加它们)
[client]
default-character-set = utf8mb4
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
[mysql]
default-character-set = utf8mb4
- 重启mysql(以下命令适用于OS X):
sudo launchctl unload -F /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist
sudo launchctl load -F /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist
- 在mysql中运行相同的SHOW VARIABLE
命令,结果为
+--------------------------+--------------------+
| Variable_name | Value |
+--------------------------+--------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| collation_connection | utf8_general_ci |
| collation_database | utf8mb4_unicode_ci |
| collation_server | utf8mb4_unicode_ci |
+--------------------------+--------------------+
...现在allCountries.txt
文件加载应该正常工作。