1.环境
centos7.4和mysql
2.原因
mysql默认编码latin1是不支持中文字符的,所以报错。
3.解决方法
通过设置编码字符为utf8,从而支持中文字符。
4.步骤
4.1方法一:代码实现【测试实现!】
(1)查看mysql默认编码格式
mysql>show variables like '%char%';
+--------------------------+----------------------------+
| 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 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
(2)将character_set_database和character_set_server设置为utf8
set character_set_database=utf8;
set character_set_server=utf8;
(3)删除数据库并新建数据库,然后插入数据!成功
drop database sparkstreaming;
create database sparkstreaming;
use sparkstreaming;
CREATE TABLE course_info(ID int(4) PRIMARY KEY,course_id VARCHAR(10),course_name VARCHAR(100));
INSERT INTO course_info values (2,'128','10小时入门大数据');
4.2方法二:修改配置文件【经测试,实现】
(1)修改/etc/my.cnf文件
vi /etc/my.cnf
(2)在[mysqld]下面添加下面的即可!
character_set_server=utf8
init_connect='SET NAMES utf8'
(3)重启mysql
systemctl restart mysqld.service
(4)删除之前数据库,重新建表即可
drop database sparkstreaming;
create database sparkstreaming;
use sparkstreaming;
CREATE TABLE course_info(ID int(4) PRIMARY KEY,course_id VARCHAR(10),course_name VARCHAR(100));
INSERT INTO course_info values (2,'128','10小时入门大数据');