在MySQL数据库中, 当插入数据无法支持中文时, 可将该数据库的编码集设置为utf8, 故在创建数据库时, 将数据库编码集及其他字符编码设置为utf8。
如果之前创建的数据库的数据库编码集不为utf8, 可以进行如下设置:
mysql> alter database 数据库名 character set utf8;
或删除原有旧的数据库, 再重新创建新的数据库并将默认的编码集设置为utf8:
mysql> create database 数据库名 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
* 删除数据库:
mysql> drop database dbname if exists dbname;
* 创建数据库:
mysql> create database dbname if not exists dbname;
为确保 create database dbname 命令创建的数据库编码集为utf8, 我们可对mysql的配置文件进行修改windows下的mysql配置文件为my.ini, my.ini存在于mysql的安装目录下linux下的mysql配置文件为my.cnf, mysql.cnf存在于mysql的安装目录下或/etc/下在my.cnf或my.ini下找到[mysqld], 在其下方添加一行:
character_set_server=utf8
然后保存退出, 并重启mysql服务即可。
查看字符编码:
mysql> show variables like '%character%';
或
mysql> show variables like 'character_set_%';
或
mysql> use 数据库名;
mysql> \s
或
mysql> use 数据库名
mysql> status;
具体操作如下:
mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+----------------------------------+
| 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/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)
mysql> set character_set_database = utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_server = utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+----------------------------------+
| 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/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)
mysql> use db_ets;
Database changed
mysql> \s
--------------
mysql? Ver 14.14 Distrib 5.7.5-m15, for linux-glibc2.5 (x86_64) using? EditLine wrapper
Connection id:? 16
Current database:?db_ets
Current user:? root@localhost
SSL:? ?Not in use
Current pager:? stdout
Using outfile:? ''
Using delimiter:?;
Server version:? 5.7.5-m15 MySQL Community Server (GPL)
Protocol version:?10
Connection:? Localhost via UNIX socket
Server characterset:?utf8
Db? ? characterset:?utf8
Client characterset:?utf8
Conn.? characterset:?utf8
UNIX socket:? /tmp/mysql.sock
Uptime:? ?23 min 45 sec
Threads: 3? Questions: 487? Slow queries: 0? Opens: 144? Flush tables: 1? Open tables: 129? Queries per second avg: 0.341
--------------
mysql> status;
--------------
mysql? Ver 14.14 Distrib 5.7.5-m15, for linux-glibc2.5 (x86_64) using? EditLine wrapper
Connection id:? 16
Current database:?db_ets
Current user:? root@localhost
SSL:? ?Not in use
Current pager:? stdout
Us