mysql的字符集统一设置问题_MySQL

WBOY
发布: 2016-06-01 13:44:57
原创
918 人浏览过

bitsCN.com

最近做了一个项目,过程中发生了数据库乱码,从phpmyadmin中怎么设置不知道。我去啊!!想了想肯定是字符集设置不一致,php编码是对了,并且都是采用utf-8的字符集设置的,所以肯定是mysql字符集设置的问题。又用phpmyadmin设置了半天还是不行。奶奶的,索性不用phpmyadmin了,真费劲!!
直接打开了mysql的控制台,哈哈弄好了,希望下边的操作对你们有帮助,记得回哦,大家一起进步!!!
一、查看 MySQL 数据库服务器和数据库字符集。 
 
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:/MySQL Server 5.0/share/charsets/ |...... 
-------------------------- ------------------------------------- ------ 
二、查看 MySQL 数据表(table) 的字符集。 
 
mysql> show table status from sqlstudy_db like '%countries%'; 
----------- -------- --------- ------------ ------ ----------------- ------ 
| Name      | Engine | Version | Row_format | Rows | Collation       |...... 
----------- -------- --------- ------------ ------ ----------------- ------ 
| countries | InnoDB |      10 | Compact    |   11 | utf8_general_ci |...... 
----------- -------- --------- ------------ ------ ----------------- ------ 
三、查看 MySQL 数据列(column)的字符集。 
 
mysql> show full columns from countries; 
---------------------- ------------- ----------------- -------- 
| Field                | Type        | Collation       | ....... 
---------------------- ------------- ----------------- -------- 
| countries_id         | int(11)     | NULL            | ....... 
| countries_name       | varchar(64) | utf8_general_ci | ....... 
| countries_iso_code_2 | char(2)     | utf8_general_ci | ....... 
| countries_iso_code_3 | char(3)     | utf8_general_ci | ....... 
| address_format_id    | int(11)     | NULL            | ....... 
---------------------- ------------- ----------------- -------- 
四、查看当前安装的 MySQL 所支持的字符集。 
 
mysql> show charset; 
mysql> show char set; 
---------- ----------------------------- --------------------- --------  
| Charset | Description                 | Default collation   | Maxlen | 
---------- ----------------------------- --------------------- --------  
| big5     | Big5 Traditional Chinese    | big5_chinese_ci     |      2 | 
| dec8     | DEC West European           | dec8_swedish_ci     |      1 | 
| cp850    | DOS West European           | cp850_general_ci    |      1 | 
| hp8      | HP West European            | hp8_english_ci      |      1 | 
| koi8r    | KOI8-R Relcom Russian       | koi8r_general_ci    |      1 | 
| latin1   | cp1252 West European        | latin1_swedish_ci   |      1 | 
| latin2   | ISO 8859-2 Central European | latin2_general_ci   |      1 | 
| swe7     | 7bit Swedish                | swe7_swedish_ci     |      1 | 
| ascii    | US ASCII                    | ascii_general_ci    |      1 | 
| ujis     | EUC-JP Japanese             | ujis_japanese_ci    |      3 | 
| sjis     | Shift-JIS Japanese          | sjis_japanese_ci    |      2 | 
| hebrew   | ISO 8859-8 Hebrew           | hebrew_general_ci   |      1 | 
| tis620   | TIS620 Thai                 | tis620_thai_ci      |      1 | 
| euckr    | EUC-KR Korean               | euckr_korean_ci     |      2 | 
| koi8u    | KOI8-U Ukrainian            | koi8u_general_ci    |      1 | 
| gb2312   | GB2312 Simplified Chinese   | gb2312_chinese_ci   |      2 | 
| greek    | ISO 8859-7 Greek            | greek_general_ci    |      1 | 
| cp1250   | Windows Central European    | cp1250_general_ci   |      1 | 
| gbk      | GBK Simplified Chinese      | gbk_chinese_ci      |      2 | 
| latin5   | ISO 8859-9 Turkish          | latin5_turkish_ci   |      1 | 
| armscii8 | ARMSCII-8 Armenian          | armscii8_general_ci |      1 | 
| utf8     | UTF-8 Unicode               | utf8_general_ci     |      3 | 
| ucs2     | UCS-2 Unicode               | ucs2_general_ci     |      2 | 
| cp866    | DOS 俄语                 | cp866_general_ci    |      1 | 
|密钥BCS2 | DOS Kamenicky 捷克-斯洛伐克 | keybcs2_general_ci | keybcs2_general_ci |      1 | 
|麦斯 | Mac 中欧 |       | macce_general_ci    |      1 | 
|宏观人| Mac 西欧           |宏曼_将军_ci |      1 | 
| cp852    | DOS 中欧 |       | cp852_general_ci    |      1 | 
|拉丁7 | ISO 8859-13 波罗的海          | latin7_general_ci | | latin7_general_ci      1 | 
| cp1251 | | Windows 西里尔字母            | cp1251_general_ci | |      1 | 
| cp1256 | | Windows 阿拉伯语              | cp1256_general_ci | |      1 | 
| cp1257 | | Windows 波罗的海              | cp1257_general_ci | |      1 | 
|二进制 |二进制伪字符集       |二进制              |      1 | 
|地理标准8 | GEOSTD8 格鲁吉亚语            | geostd8_general_ci | geostd8_general_ci |      1 | 
| cp932    |适用于 Windows 日语的 SJIS | cp932_japanese_ci | cp932_japanese_ci      2 | 
| eucjpms | Windows 版 UJIS 日语 | eucjpms_japanese_ci |      3 | 
---------- ----------------------------- -------- ------------- --------  
以上查看 MySQL 字符集命令,适用于 Windows & Linux。 
 
  
 
