I cant seem to find any info on how to use datamapper talk to a mysql master/slave setup. I'm running rails3 with dm-mysql-adapter
-
2I need this too. If I can't find anything, I'll write one and contribute back. Since DM supports multiple repositories, and CRUD ops are already split internally, I wouldn't imagine it should be that hard... it may even just work with a little configuration once I start digging around in the DM internals a bit more. – d11wtq May 06 '11 at 00:07
4 Answers
This may be what you are looking for...

- 321
- 2
- 6
-
This is exactly what I'm looking for! Thanks to d11wtq for writing it after commenting above - unfortunately I haven't been able to get it to work in my application. It doesn't cause any errors, the app just reads from the master all the time instead of using the slave for some reason - so that hasn't helped much. Still trying to figure out how to do this properly. – mltsy Dec 13 '12 at 19:23
You can use DataMapper's Multiple Data Store feature for this:
DataMapper.setup(:default, 'mysql://master-host/mydb')
DataMapper.setup(:slave, 'mysql://slave-host/mydb')
Now, whenever you'd like to read from the slave, use:
DataMapper.repository(:slave) do
Person.first
end
Unfortunately, it doesn't look like you can do transparent read-from-slave write-to-master:
[...] This will use your connection to the :external data-store and the first Person it finds. Later, when you call .save on that person, it'll get saved back to the :external data-store; An object is aware of what context it came from and should be saved back to.
So, if you try to save a person you retrieved from the slave, it will try to store it back to the slave database when modified.

- 22,785
- 8
- 39
- 55
you don't have to do this in your application, but may Mysql handle that itself. That way, you can take all the benefits of load-balancing and high-availability with automatic master fail-over with you. I'd highly recommend this setup over a manual, application based load-balancing, as you should leave the persistence-layer out of the business logic provided by your application. This brings the already written advantages and makes your application more scalable and maintainable due to a better separation of concerns.
Have a look at the Mysql NDB documentation for the concepts and this tutorial for a quickstart in Mysql Clustering.

- 5,757
- 4
- 25
- 55
-
The issue I have with this is that it NDB seems to add a additional layer of complexity, I'd just like to do certain reads from a slave, while keeping the master free for writes. – Tom Aug 28 '11 at 16:46
-
@Tom: But you'll have to transfer the data from the master to the slaves somehow anyway. Before utilizing some custom script for that, why not use the intended way of MySQL and get the advantages above, too? – Lars Aug 29 '11 at 06:41
1) You have to instal the adapter.
2) Add requires require 'rubygems' require 'data_mapper' 3) Config my sql
DataMapper.setup(:default, 'mysql://localhost/the_database_name')
or
DataMapper.mysql(:host xxxx, :user xxxx, :password xxxx, :database xxxx)
4) Create you model objects for example:
class Post
include DataMapper::Resource
property :id, Serial # An auto-increment integer key
property :title, String # A varchar type string, for short strings
property :body, Text # A text block, for longer string data.
property :created_at, DateTime # A DateTime, for any date you might like.
end
If you have any doubt you could check this link: http://datamapper.org/getting-started

- 5,914
- 2
- 22
- 21
-
I already have dm talking to mysql just like you're describing, my question is how to direct all write operations to the mysql master and all the reads to a pool of readers – Anders Apr 15 '11 at 23:52
-
-
take a look a this post: http://stackoverflow.com/questions/4062113/rails-3-and-mysql-master-slave-replications/4383538#4383538 – JAiro Apr 15 '11 at 23:59
-
saw that post...from what i can tell, its only for active record...im using datamapper – Anders Apr 16 '11 at 00:05