1.Linux下MySQL的安装
引用
sudo apt-get install mysql-server libmysql-ruby
查看版本号确定是否安装完成。
可选安装Ruby语言数据库通用接口包及其MySQL接口包:
引用
apt-get install libdbi-ruby1.8 libdbi-ruby libdbd-mysql-ruby1.8 libdbd-mysql-ruby
检查安装结果:
irb(main):001:0> require 'dbi'
=> true
irb(main):002:0> exit
安装MySQL接口工具包
引用
sudo apt-get install libmysqlclient15-dev zlib1g-dev
sudo apt-get install libdbm-ruby1.8 libfcgi-ruby1.8 libfcgi0c2 libgdbm-ruby1.8 libopenssl-ruby1.8 libruby1.8-dbg
安装MySQL API接口包
引用
sudo gem install mysql -r
检查安装结果:
irb(main):001:0> require 'mysql'
=> true
irb(main):002:0> require 'rubygems'
=> true
irb(main):004:0> require_gem 'mysql'
=> true
irb(main):005:0> exit
2.设置UTF8编码
(1)确定mysql数据库编码是utf8
(2)database.yml里面增加encoding: utf8
(3)确定编辑器的保存文件格式为:UTF-8,保证rhtml文件编码是UTF-8
2,3条很好解决,MySQL编码的问题解决如下:
在linux下修改3个my.cnf的1个/etc/mysql/my.cnf文件
找到客户端配置[client] 在下面添加
default-character-set=utf8 默认字符集为utf8
在找到[mysqld] 添加
default-character-set=utf8 默认字符集为utf8
init_connect='SET NAMES utf8' (设定连接mysql数据库时使用utf8编码,以让mysql数据库为utf8运行)
修改好后,重新启动mysql 即可,查询一下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/ |
+--------------------------+----------------------------+
这样就不用每次连接 都使用 --default-character-set=utf8 了
3.安装MySQL前端
PHP下有phpmyadmin,Rails下也有railsmyadmin。安装方法见
http://code.google.com/p/railsmyadmin/
首先在应用中安装railsmyadmin
引用
.script/plugin install http://railsmyadmin.googlecode.com/svn/trunk/my_admin/
插件安装好后,生成RailsMyAdmin文件(这会把插件文件夹拷进应用文件夹)
引用
.script/generate my_admin
将下列代码加入environment.rb文件的底部,重启应用
## MY ADMIN CONFIG
require 'my_admin/my_admin_tool'
# If you only want certain models to be available to RailsMyAdmin,
# set :all_models to false and specify the desired models in MY_ADMIN_MODELS
MY_ADMIN_GLOBALS = {:all_models => true, :confirm_destroy => false}
# Uncomment the following line if you set :all_models to false above.
#MY_ADMIN_MODELS = [User, Content]
# Replace [User, Content] with your desired array of model classes that
# RailsMyAdmin should be restricted to.
# MY_ADMIN_AUTH must define a Proc object that takes as a paramater
# an ApplicationController instance variable (c - in the example below).
# If you have a method defined in your ApplicationController,
# 'admin_logged_in?' for example, the following sample code will
# authenticate against that method and only allow visitors to
# view RailsMyAdmin if the 'admin_logged_in?' method returns true.
MY_ADMIN_AUTH = Proc.new { |c| c.send('admin_logged_in?') }
使用方法如下:
http://localhost:3000/my_admin/main
命令列表:获得一个其他RailsMyAdmin的rake任务列表
引用
rake -T my_admin