Liunx下修改MySQL字符集: 
1.查找MySQL的cnf文件的位置 
find / -iname '*.cnf' -print 
 
/usr/share/mysql/my-innodb-heavy -4G.cnf
/usr/share/mysql/my-large.cnf
/usr/share/mysql/my-small.cnf
/usr/share/mysql/my-medium.cnf
/usr/share/mysql/my-huge.cnf
/usr/share/texmf/web2c/texmf.cnf
/usr/share/texmf/web2c/mktex.cnf
/usr/ share/texmf/web2c/fmtutil.cnf
/usr/share/texmf/tex/xmltex/xmltexfmtutil.cnf 
/usr/share/texmf/tex/jadetex/jadefmtutil.cnf
/usr/share /doc/MySQL-server-community-5.1.22/my-innodb-heavy-4G.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-large.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-small.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-medium.cnf
/usr/share/doc/MySQL-server-community-5.1.22/my-huge.cnf 
 
2. 拷贝small.cnf、my-medium.cnf、my-huge.cnf、my-innodb -heavy-4G.cnf其中的一个到/etc下,命名为my.cnf
cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
 
3. 修改my.cnf
vi /etc/my.cnf
在[client]下添加
default-character-set=utf8
在[mysqld]下添加
default-character-set= utf8
 
4.重新启动 MySQL
[root@bogon ~]# /etc/rc.d/init.d/mysql restart 
关闭 MySQL                                         [ 确定 ] 
启动 MySQL。                                           [ 确定 ] 
[root@bogon ~]# mysql -u root -p 
输入密码: 
欢迎使用 MySQL 监控。命令以 ; 结尾或/g。 
您的 MySQL 连接 ID 是 1
Server version: 5.1.22-rc-community-log MySQL Community Edition (GPL) 
Type 'help;' or '/h' for help. Type '/c' to clear the buffer. 
 
其他的一些设置方法: 
 
修改数据库的字符集 
mysql>use mydb 
mysql>alter database mydb character set utf-8; 
创建数据库指定数据库的字符集 
mysql>create database mydb character set utf-8; 
 
通过配置文件修改: 
修改/var/lib/mysql/mydb/db.opt 
default-character-set=latin1
default-collation=latin1_swedish_ci
为 
default-character-set=utf8
default-collation=utf8_general_ci
重起MySQL: 
[root@bogon ~]# /etc/rc.d/init.d/mysql restart 
 
通过MySQL命令行修改: 
mysql> set character_set_client=utf8; 
Query OK, 0 rows affected (0.00 sec) 
mysql> set character_set_connection=utf8; 
Query OK, 0 rows affected (0.00 sec) 
mysql> set character_set_database=utf8; 
Query OK, 0 rows affected (0.00 sec) 
mysql> set character_set_results=utf8; 
Query OK, 0 rows affected (0.00 sec) 
mysql> set character_set_server=utf8; 
Query OK, 0 rows affected (0.00 sec) 
mysql> set character_set_system=utf8; 
Query OK, 0 rows affected (0.01 sec) 
mysql> set collation_connection=utf8; 
Query OK, 0 rows affected (0.01 sec) 
mysql> set collation_database=utf8; 
Query OK, 0 rows affected (0.01 sec) 
mysql> set collation_server=utf8; 
Query OK, 0 rows affected (0.01 sec) 
 
作者“You_Can”

bitsCN.com
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板