Moving rails from MySQL to MongoDB

I have one project in development. I am using mysql and ActiveRecord. It looks like this project is good to start using MongoDB (which I never used) with the Mongoid adapter.

What is the best way to move my project to Mongo? As far as I understand, should I edit my models (not a big deal) and that's it?

How can I export my data from a Mysql database to Mongo? I just tried yaml_db but that doesn't help me.

And the second question is how should I save my Mongo database if my system crashes (since I understand that Mongo will lose data because it stores data in RAM). My db is 10% / 90% write / read.

+4
source share
4 answers

I did something similar recently and approached it as a three-step process. There may be more Rails ways to do this, but this method worked for me and was painless.

I used an iterative approach and took advantage of the fact that MongoDB is actually a very relational friendly document storage system. I started with a relational installation using the Mongoid Relational Associations (at the bottom of the page) syntax reference_many and referenced_in. Once everything worked, I reorganized iteratively to a more document-oriented approach.

1. Reset the existing database directly to MongoDB

I used my existing models to brute-force the SQL table and SQL data into parallel MongoDB documents. I just slammed everything that I had, without worrying about names or relationships; strict display of tables in the collection 1: 1.

It was a one-time process for me. Once everything is here, steps 2 and 3 take this data and transform it into the structure I wanted. I could also change my models since I no longer need to interact with the relational system. You might want to create some kind of copy of existing models if you need to return to this step for any reason.

2. Data transfer

In the next step, I went with the ruby ​​custom shell application. Again, this might be the best way to do this using Rails functions, but I went using the script's direct transaction method.

I used raw MongoDB Ruby driver , read the database created in step 1, and converted the source collections to the forms I wanted. I used a separate database for the recipient from the one that was created in step 1, because I iteratively modified this script to become more and more document-oriented, as I reorganized my model in step 3.

3. Referential models as needed

I reorganized my model for working with the database created in step 2, skipped my tests, and then returned to the script in step 2 and made changes to make the database more document-oriented.

I repeated steps 2 and 3 until I reworked the models and layouts of the documents until I reached the final result.

+7
source

Re: your second question

1.8.0 now supports single server durability by adding a log (see docs here ). This suggests that when you use logging, mongo will issue batch commit every 100 ms (will be more common in future versions). As soon as he appears in the magazine, if it works, he can return and restore. You can request that the call in Mongo does not return until it is safe (with an expensive minor hit on performance), or if you use replicas, you can specify how many nodes should be replicated before returning, so you can do sure it was replicated to most / all nodes.

+3
source

Others answered your second question: the transition from MySQL to MongoDB depends on how your data looks. There is no automatic tool that will convert the database. You most likely want to just write your own conversion script.

I would suggest just dumping one model in YAML, as you tried, convert this model to use Mongoid , then read YAML and re-create the objects.

If you have complex relational data, MongoDB may not be what we're looking for. Check out the differences between key stores and relational databases.

There is a popular question that only covers this: When to use MongoDB or other database workflow systems?

+1
source

Diff is that you have an object inline object. Use it in your models. You can also use Event Sourcing to protect against failure.

0
source

Source: https://habr.com/ru/post/1344075/


All Articles