日期:2014-05-16  浏览次数:20446 次

rails 3 connect to mysql db

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


首先
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 => 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\"=>5, \"timeout\"=>5000, \"username\"=>\"username\", \"password\"=>\"password\", \"host\"=>\"127.0.0.1\", \"adapter\"=>\"mysql\", \"encoding\"=>\"utf8\", \"reconnect\"=>false, \"socket\"=>\"/var/run/mysqld/mysqld.sock\", \"database\"=>\"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=>true})
?? -> 0.1520s
?? -> 0 rows
-- create_table("line_items", {:force=>true})
?? -> 0.0670s
?? -> 0 rows
-- create_tabl