converting database from mysql to mongoDb

后端 未结 13 1270
陌清茗
陌清茗 2020-12-07 10:43

is there any easy way to change the database from mysql to mongoDB ?

or better any one suggest me good tutorial do it

相关标签:
13条回答
  • 2020-12-07 10:52

    If you are looking for a tool to do it for you, good luck.

    My suggestion is to just pick your language of choice, and read from one and write to another.

    0 讨论(0)
  • 2020-12-07 10:52

    I think one of the easiest ways is to export the MySQL database to JSON and then use mongorestore to import it to a MongoDB database.

    Step 1: Export the MySQL database to JSON

    Load the mysql dump file into a MySQL database if necessary

    Open MySQL Workbench and connect to the MySQL database

    Go to the Schema viewer > Select database > Tables > right-click on the name of the table to export

    Select 'Table Data Export Wizard'

    Set the file format to .json and type in a filename such as tablename.json

    Note: All tables will need to be exported individually

    Step 2: Import the JSON files to a MongoDB using the mongorestore command

    The mongorestore command should be run from the server command line (not the mongo shell)

    Note that you may need to provide the authentication details as well as the --jsonArray option, see the mongorestore docs for more information

    mongoimport -d dbname -u ${MONGO_USERNAME} -p ${MONGO_PASSWORD} --authenticationDatabase admin -c collectionname --jsonArray --file tablename.json
    

    Note: This method will not work if the original MySQL database has BLOBs/binary data.

    0 讨论(0)
  • 2020-12-07 10:54

    I am kind of partial to TalendOpenStudio for those kind of migration jobs. It is an eclipse based solution to create data migration "scripts" in a visual way. I do not like visual programming, but this is a problem domain I make an exception.

    Adrien Mogenet has create a MongoDBConnection plugin for mongodb.

    It is probably overkill for a "simple" migration but ut is a cool tool.

    Mind however, that the suggestion of Nix will probably save you time if it is a one-of migration.

    0 讨论(0)
  • 2020-12-07 11:02

    is there any easy way to change the database from mysql to mongoDB ?

    Method #1: export from MySQL in a CSV format and then use the mongoimport tool. However, this does not always work well in terms of handling dates of binary data.

    Method #2: script the transfer in your language of choice. Basically you write a program that reads everything from MySQL one element at a time and then inserts it into MongoDB.

    Method #2 is better than #1, but it is still not adequate.

    MongoDB uses collections instead of tables. MongoDB does not support joins. In every database I've seen, this means that your data structure in MongoDB is different from the structure in MySQL.

    Because of this, there is no "universal tool" for porting SQL to MongoDB. Your data will need to be transformed before it reaches MongoDB.

    0 讨论(0)
  • 2020-12-07 11:02

    If anyone's still looking for a solution, i found that the easiest way is to write a PHP script to connect to your SQL DB, retrieve the information you want using the usual Select statement, transform the information into JSON using the PHP JSON Encode functions and simply output your results to file or directly to MongoDB. It's actually pretty simple and straight forward, the only thing to do is to double check your output against a Json validator, you may have to use functions such as explode to replace certain characters and symbols to make it valid. I have done this before however i currently do not have the script at hand but from what i can remember it was literally half a page of code.

    Oh also remember Mongo is a document store so some data mapping is required to get it to be acceptable with mongo.

    0 讨论(0)
  • 2020-12-07 11:07

    For those coming to this with the same problem, you can check out this Github project. This is an ongoing development that will help you migrate data from MySQL database to MongoDB by simply running a simple command.

    It will generate MongoDB Schemas in TypeScript so you can use them later in your project. Each MySQL table will be a MongoDB collection, and datatypes will be efficiently converted to their MongoDB compatibles.

    The documentation for the same can be found in the project's README.md. Feel free to come in and contribute. Would like to help if need be.

    0 讨论(0)
提交回复
热议问题