1.查看MySQL数据库的默认编码
(1).使用status命令
mysql> status
--------------
mysql Ver 14.14 Distrib 5.5.28, for Win64 (x86) Connection id: 2
Current database: day17
Current user: root@localhost
SSL: Not in use
Using delimiter: ;
Server version: 5.5.28 MySQL Community Server (GPL)
Protocol version: 10
Connection: localhost via TCP/IP
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
TCP port: 3306
Uptime: 1 hour 14 min 9 sec
(2)使用show variables命令
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 | D:\WorkTool\MySQL\MySQL Server 5.5\share\charsets\ |
+--------------------------+----------------------------------------------------+
8 rows in set (0.08 sec)
2.查看某个数据库的默认编码
首先执行use db_name命令,切换到要查看的数据库,再执行上述命令。
3.改变数据库的默认编码
安装MySQL数据库时的默认编码是latin1,实际使用时可能要使用其他编码。下文描述如何将默认编码改为utf8:
首先修改MySQL的配置文件/etc/mysql/my.cnf:
在[client]下追加:
default-character-set=utf8
在[mysqld]下追加:
character-set-server=utf8
在[mysql]下追加:
default-character-set=utf8
修改完毕后,使用如下命令之一重启mysql服务:
service mysql restart
/etc/init.d/mysql restart
改变后,之前手工创建的数据库如未显式指定编码,则其编码仍是默认的latin1,可使用如下命令更改编码:
mysql> alter database db_name CHARACTER SET utf8;