简体   繁体   中英

Rails 4 connect to remote MySQL database and pull Database structure for easy access

In my team, we are developing a system that is accessed by different platforms such as a Ruby on Rails website, a desktop java application and Android as well as iOS apps.
Our central MySQL database is running remotely on a server and can be accessed through PHPMyAdmin (and ControlPanel).

While the connection between the other platforms works well, I struggle with connecting my Rails app to the database. I would like to copy the database structure (tables, indexes, columns) so that I can access the database within Rails just as I would access data from a model that has been created locally (eg Customer.find(name: "Florian") ).

Is there a way to accomplish this? I tried several things such as altering my database.yml file, but when I then run something, like for example rails c it shows:

/Users/florianpfisterer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/mysql2-0.4.2/lib/mysql2.rb:31:in `require': dlopen(/Users/florianpfisterer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-15/2.2.0-static/mysql2-0.4.2/mysql2/mysql2.bundle, 9): Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.18.dylib
  Referenced from: /Users/florianpfisterer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-15/2.2.0-static/mysql2-0.4.2/mysql2/mysql2.bundle
  Reason: image not found - /Users/florianpfisterer/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-15/2.2.0-static/mysql2-0.4.2/mysql2/mysql2.bundle (LoadError)
  ...

My `database.yml' file:

development:
  adapter: mysql2
  encoding: utf8
  host: <host-IP>
  username: <username>
  password: <password>
  port: <port>
  database: <database>
  pool: 5
  timeout: 5000

The same block is under test and production as well. In my Gemfile I have included:

gem 'mysql2'

I'm running Mac OS X 10.11.2 El Capitan and the server is a linux system. My Rails version is Rails 4.2.4 and Ruby ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin15] .

Thank you!

EDIT: Thank you all, finally fixed my database.yml file and running rake db:schema:dump did it. But how do I convert my schema.rb to locally usable ActiveRecord::Model s ?

Use this in your Gemfile

gem 'mysql2', '~> 0.3.18'

config/database.yml

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  socket: /var/run/mysqld/mysqld.sock
  username:  <%= ENV['DATABASE_USER'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>

insert into your .bashrc

export DATABASE_USER='root'

export DATABASE_PASSWORD='123'

Add library path to your .bash_profile file:

MYSQL=/usr/local/mysql/bin
export PATH=$PATH:$MYSQL
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH

The problem not in connection but with loading mysql itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM