Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1966742
  • 博文数量: 606
  • 博客积分: 9991
  • 博客等级: 中将
  • 技术积分: 5725
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-17 19:07
文章分类

全部博文(606)

文章存档

2011年(10)

2010年(67)

2009年(155)

2008年(386)

分类: Mysql/postgreSQL

2010-12-25 15:49:06

安装完的MySQL的默认字符集为 latin1 ,为了要将其字符集改为用户所需要的(比如utf8),就必须改其相关的配置文件。

查看MySQL字符设置

mysql> show variables like 'character_set%' ;
+------------------------------------+----------------------------------+
| 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/ |
+------------------------------------+----------------------------------+

查找/etc目录下是否有my.cnf文件;
#ls -l | grep my.cnf (在/etc下查找是否有my.cnf文件存在)
如果没有就要从/usr/share/mysql,拷贝一个到/etc 下,在/usr/share/mysql目录下有五个后缀为.cnf的文件,分别是 my-huge.cnf     my-innodb-heavy-4G.cnf     my-large.cnf    my-medium.cnf      my-small.cnf ;从中随便拷贝一个到/etc目录下并将其改为my.cnf文件,我选择的是my-medium.cnf :
#cp /usr/share/mysql/my-medium.cnf    /etc/my.cnf
修改my.cnf文件,在该文件中的三个地方加上 default-character-set=utf8([client] [mysqld] [mysql])

              #vi /etc/my.cnf

              修改如下:(红色为添加部分)

              [client]
             #password        = your_password
              port             = 3306
              socket           = /var/lib/mysql/mysql.sock
             default-character-set=utf8 (经过验证好像这一步不用设置也可以达到效果)

              [mysqld]
              port             = 3306
              socket           = /var/lib/mysql/mysql.sock
              skip-locking
              key_buffer = 16M
              max_allowed_packet = 1M
              table_cache = 64
              sort_buffer_size = 512K
              net_buffer_length = 8K
              read_buffer_size = 256K
              read_rnd_buffer_size = 512K
              myisam_sort_buffer_size = 8M
             default-character-set=utf8
              init_connect='SET NAMES utf8'

              [mysql]
              no-auto-rehash
             default-character-set=utf8
              保存退出;
重起MySQL服务器,使其设置的内容生效

如果重启的过程中就出会错误

 Starting MySQL............The server quit without updating PID file (/var/lib/mysql/node1.pid). [失败]

  查一下 /var/lib/mysql/node1.error 错误文件会发现

101004 20:23:38 [ERROR] /usr/sbin/mysqld: unknown variable 'default-character-set=utf8'

这是因为 mysqld 不能识别这种格式的 可以将 [ mysqld ] 下的 default-character-set=utf8

重新登入mysql;
mysql> show variables like 'character_set%' ;
+----------------------------------+---------------------------------+
| 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/ |
+-----------------------------------+----------------------------------+

mysql> set NAMES 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_server=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like ‘character%’;

阅读(1100) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~