# Multiple database connection

**URL:** <https://www.ruby-forum.com/t/multiple-database-connection/142094>\
**Category:** Rails\
**Created:** [July 3, 2008, 11:44am UTC](https://www.ruby-forum.com/t/multiple-database-connection/142094 "2008-07-03T11:44:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaiganez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jaiganez](https://www.ruby-forum.com/u/jaiganez)\
**Post date:** [July 3, 2008, 11:44am UTC](https://www.ruby-forum.com/t/multiple-database-connection/142094/1 "2008-07-03T11:44:17Z")

</div>

hai…friends…  
how to create multiple mysql database ??pls help  
i ve used the following type but it creates only one database  
mobile\_development…any help pls…

development:  
adapter: mysql  
database: mobile\_development  
timeout: 5000  
username: root  
password:  
host: localhost  
environment\_name1:  
adapter: mysql  
database: database1  
username: root  
password:  
host: localhost

environment\_name2:  
adapter: mysql  
database: database2  
username: root  
password:  
host: localhost  
test:  
adapter: mysql  
database: mobile\_test  
timeout: 5000  
username: root  
password:  
host: localhost

production:  
adapter: mysql  
database: mobile\_production  
timeout: 5000  
username: root  
password:  
host: localhost

---

<div class="post-metadata">

**Author:** ![jaiganez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jaiganez](https://www.ruby-forum.com/u/jaiganez)\
**Post date:** [July 3, 2008, 12:00pm UTC](https://www.ruby-forum.com/t/multiple-database-connection/142094/2 "2008-07-03T12:00:10Z")

</div>

On 3 Jul 2008, at 10:44, Jai Jai wrote:

> hai…friends…  
> how to create multiple mysql database ??pls help  
> i ve used the following type but it creates only one database  
> mobile\_development…any help pls…

I don’t know what you’ve done beyond edit database.yml, but at the  
very least you’re going to need to indent it properly: the  
environment\_name1 and environment\_name2 entries you’ve added aren’t  
indented like the other ones.

Fred

---

<div class="post-metadata">

**Author:** ![jaiganez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jaiganez](https://www.ruby-forum.com/u/jaiganez)\
**Post date:** [July 3, 2008, 12:09pm UTC](https://www.ruby-forum.com/t/multiple-database-connection/142094/3 "2008-07-03T12:09:22Z")

</div>

thank u for ur kind reply…  
development:  
adapter: mysql  
database: library\_development  
timeout: 5000  
username: root  
password:  
host: localhost

test:  
adapter: mysql  
database: library\_test  
timeout: 5000  
username: root  
password:  
host: localhost  
production:  
adapter: mysql  
database: library\_production  
timeout: 5000  
username: root  
password:  
its the normal way to connect a data base…how to connect two database  
or more like this …any help pls…am a new one to ruby rails…

---

<div class="post-metadata">

**Author:** ![jaiganez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jaiganez](https://www.ruby-forum.com/u/jaiganez)\
**Post date:** [July 3, 2008, 4:32pm UTC](https://www.ruby-forum.com/t/multiple-database-connection/142094/4 "2008-07-03T16:32:39Z")

</div>

You might also want to take a look at

[http://magicmodels.rubyforge.org/magic\_multi\_connections/](http://magicmodels.rubyforge.org/magic_multi_connections/)

If you’re a newbie, it might be a bit over your head. That will be up to  
you to decide. I’m using MMC in a rake task right now that will be  
migrating data from one environment to another.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![jaiganez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jaiganez](https://www.ruby-forum.com/u/jaiganez)\
**Post date:** [July 3, 2008, 8:18pm UTC](https://www.ruby-forum.com/t/multiple-database-connection/142094/5 "2008-07-03T20:18:26Z")

</div>

> Theoreticaly you can have every table in its own database, server.
> 
> class Mytable \< ActiveRecord::Base  
> establish\_connection(:environment\_name1)  
> end

In my case I have multiple tables in each db so I do something like  
this:

class FinancialConnection \< ActiveRecord::Base  
self.abstract\_class = true  
establish\_connection("#{RAILS\_ENV}\_financials".to\_sym)  
end

By doing that I can have a development\_financials, test\_financials,  
and production\_financials DB and vary the connection for the  
environment. Further, by making this connection class an abstract  
ARec class I can use it as the superclass for all the financially  
related tables:

class Account \< FinancialConnection  
end

class Journal \< FinancialConnection  
end

etc

(Note: this relies on the fact that subclasses share the db connection  
of their superclass if they do not establish one of their own).

---

<div class="post-metadata">

**Author:** ![jaiganez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jaiganez](https://www.ruby-forum.com/u/jaiganez)\
**Post date:** [July 3, 2008, 1:44pm UTC](https://www.ruby-forum.com/t/multiple-database-connection/142094/6 "2008-07-03T13:44:53Z")

</div>

Jai Jai wrote:

> hai…friends…  
> how to create multiple mysql database ??pls help  
> i ve used the following type but it creates only one database  
> mobile\_development…any help pls…
> 
> development:  
> adapter: mysql  
> database: mobile\_development  
> timeout: 5000  
> username: root  
> password:  
> host: localhost  
> environment\_name1:  
> adapter: mysql  
> database: database1  
> username: root  
> password:  
> host: localhost

Theoreticaly you can have every table in its own database, server.

class Mytable \< ActiveRecord::Base  
establish\_connection(:environment\_name1)  
end

or

class Mytable2 \< ActiveRecord::Base  
establish\_connection(  
:adapter =\> “mysql”,  
:host =\> “localhost”,  
:username =\> “root”,  
:password =\> “”,  
:database =\> “database2”  
)  
set\_table\_name “anyname”  
end

set\_table\_name comes wery handy when pluralization is a problem.

by  
TheR
