1.安裝好railsinstaller後,先修改gem的源。
1.1查看源
gem sources
1.2移除原有的:https://rubygems.org,由於使用原有的源來用gem安裝的時候,一直報連接失敗的原因,我們改成淘寶的源。
gem sources -r https://rubygems.org
1.3添加gem的淘寶源
gem sources -a https://ruby.taobao.com
添加成功後,我們來安裝mysql和mysql2
前提,本機已經安裝了mysql-5.6.24-win32。在用gem安裝mysql和mysql2,需要將配置路徑指導本機mysql的安裝根目錄,我本機的mysql安裝在:D:\software\MySql
所以安裝mysql和mysql2的時候,使用如下命令:
# #mysql
gem install mysql --platform=ruby -- --with-opt-dir=D:\software\MySql
# #mysql2
gem install mysql2 --platform=ruby -- --with-mysql-dir=D:\software\MySql
安裝成功後顯示如下:
D:\>gem install mysql --platform=ruby -- --with-opt-dir=D:\software\MySql
Fetching: mysql-2.9.1.gem (100%)
Temporarily enhancing PATH to include DevKit...
Building native extensions with: '--with-opt-dir=D:\software\MySql'
This could take a while...
Successfully installed mysql-2.9.1
Parsing documentation for mysql-2.9.1
Installing ri documentation for mysql-2.9.1
Done installing documentation for mysql after 0 seconds
1 gem installed
安裝完成後,記得一定,要將,下圖的兩個文件(libmysql.dll,libmysql.lib)
拷貝到ruby的根目錄下下的bin目錄裏面,如下圖:
如果上面安裝的時候,指定了其它版本的mysql源,在運行的時候,會報如下錯誤:
D:/software/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:
in `require': Incorrect MySQL client library version!
This gem was compiled for 5.6.24 but the client library is 6.1.6. (RuntimeError)
from D:/software/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from D:/software/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/mysql-2.9.1/lib/mysql.rb:6:in `rescue in <top (required)>'
from D:/software/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/mysql-2.9.1/lib/mysql.rb:2:in `<top (required)>'
from D:/software/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:135:in `require'
from D:/software/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:135:in `rescue in require'
from D:/software/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:144:in `require'
from a.rb:4:in `<main>'
切記,一定要配置成本機安裝的mysql根路徑哦,不要從網上下載其它版本。
然後用ruby編寫如下命令:
# -*- coding: UTF-8 -*-
require "dbi"
require 'mysql'
class Person
def say
# need dbi
begin
# 連接到 MySQL 服務器
dbh = DBI.connect("DBI:Mysql:jucheap:localhost",
"root", "123")
# 獲取服務器版本字符串,並顯示
row = dbh.select_one("SELECT VERSION()")
puts "Server version: " + row[0]
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
ensure
# 斷開與服務器的連接
dbh.disconnect if dbh
end
#need mysql
begin
db = Mysql.init
db.options(Mysql::SET_CHARSET_NAME, 'utf8')
db = Mysql.real_connect("127.0.0.1", "root", "123", "jucheap", 3306)
db.query("SET NAMES utf8")
db.query("drop table if exists tb_test")
db.query("create table tb_test (id int,text LONGTEXT) ENGINE=MyISAM DEFAULT CHARSET=utf8")
db.query("insert into tb_test (id, text) values (1,'first line'),(2,'second line')")
printf "%d rows were inserted\n",db.affected_rows
rslt = db.query("select text from tb_test")
while row = rslt.fetch_row do
puts row[0]
end
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
db.close if db
end
end
end
jack = Person.new
jack.say()