Question

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?

Was it helpful?

Solution

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?).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top