mysql5中文乱码问题怎么办?

发布时间:2020-03-22编辑:脚本学堂
有关mysql5数据库中文乱码问题的解决办法,mysql数据库迁移时,原字符集是latin1,迁移后字符集是UTF8,结果就出现乱码了,这里分享了解决方法,一起来看下。

mysqlzhongwenluanma/ target=_blank class=infotextkey>mysql中文乱码问题
由于mysql是从版本 4迁移到version 5的,原来字符集是latin1,现在是utf8,即使通过phpadmin来访问也是乱码。

mysql数据乱码是由于字符集的变化和混乱引起的
 

复制代码 代码示例:

[root@localhost plugins]# /mysql -hlocalhost -ueygle -p eygle
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 1693469
Server version: 5.0.45 Source distribution

Type 'help;' or 'h' for help. Type 'c' to clear the buffer.

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

现在的mysql字符集是utf8,但是由于原来是latin1的字符集,我使用latin1进行导出:
 

mysqldump -hlocalhost -ueygle -p --default-character-set=latin1 blog >blog.sql

这样导出之后,如果打开看,正文应当都是正常显示才对,然后更改mysql字符集,将所有表的字符集设置改为utf8,在vim中的命令是:
 

:%s/latin1/utf8/g

再执行导入操作:
 

mysql -hlocalhost -ueygle -p eyglenew < blog.sql

修改后的mysql字符集:
 

复制代码 代码示例:
mysql> show variables like '%char%';
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | latin1                           |
| character_set_connection | latin1                           |
| character_set_database   | utf8                             |
| character_set_filesystem | binary                           |
| character_set_results    | latin1                           |
| character_set_server     | latin1                           |
| character_set_system     | utf8                             |
| character_sets_dir       | /opt/mysql/share/mysql/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

如此,mysql数据库中中文显示就正确了,成功解决mysql中文乱码的问题。