문제

I'am developing some Ruby scripts which work with MySQL databases. All the databases are on one server. Unfortunately, we have a lot of databases (one for every costumer) and some scripts need to connect to each of those databases.

Right now, the scripts connect to some database using

db = Sequel.connect(:adapter => 'mysql', :user => user, :host => server, :database => database, :password => passwd, :encoding => 'utf8')

which works, but needs to disconnect and reconnect for each of those databases.

Is there a way of how to connect once to database server and then just change the database I'am working with?

도움이 되었습니까?

해결책

You can probably do something like this if you really want to avoid disconnecting and reconnecting:

db = Sequel.connect(:adapter => 'mysql', :user => user, :host => server, :database => database, :password => passwd, :encoding => 'utf8', :single_threaded=>true)
databases = %w'db1 db2 db3'
databases.each do |db_name|
  db << "USE #{db_name}"
  ...
end

I'm not sure I'd recommend it unless the disconnecting and reconnecting was very slow (high latency between database and application?).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top