1. 查看默认的编码格式:
mysql>show variables like "%char%";
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | utf8 |
| character_set_system | utf8 |
mysql>set names utf8;
作用:SET character_set_client='utf8';
SET character_set_connection='utf8';
SET character_set_results='utf8';
2. 查看数据库的编码格式:
mysql>show create database test;
3. 修改数据库编码格式:
mysql>ALTER DATABASE test DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
4. 查看创建表语句及其编码格式:
mysql>show create table student;
5. 修改表的编码格式:
mysql>ALTER TABLE admin DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
CLIENT SECTION
[mysql]
default-character-set=utf8(作用跟set names utf8;一样,都是将client,connection,results字符集由Latin1转为utf8,不同的是set names utf8;仅仅对当前操作有效,重启mysql服务器恢复默认字符集Latin1)
为避免乱码,建库建表时设置字符集为utf8:
建库:
CREATE DATABASE `test` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
建表:
CREATE TABLE `database_user` () ENGINE=InnoDB DEFAULT CHARSET=utf8;
mysql>\s mysql>status 都可以显示数据库字符集信息
set character_set_connection = 'utf8';改变编码集
注:*****创建数据库,表均已设置编码集为utf8时,如果还不能在cmd窗口写入中文,则检查一下cmd默认编码集,若为gbk,则应:mysql>set names gbk;此时写入的中文数据可以被cmd正确解释