ruby使用DBI连接MySQL数据库发生异常:in `error': Can't connect to MySQL server on 'localhost' (10061) (DBI::DatabaseError)

Ruby使用DBI连接MySQL数据库一般为:

require "dbi"
dbh = DBI.connect("dbi:Mysql:test:localhost", "root", "123456")
dbh.disconnect if dbh

  但是当mysql数据库端口改变时,如果直接在“dbi:Mysql:test:localhost”后面加上端口号,如“dbi:Mysql:test:localhost:10086”,将出现错误代码为2003的异常:

C:/Ruby193/lib/ruby/gems/1.9.1/gems/dbd-mysql-0.4.4/lib/dbd/Mysql.rb:106:in `error': Can't connect to MySQL server on 'localhost' (10061) (DBI::DatabaseError)
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dbd-mysql-0.4.4/lib/dbd/mysql/driver.rb:70:in `rescue in connect'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dbd-mysql-0.4.4/lib/dbd/mysql/driver.rb:32:in `connect'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dbi-0.4.5/lib/dbi/handles/driver.rb:33:in `connect'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/dbi-0.4.5/lib/dbi.rb:148:in `connect'

  应该写成:

dbh = DBI.connect("dbi:Mysql:database=test;host=localhost;port=10086", "root", "123456")

  其中“database=test;host=localhost;port=10086”三者顺序可任意交换。

  另外,出现错误“Can't connect to MySQL server on 'localhost' (10061) (DBI::DatabaseError)”的另一个可能原因是数据库服务未启动。

上一篇:MessageChannel 消息通道


下一篇:ubuntu16.04LTS更换阿里源