我安装的rails1.9.3默认是mysql2数据库,这个性能究竟比mysql强在哪里,没研究过,也不太会用,看网上有说在windows环境下性能体现不出来,所以还是想用mysql数据库连接, 步骤如下:
1. 首先卸载掉mysql2:
gem uninstall mysql2
2. 安装mysql ruby驱动,需要指定参数,否则以后启动会报错:
gem install mysql --platform=ruby -- '--with-mysql-lib="[mysql安装路径]\lib" --with-mysql-include="[mysql安装路径]\include" --with-mysql-dir="[mysql安装路径]"'
3. 修改database.yml文件:
adapter: mysql2 改为:mysql
4. 修改项目的gemfile:
注释掉下面一行:
# gem mysql2
改为:
gem mysql
5. 用下面的程序测试mysql数据库链接, 需在mysql中先见一个railsTest的database,然后run下面的测试脚本:
MySqlTest.rb:
class MysqlTest
#Code here
require "mysql"
def testMysql
dbc=Mysql.real_connect('localhost','root','fm0vrwc','mysql')
res=dbc.query('select * from user')
puts "Test Mysql...."
while row=res.fetch_row do
puts "#{row[0]}"
end
end
def createTable
dbc=Mysql.real_connect('localhost','root','fm0vrwc','railsTest')
dbc.query("drop table if exists users")
dbc.query("create table users(id int,name char(20), age int)")
dbc.query("insert into users values(1,'Tom', 30)")
dbc.query("insert into users values(2,'Art', 50)")
dbc.query("insert into users values(3,'Kevin', 40)")
printf "Create Table........"
printf "%d rows were inserted\n",dbc.affected_rows
res = dbc.query("SELECT * FROM users")
puts "===Select Data===\n"
while row = res.fetch_row do
printf "%d, %s, %d\n", row[0], row[1] , row[2]
end
puts "==================\n"
puts "Server version: " + dbc.get_server_info
rescue Mysql::Error => e
puts "Error code: #{e.errno}"
puts "Error message: #{e.error}"
puts "Error SQLSTATE: #{e.sqlstate}" if e.respond_to?("sqlstate")
ensure
puts "Close Connection......."
dbc.close if dbc
end
(MysqlTest.new).testMysql
(MysqlTest.new).createTable
end
6. 测试输出结果为:
C:\Ruby193\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) -Itest C:/Users/q/RubymineProjects/railTest2/test/MySqlTest.rb --name=testMysql
Testing started at 12:46 PM ...
Test Mysql....
localhost
127.0.0.1
::1
%
Create Table........1 rows were inserted
===Select Data===
1, Tom, 30
2, Art, 50
3, Kevin, 40
==================
Server version: 5.6.11
Close Connection.......
Process finished with exit code 0