且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

mac下修改mysql默认字符集为utf8

更新时间:2022-04-16 18:31:31

1、首先检查默认安装的字符集

[html] view plain copy  print?
  1. mysql> show variables like '%char%';  
  2. +--------------------------+--------------------------------------------------------+  
  3. | Variable_name            | Value                                                  |  
  4. +--------------------------+--------------------------------------------------------+  
  5. | character_set_client     | utf8                                                   |  
  6. | character_set_connection | utf8                                                   |  
  7. | character_set_database   | latin1                                                   |  
  8. | character_set_filesystem | binary                                                 |  
  9. | character_set_results    | utf8                                                   |  
  10. | character_set_server     | latin1                                                   |  
  11. | character_set_system     | utf8                                                   |  
  12. | character_sets_dir       | /usr/local/mysql-5.6.23-osx10.8-x86_64/share/charsets/ |  
  13. +--------------------------+--------------------------------------------------------+  
  14. 8 rows in set (0.00 sec)  

character_set_database和character_set_server依然是latin1的字符集,也就是说mysql后续创建的表都是latin1字符集的,不是utf8,会造成一些麻烦。所以有必要修改my.cnf,在修改my.cnf之前一定要关闭mysql进程,不然会遇到mysql的sock不能连接的问题。


2、查看是否有my.cnf文件在/etc/目录下

[html] view plain copy  print?mac下修改mysql默认字符集为utf8mac下修改mysql默认字符集为utf8
  1. ls -al /etc/  

检查了一下,发现本机没有my.cnf,查看了很多资料,他们说可以去/usr/local/mysql的安装目录下找到support-files,复制下面格式类似my-**.cnf的文件到/etc/目录下即可。命令如下:

[html] view plain copy  print?mac下修改mysql默认字符集为utf8mac下修改mysql默认字符集为utf8
  1. cp /usr/local/mysql/support-files/my-default.cnf /etc/  


3、修改/etc/my-default.cnf文件名为my.cnf,并修改里面的配置

[html] view plain copy  print?mac下修改mysql默认字符集为utf8mac下修改mysql默认字符集为utf8
  1. cat /etc/my.cnf  

查看配置文件的内容

[html] view plain copy  print?mac下修改mysql默认字符集为utf8mac下修改mysql默认字符集为utf8
  1. ls -l /etc/my.cnf  

查看文件的读写权限,如果为644(rw- r-- r--)则改为(664) (rw- rw- r--)

如果改为(666)(rw- rw- rw-)则修改以后配置文件不会生效。下面详细讲解为什么不会生效。

[html] view plain copy  print?mac下修改mysql默认字符集为utf8mac下修改mysql默认字符集为utf8
  1. sudo chmod 664 /etc/my.cnf  

输入密码即可。


4、修改配置文件内容

[html] view plain copy  print?mac下修改mysql默认字符集为utf8mac下修改mysql默认字符集为utf8
  1. vi /etc/my.cnf  

在文件最上面加上

[html] view plain copy  print?mac下修改mysql默认字符集为utf8mac下修改mysql默认字符集为utf8
  1. [client]  
  2. default-character-set=utf8  

在[mysqld]下增加

[html] view plain copy  print?mac下修改mysql默认字符集为utf8mac下修改mysql默认字符集为utf8
  1. character-set-server=utf8  

键入:wq!保存并退出


5、重新查看编码集

[html] view plain copy  print?mac下修改mysql默认字符集为utf8mac下修改mysql默认字符集为utf8
  1. mysql> show variables like '%char%';  
  2. +--------------------------+--------------------------------------------------------+  
  3. | Variable_name            | Value                                                  |  
  4. +--------------------------+--------------------------------------------------------+  
  5. | character_set_client     | utf8                                                   |  
  6. | character_set_connection | utf8                                                   |  
  7. | character_set_database   | utf8                                                   |  
  8. | character_set_filesystem | binary                                                 |  
  9. | character_set_results    | utf8                                                   |  
  10. | character_set_server     | utf8                                                   |  
  11. | character_set_system     | utf8                                                   |  
  12. | character_sets_dir       | /usr/local/mysql-5.6.23-osx10.8-x86_64/share/charsets/ |  
  13. +--------------------------+--------------------------------------------------------+  
  14. 8 rows in set (0.00 sec)  


出现问题详解:

一开始我输入的命令为

[html] view plain copy  print?mac下修改mysql默认字符集为utf8mac下修改mysql默认字符集为utf8
  1. <span style="color:#ff0000;">chmod a+w /etc/my.cnf</span>  
这条命令的意思是my.cnf对所有用户都有写的权限,这样就变成了666了。导致我改完以后也不报错,也不生效。

直到我关闭了数据库服务,再进入数据库的时候,数据库出现一个错误

[html] view plain copy  print?mac下修改mysql默认字符集为utf8mac下修改mysql默认字符集为utf8
  1. <span style="color:#ff0000;">World-writable config file '/etc/my.cnf' is ignored</span>  

大概意思是权限全局可写,任何一个用户都可以写。mysql担心这种文件被其他用户恶意修改,所以忽略掉这个配置文件。

一下子我就懂了,打开数据库服务,修改成664后,再重启数据库,再查看编码已经成功修改。