【修改编码格式】
修改数据库编码 |
ALTER database database_test character set utf8; |
修改数据库表编码 |
ALTER table table_test character set utf8; |
【外部数据库方便乱码问题解决】
SET NAMES 'utf8';
它相当于下面的三句指令:
SET character_set_client = utf8; SET character_set_results = utf8; SET character_set_connection = utf8; |
创建数据库
mysql> create database name character set utf8; |
创建表
CREATE TABLE `type` ( `id` int(10) unsigned NOT NULL auto_increment, `name` varchar(50) character set utf8 NOT NULL default '', PRIMARY KEY (`id`) ) DEFAULT CHARSET=utf8; |
修改数据库成utf8的.
mysql> alter database name character set utf8; |
修改表默认用utf8.
mysql> alter table type character set utf8; |
修改字段用utf8
mysql> alter table type modify type_name varchar(50) CHARACTER SET utf8; |
【数据库内部乱码问题】
1. 修改数据库配置文件(vi /etc/my.cnf)
--在 [mysqld] 标签下加上三行
default-character-set = utf8 character_set_server = utf8 lower_case_table_names = 1 //表名不区分大小写(此与编码无关) |
--在 [mysql] 标签下加上一行
default-character-set = utf8 |
--在 [mysql.server]标签下加上一行
default-character-set = utf8 |
--在 [mysqld_safe]标签下加上一行
default-character-set = utf8 |
--在 [client]标签下加上一行
default-character-set = utf8 |
2. 重启数据库服务
3. 查看设置结果
show variables like "%char%"; show variables like "%collation%"; |