Mysql 字符集的设置和修改
Show variables like ‘character%’; //显示目前mysql默认 字符集
显示数据连接字符集
修改字符集
D:\mysql>Net stop mysql //停止MYSQL
方法:修改my.ini
在安装文件夹中把my-dafault.ini改成my.ini
[mysqld]
port=3306
Character_set_server = utf8
修改完后,重启mysql的服务
Show variables like ‘character%’; //显示目前mysql默认 字符集
还有一种修改mysql默认字符集的方法,就是使用mysql的命令
- mysql> SET character_set_client = utf8 ;
- mysql> SET character_set_connection = utf8 ;
- mysql> SET character_set_database = utf8 ;
- mysql> SET character_set_results = utf8 ;
- mysql> SET character_set_server = utf8 ;
- mysql> SET collation_connection = utf8 ;
- mysql> SET collation_database = utf8 ;
- mysql> SET collation_server = utf8 ;
一般就算设置了表的mysql默认字符集为utf8并且通过UTF-8编码发送查询,你会发现存入数据库的仍然是乱码。问题就出在这个connection连接层上。解决方法是在发送查询前执行一下下面这句:
- SET NAMES 'utf8';
它相当于下面的三句指令:
- SET character_set_client = utf8;
- SET character_set_results = utf8;
- SET character_set_connection = utf8;
修改已有表的字符集为utf8
alter table t_user_friend convert to character set utf8;
修改数据库字符集:
ALTER DATABASE db_name DEFAULT CHARACTER SET character_name [COLLATE ...];
把表默认的字符集和所有字符列(CHAR,VARCHAR,TEXT)改为新的字符集:
ALTER TABLE tbl_name CONVERT TO CHARACTER SET character_name [COLLATE
...]
如:ALTER TABLE logtest CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
只是修改表的默认字符集:
ALTER TABLE tbl_name DEFAULT CHARACTER SET character_name [COLLATE...];
如:ALTER TABLE logtest DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
修改字段的字符集:
ALTER TABLE tbl_name CHANGE c_name c_name CHARACTER SET character_name [COLLATE
...];
如:ALTER TABLE logtest CHANGE title title
VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci;
查看数据库编码:SHOW CREATE DATABASE db_name;
查看表编码:SHOW CREATE TABLE tbl_name;
查看字段编码:SHOW FULL COLUMNS FROM tbl_name;