当前位置:Gxlcms > mysql > MySQL统一编码

MySQL统一编码

时间:2021-07-01 10:21:17 帮助过:117人阅读

主要是解决中文乱码的问题,下面的命令是统一成gb2312的middot;middot;要用gbk的话将gb2312换成gbk就行了 。进入命令行模式,如

主要是解决中文乱码的问题,下面的命令是统一成gb2312的··要用gbk的话将gb2312换成gbk就行了 。

进入命令行模式,,

如果MySQL数据库已经安装好,可以使用下列SQL命令查看MySQL当前的字符集设置:
mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
7 rows in set (0.00 sec)

mysql> SHOW VARIABLES LIKE 'collation_%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | latin1_swedish_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec)


依次执行:

set character_set_client =gb2312;

set character_set_connection =gb2312;

set character_set_database =gb2312;

set character_set_results =gb2312;

set character_set_server =gb2312;

set character_set_system =gb2312; --此处utf-8也可以

然后执行:

SET collation_server = gb2312_chinese_ci


SET collation_database = gb2312_chinese_ci

SET collation_connection =gb2312_chinese_ci

-----------------------------------以下是查看和修改MSQL数据库、表、字段编码的命令---------------------

mysql在创建数据库时默认指定了database,table,column的字符集;

当现有database支持的字符集不符合条件时,可以进行修改,需要将database,table,column的字符集均进行修改。在允许的情况下重新建database比较简单,

重新建立database:
1。查看数据库支持的字符集
mysql>SHOW CHARACTER SET
2.创建数据库
create database db_name DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
或create database db_name DEFAULT CHARACTER SET gbk ;
3.创建table,则table的缺省字符集同database

修改已有的database和table
1。查看数据库支持的字符集:
mysql>show create database lian;
或 mysql>show create database lian\G;
2. 修改数据库的字符集:
mysql>alter DATABASE lian DEFAULT CHARACTER SET gbk;
注:在修改后的database中建立table,则新的table使用同数据库一致的字符集
3. 查看table的字符集,注意column的字符集
mysql>show create table user_info;
4.修改table的字符集
mysql>alter TABLE user_info DEFAULT CHARACTER SET gbk;
5.修改column的字符集
mysql>ALTER TABLE user_info MODIFY id varchar(50) character set gbk NOT NULL;

linux

人气教程排行