I cant seem to find an开发者_StackOverflow社区y info on how to use datamapper talk to a mysql master/slave setup. I'm running rails3 with dm-mysql-adapter
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.
This may be what you are looking for...
https://github.com/d11wtq/dm-master-slave-adapter
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.
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
精彩评论