mysql数据库的字符编码设置
发布:smiling 来源: PHP粉丝网 添加日期:2015-04-18 11:09:14 浏览: 评论:0
对于mysql数据库来讲编码是一个比较重要的参数了,特别在以前低版本的mysql中,如果编码未设置好中文就会是乱码了,下面我给各位整理了一篇关于mysql数据库的字符编码设置教程供各位参考.
默认登录mysql之后show variables like ‘%character%’,出现:
- mysql》 show variables like ‘%character%’;
- +--------------------------+----------------------------+
- | Variable_name | Value | --phpfensi.com
- +--------------------------+----------------------------+
- | 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/ |
- +--------------------------+----------------------------+
1. 找到mysql的配置文件,拷贝到etc目录下,第一步很重要,使用find命令查找cnf文件,记得不要去找my.cnf因为它不一定存在,而应该找*.cnf.
- [root@linuxserver classes] find / -iname *.cnf -print
- /usr/share/doc/mysql-server-4.1.12/my-medium.cnf
- /usr/share/doc/mysql-server-4.1.12/my-huge.cnf
- /usr/share/doc/mysql-server-4.1.12/my-innodb-heavy-4G.cnf
- /usr/share/doc/mysql-server-4.1.12/my-large.cnf
- /usr/share/doc/mysql-server-4.1.12/my-small.cnf
- /usr/share/texmf/web2c/mktex.cnf
- /usr/share/texmf/web2c/texmf.cnf
- /usr/share/texmf/web2c/fmtutil.cnf
- /usr/share/texmf/tex/jadetex/jadefmtutil.cnf
- /usr/share/ssl/openssl.cnf
找到之后,复制一个出来:
cp /usr/share/doc/mysql-server-4.1.12/my-large.cnf /etc/my.cnf
2.打开my.cnf修改编码:
vi /etc/my.cnf
在[client]下增加default-character-set=utf8,在[mysqld]下增加default-character-set=utf8,同时加上init_connect=‘SET NAMES utf8’(设定连接mysql数据库时使用utf8编码,以让mysql数据库为utf8运行)
重新启动mysql,执行:sudo /etc/init.d/mysql stop 然后sudo /etc/init.d/mysql start.
- mysql》 show variables like ‘%character%’;
- +--------------------------+----------------------------+
- | Variable_name | Value |
- +--------------------------+----------------------------+
- | character_set_client | utf8 | --phpfensi.com
- | 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/ |
- +--------------------------+----------------------------+
即使做了以上修改如果直接数据库再创建表,然后存入中文,取出来的会是问号,解决的办法是.
创建数据库的时候指明默认字符集为utf8,如:
create database test default character set utf8;
Tags: mysql字符编码 mysql编码设置
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)