Home > Database > Mysql Tutorial > 解决mysql的默认字符编码和中文乱码问题

解决mysql的默认字符编码和中文乱码问题

WBOY
Release: 2016-06-07 17:52:00
Original
1079 people have browsed it

文章详细的介绍了解决mysql的默认字符编码和中文乱码问题 ,有需要了解的朋友可参考一下。

MySQL的默认编码是Latin1,不支持中文,那么如何修改MySQL的默认编码呢,下面以UTF-8为例来说明

启动mysql后,以root登录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/ |
+--------------------------+----------------------------+
这是安装后,默认的字符编码(图为Solaris下的截图,windows的类似)

需要注意的是,要修改的地方非常多,相应的修改方法也很多。下面是一种最简单最彻底的方法:

一、Windows
1、中止MySQL服务
2、在MySQL的安装目录下找到my.ini,如果没有就把my-medium.ini复制为一个my.ini即可
3、打开my.ini以后,在[client]和[mysqld]下面均加上default-character-set=utf8,保存并关闭
4、启动MySQL服务

二、Linux
1、中止MySQL服务(bin/mysqladmin -u root shutdown -p)
2、在/etc/下找到my.cnf,如果没有就把MySQL的安装目录下的support-files目录下的my-large.cnf复制到/etc/下并改名为my.cnf即可
3、打开my.cnf以后,修改以下,保存并关闭

         找到客户端配置[client] 在下面添加
        default-character-set=utf8 默认字符集为utf8
        在找到[mysqld] 添加
        default-character-set=utf8 默认字符集为utf8
        init_connect='SET NAMES utf8' (设定连接mysql时使用utf8编码,以让mysql数据库为utf8运行)

4、启动MySQL服务(bin/mysqld_safe &),重新查看编码显示

    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/ |
+--------------------------+----------------------------+
修改后的编码显示(图为Solaris下的截图,windows的类似)

非常简单,这样的修改一劳永逸,今后MySQL一切相关的默认编码均为UTF-8了,创建新表格的时候无需再次设置

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template