1、首先检查默认安装的字符集
- mysql> show variables like '%char%';
- +--------------------------+--------------------------------------------------------+
- | Variable_name | Value |
- +--------------------------+--------------------------------------------------------+
- | character_set_client | utf8 |
- | character_set_connection | utf8 |
- | character_set_database | latin1 |
- | character_set_filesystem | binary |
- | character_set_results | utf8 |
- | character_set_server | latin1 |
- | character_set_system | utf8 |
- | character_sets_dir | /usr/local/mysql-5.6.23-osx10.8-x86_64/share/charsets/ |
- +--------------------------+--------------------------------------------------------+
- 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/目录下
ls -al /etc/
检查了一下,发现本机没有my.cnf,查看了很多资料,他们说可以去/usr/local/mysql的安装目录下找到support-files,复制下面格式类似my-**.cnf的文件到/etc/目录下即可。命令如下:
cp /usr/local/mysql/support-files/my-default.cnf /etc/
3、修改/etc/my-default.cnf文件名为my.cnf,并修改里面的配置
cat /etc/my.cnf
查看配置文件的内容
ls -l /etc/my.cnf
查看文件的读写权限,如果为644(rw- r-- r--)则改为(664) (rw- rw- r--)
如果改为(666)(rw- rw- rw-)则修改以后配置文件不会生效。下面详细讲解为什么不会生效。
sudo chmod 664 /etc/my.cnf
输入密码即可。
4、修改配置文件内容
vi /etc/my.cnf
在文件最上面加上
- [client]
- default-character-set=utf8
在[mysqld]下增加
character-set-server=utf8
键入:wq!保存并退出 重新启动mysql
5、重新查看编码集
- mysql> show variables like '%char%';
- +--------------------------+--------------------------------------------------------+
- | 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/local/mysql-5.6.23-osx10.8-x86_64/share/charsets/ |
- +--------------------------+--------------------------------------------------------+
- 8 rows in set (0.00 sec)