converting database from mysql to mongoDb

后端 未结 13 1271
陌清茗
陌清茗 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 11:07

    Try this: Automated conversion of MySQL dump to Mongo updates using simple r2n mappings. https://github.com/virtimus/mysql2mongo

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

    If you're using Ruby, you can also try: Mongify

    It's a super simple way to transform your data from a RDBS to MongoDB without losing anything.

    Mongify will read your mysql database, build a translation file for you and all you have to do is map how you want your data transformed.

    It supports:

    • Auto updating IDs (to BSON ObjectID)
    • Updating referencing IDs
    • Type Casting values
    • Embedding tables into other documents
    • Before save filters (to allow changes to the data manually)
    • and much much more...

    Read more about it at: http://mongify.com/getting_started.html

    There is also a short 5 min video on the homepage that shows you how easy it is.

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

    You can use QCubed (http://qcu.be) framework for that. The procedure would be something like this:

    1. Install QCubed (http://www.thetrozone.com/qcubed-installation)
    2. Do the codegen on your database. (http://www.thetrozone.com/php-code-generation-qcubed-eliminating-sql-hassle)
    3. Take your database offline from the rest of the world so that only one operation runs at a time.
    4. Now write a script which will read all rows from all tables of the database and use the getJson on all objects to get the json. You can then use the data to convert to array and push it into the mongoDB!
    0 讨论(0)
  • 2020-12-07 11:13

    You can use the following project.It requires solr like configuration file to be written.Its very simple and straight forward.

    http://code.google.com/p/sql-to-mongo-importer/

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

    Here's what I did it with Node.js for this purpose:

    var mysql = require('mysql');
    var MongoClient = require('mongodb').MongoClient;
    
    function getMysqlTables(mysqlConnection, callback) {
        mysqlConnection.query("show full tables where Table_Type = 'BASE TABLE';", function(error, results, fields) {
            if (error) {
                callback(error);
            } else {
                var tables = [];
                results.forEach(function (row) {
                    for (var key in row) {
                        if (row.hasOwnProperty(key)) {
                            if(key.startsWith('Tables_in')) {
                                tables.push(row[key]);
                            }
                        }
                    }
                });
                callback(null, tables);
            }
        });
    
    }
    
    function tableToCollection(mysqlConnection, tableName, mongoCollection, callback) {
        var sql = 'SELECT * FROM ' + tableName + ';';
        mysqlConnection.query(sql, function (error, results, fields) {
            if (error) {
                callback(error);
            } else {
                if (results.length > 0) {
                    mongoCollection.insertMany(results, {}, function (error) {
                        if (error) {
                            callback(error);
                        } else {
                            callback(null);
                        }
                    });
                } else {
                    callback(null);
                }
            }
        });
    }
    
    MongoClient.connect("mongodb://localhost:27017/importedDb", function (error, db) {
        if (error) throw error;
    
        var MysqlCon = mysql.createConnection({
            host: 'localhost',
            user: 'root',
            password: 'root',
            port: 8889,
            database: 'dbToExport'
        });
    
        MysqlCon.connect();
    
        var jobs = 0;
    
        getMysqlTables(MysqlCon, function(error, tables) {
            tables.forEach(function(table) {
                var collection = db.collection(table);
                ++jobs;
                tableToCollection(MysqlCon, table, collection, function(error) {
                    if (error) throw error;
                    --jobs;
                });
            })
        });
    
        // Waiting for all jobs to complete before closing databases connections.
        var interval = setInterval(function() {
            if(jobs<=0) {
                clearInterval(interval);
                console.log('done!');
                db.close();
                MysqlCon.end();
            }
        }, 300);
    });
    
    0 讨论(0)
  • 2020-12-07 11:14

    If I could quote Matt Briggs (it solved my roblem one time):

    The driver way is by FAR the most straight forward. The import/export tools are fantastic, but only if you are using them as a pair. You are in for a wild ride if your table includes dates and you try to export from the db and import into mongo.

    You are lucky too, being in c#. We are using ruby, and have a 32million row table we migrated to mongo. Our ending solution was to craft an insane sql statement in postgres that output json (including some pretty kludgy things to get dates going properly) and piped the output of that query on the command line into mongoimport. It took an incredibly frustrating day to write, and is not the sort of thing that can ever really be changed.

    So if you can get away with it, use ado.net with the mongo driver. If not, I wish you well :-)

    (note that this is coming from a total mongo fanboi)

    MySQL is very similar to other SQL databases, so I send You to the topić: Convert SQL table to mongoDB document

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