首先在数据库里面输入
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/ | | validate_password_special_char_count | 1 | +--------------------------------------+----------------------------+ 9 rows in set (0.00 sec) mysql> set character_set_database=utf8; #更改编码属性 Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> set character_set_server=utf8; Query OK, 0 rows affected (0.00 sec) mysql> set character_set_client=gbk; Query OK, 0 rows affected (0.00 sec) mysql> set character_set_connection=gbk; Query OK, 0 rows affected (0.00 sec) mysql> set character_set_client=utf8; Query OK, 0 rows affected (0.00 sec) mysql> set character_set_connection=utf8; Query OK, 0 rows affected (0.00 sec) mysql> show variables like‘%char%‘ -> ; +--------------------------------------+----------------------------+ | Variable_name | Value | +--------------------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | | validate_password_special_char_count | 1 | +--------------------------------------+----------------------------+ 9 rows in set (0.00 sec) mysql> use test1 Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> showtables -> ; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘showtables‘ at line 1 mysql> show tables; +-----------------+ | Tables_in_test1 | +-----------------+ | address | | authors | | book_m2m_author | | books | | customer | | student | | study_record | +-----------------+ 7 rows in set (0.00 sec) mysql> insert into books (name) value(‘我‘); -> ; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘;‘ at line 1 mysql> insert into books (name) value(‘我‘); ERROR 1366 (HY000): Incorrect string value: ‘\xE6\x88\x91‘ for column ‘name‘ at row 1 mysql> alter table books convert to character set utf8; #将数据表的编码方式改成utf8 Query OK, 3 rows affected (0.02 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> insert into books (name) value(‘我‘); Query OK, 1 row affected (0.01 sec) mysql> select * from books; +----+------+----------+ | id | name | pub_date | +----+------+----------+ | 1 | bbu1 | NULL | | 2 | bbu2 | NULL | | 3 | bbu3 | NULL | | 4 | 我 | NULL | +----+------+----------+ 4 rows in set (0.00 sec) mysql> select * from books; +----+--------------+----------+ | id | name | pub_date | +----+--------------+----------+ | 1 | bbu1 | NULL | | 2 | bbu2 | NULL | | 3 | bbu3 | NULL | | 4 | 我 | NULL | | 5 | 我的世界 | NULL | +----+--------------+----------+ 5 rows in set (0.00 sec) mysql>