Ruby on Rails 敏捷开发最佳实践分享
分享你的敏捷开发最佳实践,为rails社区的枝繁叶茂做出自己的贡献.
设置Mysql数据库的默认编码为utf8
![](http://www.gravatar.com/avatar/6dfeddd9c8263e4fa53fc377ba0832c7.jpg?alt=sunny&s=30)
mysql的默认编码为latin1,存储中文时总会出现乱码,每次改过了,下次又忘了该怎么配,把它记录下来,也分享给各位。
进入mysql,查看默认编码:
$ mysql -uroot -proot
$ show variables like character%'
显示字符编码:
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
现暂停mysql服务:
$sudo /etc/init.d/mysql stop
修改 /etc/mysql/my.cnf
$ sudo chmod 777 /etc/mysql/my.cnf
$ vim /etc/mysql/my.cnf
在client下面加上:
default-character-set=utf8
在mysqld下面加上:
default-character-set=utf8
init_connect='SET NAMES utf8'
保存,退出wq
重启mysql服务:
$sudo /etc/init.d/mysql start
发现出错:
Warning: World-writable config file '/etc/mysql/my.cnf' is ignored
/usr/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'luoping'@'localhost' (using password: NO)'
原因为:/etc/mysql/my.cnf 权限太高,搜索了一番,有人说许改为600,但是我发觉不行,查看了/etc/mysql/文件夹里的其他文件,权限是755,所以修改权限
$sudo chmod 755 /etc/mysql/my.cnf
重启mysql服务,OK
再次查看数据库编码
$ mysql -uroot -proot
$ show variables like character%'
显示字符编码:
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
相关最佳实践
<script></script>
Select from these web-based feed readers:
Done
Message sent!
Share again.
![](http://s7.addthis.com//images/redloading.gif)
Sending message...
AddThis for Firefox
Make sharing easier with AddThis for Firefox.
Bookmark & Share
X
Make sharing easier with AddThis for Firefox.
Facebook
Twitter
Myspace
Print
Google
Favorites
Digg
Delicious
StumbleUpon
Messenger
Email
More... (322)