Last active
December 18, 2015 07:09
-
-
Save badlee/5744086 to your computer and use it in GitHub Desktop.
Migrate a Mysql database to Mongodb.
Migrez votre base de données Mysql vers MongoDB.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* | |
* Uploaded by Rajah Oshimin | |
* Udated from : http://cloudsherpaz.wordpress.com/2013/01/05/migrate-mysql-database-to-mongodb/ | |
* | |
*/ | |
// genghis is a single-file MongoDB admin application. | |
// Located at http://genghisapp.com/ | |
$gengis = "http://127.0.0.1:8000/servers/localhost/databases/"; | |
// configurations settings | |
if(!isset($_REQUEST["base"])) | |
die("Error can't read database name"); | |
$_REQUEST["user"] = isset($_REQUEST["user"]) ?$_REQUEST["user"] : "root"; | |
$_REQUEST["pass"] = isset($_REQUEST["pass"]) ?$_REQUEST["pass"] : "azerty"; | |
$_REQUEST["server"] = isset($_REQUEST["server"]) ?$_REQUEST["server"] : "localhost"; | |
$_REQUEST["mongodb"] = isset($_REQUEST["mongodb"]) ?$_REQUEST["mongodb"] : "localhost"; | |
// mysql settings | |
$mydb = $_REQUEST["base"]; | |
$myconn = mysql_connect($_REQUEST["server"],$_REQUEST["user"],$_REQUEST["pass"]); | |
$setmydb = mysql_select_db( $mydb ); | |
$mytables = getMyTables( $mydb ); | |
//mongo db settings | |
$modb = $mydb; // same name as mysql's table | |
$moConnect="mongodb://{$_REQUEST['mongodb']}"; | |
try { | |
$moconn = new Mongo($moConnect); | |
$modb = $moconn->selectDB( $modb ); | |
} catch(MongoConnectionException $e) { | |
die($e."Problem during mongodb initialization. Please start mongodb server."); | |
} | |
foreach( $mytables as $table => $struct ) { | |
$sql = mysql_query("SELECT * FROM `$table` LIMIT 0 , 500000") or die( mysql_error() ); | |
$count = mysql_num_rows( $sql ); | |
// Starts new collection on mongodb | |
$collection = $modb->createCollection($table); | |
// If it has content insert all content | |
if( $count > 0 ) | |
while( $info = mysql_fetch_array( $sql, MYSQL_NUM )) { | |
$infosize = count( $info ); | |
$mosql = array(); | |
for( $i=0; $i < $infosize; $i++ ) { | |
if(!empty($struct[$i])) | |
$mosql[$struct[$i]] = checkEncode($info[$i]); | |
} | |
$collection->insert($mosql); | |
} | |
} | |
$gengis = $gengis ? $gengis : "#"; | |
exit("Done! Please, check your MongoDB <a href='$gengis$modb' ".($gengis?"target='_blank'":"").">collection!</a>"); | |
// function definition | |
function getMyTables( $dbname ) { | |
$tables = array(); | |
$sql = mysql_query("SHOW TABLES FROM `$dbname`; ") or die("Error getting tables from $dbname"); | |
if( mysql_num_rows( $sql ) > 0 ) | |
while( $table = mysql_fetch_array( $sql ) ) { | |
$explain = explainMyTable( $table[0] ); | |
$tables[$table[0]] = $explain; | |
} | |
return $tables; | |
} | |
function explainMyTable( $tbname ) { | |
$explain = array(); | |
$sql = mysql_query("EXPLAIN `$tbname`") or die("Error getting table structure <br/>".mysql_error()); | |
$i = 0; | |
while( $get = mysql_fetch_array( $sql ) ) { | |
array_push( $explain, $get[0] ); | |
$i++; | |
} | |
return $explain; | |
} | |
function checkEncode($string) { | |
if( !mb_check_encoding($string,'UTF-8')) | |
$string = mb_convert_encoding($string,'UTF-8','ISO-8859-1'); | |
return $string; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment