{"id":16624,"date":"2015-01-29T09:29:13","date_gmt":"2015-01-29T03:59:13","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=16624"},"modified":"2022-02-18T17:05:23","modified_gmt":"2022-02-18T11:35:23","slug":"mysql-to-mongodb-migration","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/mysql-to-mongodb-migration\/","title":{"rendered":"MySQL to MongoDB Migration"},"content":{"rendered":"<p>MySQL and MongoDB are a very popular open-source databases these days. Through this blog, I wanted to share a use case of migrating MySQL database to MongoDB database and perhaps want to share few lessons during the migration process which would be helpful.<\/p>\n<p>There are\u00a0three steps to\u00a0the migration process:<\/p>\n<p>Suppose you have a user table with the\u00a0following structure, which needs to be migrated to MongoDB collection named\u00a0&#8220;<strong>user<\/strong>&#8221;<\/p>\n<p>[code]<\/p>\n<p>+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| Field | Type | Null | Key | Default | Extra |<br \/>\n+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| id | bigint(20) | NO | PRI | NULL | auto_increment |<br \/>\n| version | bigint(20) | NO | | NULL | |<br \/>\n| address | varchar(255) | NO | | NULL | |<br \/>\n| age | int(11) | NO | | NULL | |<br \/>\n| first_name | varchar(255) | NO | | NULL | |<br \/>\n| last_name | varchar(255) | NO | | NULL | |<br \/>\n+&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<\/p>\n<p>[\/code]<\/p>\n<p><strong>Step 1 :<\/strong> Generate csv of the data for existing table :<\/p>\n<p>[code]<\/p>\n<p>select first_name, last_name, address, age, version into outfile &#8216;user.csv&#8217; FIELDS TERMINATED BY &#8216;,&#8217; LINES TERMINATED BY &#8216;\\n&#8217; from user;<\/p>\n<p>[\/code]<\/p>\n<p>This query will generate a file named user.csv into directory where MySQL\u00a0is installed followed by your database name. Suppose your database name is Test,\u00a0then\u00a0file will be generated at \/var\/lib\/mysql\/test directory.<\/p>\n<p><strong>Step 2 :<\/strong> Now move the file to appropriate directory, or else\u00a0you will get file permissions error. Incase, you have all permissions, you may skip this step.<\/p>\n<p>[code] sudo mv \/var\/lib\/mysql\/test\/user.csv Desktop\/ [\/code]<\/p>\n<p><strong>Step 3 :<\/strong> Now import generated csv into\u00a0MongoDB<\/p>\n<p>[code]<\/p>\n<p>mongoimport &#8211;host 127.0.0.1 &#8211;port 27017&#8211;username user\u00a0&#8211;password pass\u00a0&#8211;db test\u00a0&#8211;collection user &#8211;type csv -f first_name,last_name,address,age,version &#8211;file Desktop\/user.csv<\/p>\n<p>[\/code]<\/p>\n<p>This will import data into &#8220;<strong>user<\/strong>&#8221; collection \u00a0under <strong>test\u00a0<\/strong>database.<\/p>\n<p>I hope you found this blog useful with the steps for migrating from MySQL database to MongoDB.<\/p>\n<p>Keep performing!!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL and MongoDB are a very popular open-source databases these days. Through this blog, I wanted to share a use case of migrating MySQL database to MongoDB database and perhaps want to share few lessons during the migration process which would be helpful. There are\u00a0three steps to\u00a0the migration process: Suppose you have a user table [&hellip;]<\/p>\n","protected":false},"author":145,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":16},"categories":[1],"tags":[4843,1596,1601,1597,1599,76,1600,1598],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16624"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/145"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=16624"}],"version-history":[{"count":1,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16624\/revisions"}],"predecessor-version":[{"id":53580,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16624\/revisions\/53580"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=16624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=16624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=16624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}