---
title: "rails 3  connect to mysql db"
author: "xiayw"
date: "2011-04-27T16:06:05Z"
tags: ["ActiveRecord", "MySQL", "Rails", "Ruby", "jruby"]
categories: ["编程语言"]
slug: "rails-3-connect-to-mysql-db"
draft: false
lastmod: "2011-08-09T15:33:07Z"
id: 1020184
url: "https://xiayw.iteye.com/blog/1020184"
---

其实很简单，不过自己 没有弄明白罢了

首先  
jruby -S gem install active\_record

jruby -S gem install activerecord-jdbcmysql-adapter

一般你装 rails 得时候yong bundle 就会装一坨东西了。 呵呵  
然后 在你得 Gemfile  （ Rails application in /somewhere）

platforms :jruby do  
  gem 'activerecord-jdbc-adapter', :require =&gt; false

end

\# Production  
group :production do  
  platforms :jruby do  
    gem 'jdbc-mysql'  
  end  
  platforms :ruby do  
    gem 'mysql2'  
  end  
end

最后 database.yml （ Rails application in /somewhere/config/)  
production:  
  pool: 5  
  timeout: 5000  
  username: username  
  password: password  
   socket: /var/run/mysqld/mysqld.sock  
   driver: com.mysql.jdbc.Driver  
   url: jdbc:mysql://127.0.0.1:3306/hello\_development  
  adapter: mysql  
  encoding: utf8  
  reconnect: false

  database: depot\_production

注意红色得一定要写对了 我自己得 socket 是在这个位置你的可能不一样  
找找是下面哪个  
ls -l  试试 就能找到了 或者你用 find 找找。

conn\[:socket] = Pathname.glob(%w[  
  /opt/local/var/run/mysql5/mysqld.sock  
  /tmp/mysqld.sock  
  /tmp/mysql.sock  
  /var/mysql/mysql.sock  
  /var/run/mysqld/mysqld.sock  
]).find { |path| path.socket? }.to\_s

我之前 没有写对 host 报错：

jruby -S rake db:migrate RAILS\_ENV="production" --trace  
(in /home/chenjing/Myrails/depot)  
\** Invoke db:migrate (first\_time)  
\** Invoke environment (first\_time)  
\** Execute environment  
\** Execute db:migrate  
rake aborted!  
The driver encountered an unknown error: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.  
/home/chenjing/jruby-1.6.0/lib/ruby/gems/1.9/gems/activerecord-jdbc-adapter-1.1.1/lib/arjdbc/jdbc/connection.rb:93:in \`initialize'  
/home/chenjing/jruby-1.6.0/lib/ruby/gems/1.9/gems/activerecord-jdbc-adapter-1.1.1/lib/arjdbc/jdbc/adapter.rb:31:in \`initialize'  
/home/chenjing/jruby-1.6.0/lib/ruby/gems/1.9/gems/activerecord-jdbc-adapter-1.1.1/lib/arjdbc/mysql/adapter.rb:392:in \`initialize'  
/home/chenjing/jruby-1.6.0/lib/ruby/gems/1.9/gems/activerecord-jdbc-adapter-1.1.1/lib/arjdbc/jdbc/connection\_methods.rb:6:in \`jdbc\_connection'  
/home/chenjing/jruby-1.6.0/lib/ruby/gems/1.9/gems/activerecord-jdbc-adapter-1.1.1/lib/arjdbc/mysql/connection\_methods.rb:18:in \`mysql\_connection'  
org/jruby/RubyBasicObject.java:1671:in \`\_\_send\_\_'  
org/jruby/RubyKernel.java:2063:in \`send'  
/home/chenjing/jruby-1.6.0/lib/ruby/gems/1.9/gems/activerecord-3.0.6/lib/active\_record/connection\_adapters/abstract/connection\_pool.rb:229:in \`new\_connection'

后来改好了：

chenjing@chenjing-laptop:~/Myrails/depot$ jruby -S rake db:setup RAILS\_ENV="production" --trace  
(in /home/chenjing/Myrails/depot)

\** Invoke db:setup (first\_time)  
\** Invoke db:create (first\_time)  
\** Invoke db:load\_config (first\_time)  
\** Invoke rails\_env (first\_time)  
\** Execute rails\_env  
\** Execute db:load\_config  
\** Execute db:create  
"cj mysql"  
"{\\"pool\\"=&gt;5, \\"timeout\\"=&gt;5000, \\"username\\"=&gt;\\"username\\", \\"password\\"=&gt;\\"password\\", \\"host\\"=&gt;\\"127.0.0.1\\", \\"adapter\\"=&gt;\\"mysql\\", \\"encoding\\"=&gt;\\"utf8\\", \\"reconnect\\"=&gt;false, \\"socket\\"=&gt;\\"/var/run/mysqld/mysqld.sock\\", \\"database\\"=&gt;\\"depot\_production\\"}"

depot\_production already exists  
\** Invoke db:schema:load (first\_time)  
\** Invoke environment (first\_time)  
\** Execute environment  
\** Execute db:schema:load  
\-- create\_table("carts", {:force=&gt;true})  
   -&gt; 0.1520s  
   -&gt; 0 rows  
\-- create\_table("line\_items", {:force=&gt;true})  
   -&gt; 0.0670s  
   -&gt; 0 rows  
\-- create\_table("orders", {:force=&gt;true})  
   -&gt; 0.0340s  
   -&gt; 0 rows  
\-- create\_table("products", {:force=&gt;true})  
   -&gt; 0.0200s  
   -&gt; 0 rows  
\-- create\_table("users", {:force=&gt;true})  
   -&gt; 0.0790s  
   -&gt; 0 rows  
\-- initialize\_schema\_migrations\_table()  
   -&gt; 0.0260s  
\-- assume\_migrated\_upto\_version(20110401055529, "db/migrate")  
   -&gt; 0.0120s  
\** Invoke db:seed (first\_time)  
\** Invoke db:abort\_if\_pending\_migrations (first\_time)  
\** Invoke environment  
\** Execute db:abort\_if\_pending\_migrations  
\** Execute db:seed  
\** Execute db:setup