mysql --不能输入中文

问题:当输入中文时报错

ERROR 1366 (HY000): Incorrect string value: '\xE5\xA5\xBD\xE5\xA5\xBD' for column 'name'

 

解决问题:

mysql> alter table t2 charset utf8;         #步骤1  更改表引擎
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
mysql> show create table t2;          #步骤2 查看表结构
+-------+-------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                              |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------+
| t2    | CREATE TABLE `t2` (
  `id` int(11) DEFAULT NULL,
  `name` char(30) CHARACTER SET latin1 DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
 
mysql> insert into t2(id,name) values(2,'你好');
ERROR 1366 (HY000): Incorrect string value: '\xE4\xBD\xA0\xE5\xA5\xBD' for column 'name' at row 1
 
还是不行。
这是因为之前表字符集是latin1,列的字符集没有设置,默认也跟表一样,也是latin1,故也需要修改列的字符集:
mysql> alter table t2 modify name char(30) CHARACTER SET utf8 DEFAULT NULL;        #步骤三,更改表里面的字段
Query OK, 1 row affected (2.04 sec)
Records: 1  Duplicates: 0  Warnings: 0
 
mysql> show create table t2;
+-------+----------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                         |
+-------+----------------------------------------------------------------------------------------------------------------------+
| t2    | CREATE TABLE `t2` (
  `id` int(11) DEFAULT NULL,
  `name` char(30) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
 
现在再次插入,就不再报错了。

 linux直接修改成charset=utf8

步骤1;
在终端中输入 sudo gedit /etc/mysql/mysql.conf.d/mysqld.cnf  命令

步骤二:
打开mysqld.cnf 文件,在lc-messages-dir = /usr/share/mysql 语句后添加 character-set-server=utf8 语句

步骤三:
4.在终端输入 sudo gedit /etc/mysql/conf.d/mysql.cnf 命令打开mysql.cnf配置文件,如图添加代码:default-character-set=utf8 如图:

步骤四
5.在终端中输入  /etc/init.d/mysql restart 命令重启MySQL服务,如图所示,重启成功。

 

posted @ 2018-05-31 20:13  forjie  阅读(166)  评论(0)    收藏  举报