A-A+

Liunx如何修改MySQL字符集

2017年10月26日 PHP技术文章 暂无评论 阅读 0 views 次

今天发现在linux中我mysql数据库中的中文全部都是乱码了,我查看了mysql字符集默认为latin1了,下面我来给大家介绍几种修改方法.

Linux下使用service mysql restart命令重启MySql服务,新建的数据库都会使用到这个字符集做为默认字符集,修改MySql服务器各个数据库的字符集,如果在设置服务器字符集之前,存在了数据库,需要更改数据库的字符集,代码如下:

use datatable_name;

alter database character set gbk;

改完字符集的其它问题,字符集修改好了以后发现使用存储过程做新增操作时,还会出现中文乱码,但是直接写SQL语句就不会出现乱码.

查看字符集设置,代码如下:

  1. mysql> show variables like 'collation_%';   
  2. +----------------------+-----------------+   
  3. | Variable_name         | Value            |   
  4. +----------------------+-----------------+   
  5. | collation_connection | utf8_general_ci |   
  6. | collation_database    | utf8_general_ci |   
  7. | collation_server      | utf8_general_ci |   
  8. +----------------------+-----------------+   
  9. rows in set (0.02 sec)   
  10. mysql> show variables like 'character_set_%';   
  11. +--------------------------+----------------------------+   
  12. | Variable_name             | Value                       |   
  13. +--------------------------+----------------------------+   
  14. | character_set_client      | utf8                        |   
  15. | character_set_connection | utf8                        |   
  16. | character_set_database    | utf8                        |   
  17. | character_set_filesystem | binary                      |   
  18. | character_set_results     | utf8                        |   
  19. | character_set_server      | utf8                        |   
  20. | character_set_system      | utf8                        |   
  21. | character_sets_dir        | /usr/share/mysql/charsets/ |   
  22. +--------------------------+----------------------------+   
  23. rows in set (0.02 sec)   
  24. mysql>  

1.查找MySQL的cnf文件的位置,代码如下:

  1. find / -iname '*.cnf' -print   
  2. /usr/share/mysql/my-innodb-heavy-4G.cnf   
  3. /usr/share/mysql/my-large.cnf   
  4. /usr/share/mysql/my-small.cnf   
  5. /usr/share/mysql/my-medium.cnf   
  6. /usr/share/mysql/my-huge.cnf   
  7. /usr/share/texmf/web2c/texmf.cnf   
  8. /usr/share/texmf/web2c/mktex.cnf   
  9. /usr/share/texmf/web2c/fmtutil.cnf   
  10. /usr/share/texmf/tex/xmltex/xmltexfmtutil.cnf   
  11. /usr/share/texmf/tex/jadetex/jadefmtutil.cnf   
  12. /usr/share/doc/MySQL-server-community-5.1.22/my-innodb-heavy-4G.cnf   
  13. /usr/share/doc/MySQL-server-community-5.1.22/my-large.cnf   
  14. /usr/share/doc/MySQL-server-community-5.1.22/my-small.cnf   
  15. /usr/share/doc/MySQL-server-community-5.1.22/my-medium.cnf   
  16. /usr/share/doc/MySQL-server-community-5.1.22/my-huge.cnf   

2.拷贝,代码如下:

small.cnf、my-medium.cnf、my-huge.cnf、my-innodb-heavy-4G.cnf其中的一个到/etc下,命名为my.cnf

cp /usr/share/mysql/my-medium.cnf /etc/my.cnf

3.修改my.cnf,代码如下:

vi /etc/my.cnf

在[client]下添加

default-character-set=utf8

在[mysqld]下添加

default-character-set=utf8

4.重新启动MySQL,代码如下:

  1. [root@bogon ~]# /etc/rc.d/init.d/mysql restart   
  2. Shutting down MySQL          [ 确定 ]   
  3. Starting MySQL.                                     [ 确定 ]   
  4. [root@bogon ~]# mysql -u root -p   
  5. Enter password:  --www.xiariboke.net   
  6. Welcome to the MySQL monitor. Commands end with ; or g.   
  7. Your MySQL connection id is 1   
  8. Server version: 5.1.22-rc-community-log MySQL Community Edition (GPL)   
  9. Type 'help;' or 'h' for help. Type 'c' to clear the buffer.  
标签:

给我留言