登录
首页 >  数据库 >  MySQL

浅析怎么将mysql数据向mongo迁移(思路分享)

来源:SegmentFault

时间:2023-02-24 20:29:49 409浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《浅析怎么将mysql数据向mongo迁移(思路分享)》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下MySQL、mongodb、PHP,希望所有认真读完的童鞋们,都有实质性的提高。

由于之前涉及mysql分表,把用户所关联的各种数据,我们暂设定为abcd,代表四种业务

比如userid 为1 ,可能属于 a1表,a2表,b3表,d4表。
现在要统一改为mongo
实现在user表中有一个abcd字段,类型是数组,分别存储相当应的表明

比如 abcd [0=>a1,1=>b2,2=>c3]这样。

思路也很简单,分表找到数据库属于abcd的所有表,联合查询,取到表名和pid(person id),然后插入更新到mongo abcd字段即可。

以下是简单实现:

<?php &nbsp; &nbsp; &nbsp; &nbsp;$conn = mysql_connect("14.11.1.1", "d", "d2015");
 &nbsp; &nbsp; &nbsp; &nbsp;mysql_select_db("dmp", $conn);//选择MYSQL数据库
 &nbsp; &nbsp; &nbsp; &nbsp;mysql_query("set names utf8");//
 &nbsp; &nbsp; &nbsp; &nbsp;$result = mysql_query("SHOW TABLES");
 &nbsp; &nbsp; &nbsp; &nbsp;while ($row = mysql_fetch_array($result)) {
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (strstr($row[0], &#39;d_user_aaa_&#39;)) {
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//$sql[&#39;aaa&#39;][] = $row[0];
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$aaa_sql .= "select &#39;$row[0]&#39; tbname,$row[0].pid from $row[0] union all ";
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} elseif (strstr($row[0], &#39;d_user_ccc_&#39;)) {
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$ccc_sql .= "select &#39;$row[0]&#39; tbname,$row[0].pid from $row[0] union all ";
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else if (strstr($row[0], &#39;d_user_ddd_&#39;)) {
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$ddd_sql .= "select &#39;$row[0]&#39; tbname,$row[0].pid from $row[0] union all ";
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else if (strstr($row[0], &#39;d_user_ttt_&#39;)) {
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$ttt_sql .= "select &#39;$row[0]&#39; tbname,$row[0].pid from $row[0] union all ";
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
 &nbsp; &nbsp; &nbsp; &nbsp;}
 &nbsp; &nbsp; &nbsp; &nbsp;$aaa_sql = rtrim($aaa_sql, "union all");
 &nbsp; &nbsp; &nbsp; &nbsp;$ccc_sql = rtrim($ccc_sql, "union all");
 &nbsp; &nbsp; &nbsp; &nbsp;$ddd_sql = rtrim($ddd_sql, "union all");
 &nbsp; &nbsp; &nbsp; &nbsp;$ttt_sql = rtrim($ttt_sql, "union all");
 &nbsp; &nbsp; &nbsp; &nbsp;// 可以统计下数据量大小
 &nbsp; &nbsp; &nbsp; &nbsp;//echo "select count(*) from "."($ccc_sql)"." as nums";
 &nbsp; &nbsp; &nbsp; &nbsp;// 测试 先来100条,一般这种情况在cli执行或者放在cron中执行,数据量大不适合在浏览器中跑,会超时。
 &nbsp; &nbsp; &nbsp; &nbsp;//$aaa_sql .= " limit 100";
 &nbsp; &nbsp; &nbsp; &nbsp;$ccc_sql .= " limit 100";
 &nbsp; &nbsp; &nbsp; &nbsp;$res = mysql_query($ccc_sql);
 &nbsp; &nbsp; &nbsp; &nbsp;$conn=new Mongo("mongodb://192.168.1.40:10001/test");
 &nbsp; &nbsp; &nbsp; &nbsp;$collection=$conn->test->d_user;
        while ($row = mysql_fetch_assoc($res)) {
            $collection->update(array("pid"=>(int)$row['pid']),array('$addToSet'=>array('acdt'=>$row['tbname'])));
        }

ps:myslq Union要进行重复值扫描,所以效率比较低。如果合并没有刻意要删除重复行,那么就使用Union All两个要联合的SQL语句 字段个数必须一样,而且字段类型要(一致)因为这些表里就是存了一个pid和时间,mongo的addtoset去重我猜又比mysql快,所以用了union all;

终于介绍完啦!小伙伴们,这篇关于《浅析怎么将mysql数据向mongo迁移(思路分享)》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布数据库相关知识,快来关注吧!

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>