cleey
望着那一丝海线,若隐若现。落日下的海霞,数不尽的美,看不完的醉
Linux下MySQL数据库乱码

安装完的MySQL,默认字符集为 latin1 。如果数据库中有中文就会乱码,为了让数据库支持中文,我们需要修改mysql配置文件字符集设置为utf8,Linux下修改my.cnf

  • 查看mysql字符集,在mysql命令行下:
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

可以看到mysql默认字符集为 latin1,那么我们来修改下支持中文。

  • 修改mysql配置文件,vim /etc/my.cnf
[client]
default-character-set=utf8 ##字符集

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
character-set-server=utf8 ##字符集
init_connect='SET NAMES utf8' ##设定连接mysql是使用UTF8编码


[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid


# mysql cache
query_cache_type = 1
query_cache_size = 10000000
  • 重启mysql,service mysqld restart

继续查看mysql的字符集,如下已改成中文

mysql> show variables like 'character%';
+--------------------------+----------------------------+
| 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       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

OK啦

还有一种方法,直接在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 ;


数据库不乱码了吧。

<< 上一篇 ubantu 忘记密码 Linux防火墙规则的查看、添加、删除和修改 下一篇 >>
文章标签
随意 | Created At 2014 By William Clinton | 蜀ICP备14002619号-4 |