简体   繁体   English

无论如何用PHP做MongoDB分片?

[英]Anyway to do MongoDB Sharding in PHP?

I have started using MongoDB for one of my PHP project. 我已经开始将MongoDB用于我的PHP项目之一。 In that database, i am trying to use MongoDB sharding concept. 在该数据库中,我尝试使用MongoDB分片概念。 I got the below link and tried, 我获得了以下链接并尝试过,

MongoDB Sharding Example MongoDB分片示例

It is working well. 运行良好。 But the problem is, in the above example, everything is done in command prompt. 但是问题是,在上面的示例中,所有操作都在命令提示符下完成。 But i am trying to do everything in PHP. 但是我正在尝试用PHP做所有事情。 I am not able to get any example in PHP. 我无法在PHP中获得任何示例。

So far, i have started with these piece of codes, 到目前为止,我已经从这些代码开始,

    $connection =   new Mongo();
    $db     =   $connection->selectDB('TestDB');
    $db     =   $connection->TestDB;

    $connection->selectDB('admin')->command(array('addshard'=>'host:port'));
    $connection->selectDB('admin')->command(array('enablesharding'=>'TestDB'));
    $connection->selectDB('admin')->command(array('shardcollection'=>'TestDB.large', 'key' => '_id'));

It is not working. 它不起作用。 Also, i dont know how to set shard servers and config database in PHP. 另外,我不知道如何在PHP中设置分片服务器和配置数据库。

Is there any other way to do MongoDB sharding in PHP? 还有其他方法可以在PHP中进行MongoDB分片吗?

MongoDB's database commands are case-sensitive. MongoDB的数据库命令区分大小写。 You were passing in lowercase command names, while the real commands are camelCased (see: sharding commands ). 您传递的是小写的命令名称,而真实的命令是驼峰式的(请参阅: sharding命令 )。 Additionally, the key paremeter of shardCollection needs to be an object. 另外, shardCollectionkey参数必须是一个对象。

A corrected version of your above code would look like: 您的上述代码的更正版本如下所示:

$connection->selectDB('admin')->command(array('addShard'=>'host:port'));
$connection->selectDB('admin')->command(array('enableSharding'=>'TestDB'));
$connection->selectDB('admin')->command(array('shardCollection'=>'TestDB.large', 'key' => array('_id' => 1)));

Additionally, you should have been able to examine the command result of to determine why this wasn't working. 此外,您应该能够检查的命令结果以确定为什么此命令不起作用。 The ok field would be zero, and an errmsg field would explain the error (eg "no such cmd: addshard"). ok字段将为零,而errmsg字段将说明该错误(例如“ no such cmd:Addshard”)。

Lastly, if you're attempting to mimic the shell's functionality to configure sharding in PHP, you may realize that some of the shell methods are more than just database commands. 最后,如果您试图模仿Shell的功能来配置PHP中的分片,您可能会意识到某些Shell方法不仅仅是数据库命令。 For any of those methods, you can get the JS source by omitting the parentheses. 对于任何一种方法,都可以通过省略括号来获取JS源代码。 For example: 例如:

> sh.addTagRange
function ( ns, min, max, tag ) {
    var config = db.getSisterDB( "config" );
    config.tags.update( {_id: { ns : ns , min : min } } ,
            {_id: { ns : ns , min : min }, ns : ns , min : min , max : max , tag : tag } ,
            true );
    sh._checkLastError( config );
}

That would be a useful hint for what an addTagRange() function in PHP would need to do. 对于PHP中的addTagRange()函数需要做什么,这将是一个有用的提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM