简体   繁体   English

如何在Perl脚本中断开与MongoDB的连接?

[英]How to disconnect from MongoDB in a Perl script?

I am trying to write a Perl5 script that checks the status of a MongoDB server every minute and notifies me if it is down. 我正在尝试编写一个Perl5脚本,每分钟检查一次MongoDB服务器的状态,并通知我它是否已关闭。 Any suggestion is welcome for this task. 任何建议都欢迎这项任务。 Currently, I am using 'MongoDB' module and 'MongoDB::MongoClient' to establish a connection to see if server is available. 目前,我使用'MongoDB'模块和'MongoDB :: MongoClient'建立连接以查看服务器是否可用。 Here is the basic idea: 这是基本的想法:

while(1)
{
        my $conn = connectMongoDB();

        if($conn){
                sleep 60;
        }   
        else{
                sendMail();
                last;
        }   
}

sub connectMongoDB
{
        my $client;

        eval{ $client =  MongoDB::MongoClient->new( host => "mongodb://:\@$server");};

        return ($client)?1:0;       
}

The main problem is that there is no way to disconnect from the server. 主要问题是无法断开与服务器的连接。 Here is what it says on the cpan page: 以下是它在cpan页面上的内容:

"There is no way to explicitly disconnect from the database. However, the connection will automatically be closed and cleaned up when no references to the MongoDB::MongoClient object exist, which occurs when $client goes out of scope (or earlier if you undefine it with undef)." “没有办法明确地与数据库断开连接。但是,当没有对MongoDB :: MongoClient对象的引用时,连接将自动关闭并清除,当$ client超出范围时会发生(或者如果你取消定义则更早)它与undef)。“

I tried 'undef' and subroutines. 我试过'undef'和子程序。 None of them terminates the connection. 它们都没有终止连接。 Because of the loop, the number of connections keep increasing. 由于循环,连接数量不断增加。 Is there any other method I can try to keep the number of connections under control? 有没有其他方法可以尝试控制连接数量?

Any other suggestion for tackling this issue is appreciated as long as it doesn't involve crontab. 只要不涉及crontab,任何其他解决此问题的建议都会受到赞赏。

Thanks. 谢谢。

EDIT: 编辑:

This is the output when sleep time is set to 2 secs where db version is v2.4.4 and MongoDB module version is 0.701.4. 这是睡眠时间设置为2秒时的输出,其中db版本为v2.4.4且MongoDB模块版本为0.701.4。 The problem persists after upgrading MongoDB module to 0.702.1. 将MongoDB模块升级到0.702.1后问题仍然存在。

Fri Aug 16 20:33:06.986 [initandlisten] connection accepted from 127.0.0.1:51031 #3 (1 connection now open)
Fri Aug 16 20:33:08.989 [initandlisten] connection accepted from 127.0.0.1:51033 #4 (2 connections now open)
Fri Aug 16 20:33:10.991 [initandlisten] connection accepted from 127.0.0.1:51034 #5 (3 connections now open)
Fri Aug 16 20:33:12.994 [initandlisten] connection accepted from 127.0.0.1:51035 #6 (4 connections now open)
Fri Aug 16 20:33:14.996 [initandlisten] connection accepted from 127.0.0.1:51036 #7 (5 connections now open)
Fri Aug 16 20:33:16.999 [initandlisten] connection accepted from 127.0.0.1:51038 #8 (6 connections now open)
Fri Aug 16 20:33:19.003 [initandlisten] connection accepted from 127.0.0.1:51039 #9 (7 connections now open)
Fri Aug 16 20:33:21.006 [initandlisten] connection accepted from 127.0.0.1:51040 #10 (8 connections now open)
Fri Aug 16 20:33:23.009 [initandlisten] connection accepted from 127.0.0.1:51042 #11 (9 connections now open)
Fri Aug 16 20:33:25.013 [initandlisten] connection accepted from 127.0.0.1:51043 #12 (10 connections now open)
Fri Aug 16 20:33:27.016 [initandlisten] connection accepted from 127.0.0.1:51044 #13 (11 connections now open)
Fri Aug 16 20:33:29.019 [initandlisten] connection accepted from 127.0.0.1:51045 #14 (12 connections now open)
Fri Aug 16 20:33:31.022 [initandlisten] connection accepted from 127.0.0.1:51047 #15 (13 connections now open)

EDIT 2: SOLVED!! 编辑2:已解决!!

I believe the problem is related to auto_connect option. 我相信这个问题与auto_connect选项有关。 Once it is disabled, The code works fine. 一旦禁用,代码工作正常。 Below is something that works for now. 以下是现在有用的东西。

sub connectMongoDB
{
        my $client;

        eval{
                $client =  MongoDB::MongoClient->new( host => "mongodb://:\@$server",
                      auto_connect => 0); 
                $client->connect;
        };  
        return (!$@)?1:0;
}

Best 最好

I'm unable to reproduce this problem with MongoDB 2.4.1 and version 0.702.1 of the MongoDB driver from CPAN. 我无法使用CPAN的MongoDB 2.4.1和版本0.702.1的MongoDB驱动程序重现此问题。

I ran your code with the sleep timeout changed to 10 seconds, connecting to localhost. 我运行你的代码,睡眠超时改为10秒,连接到localhost。 After running for a couple minutes, running db.serverStatus() in the mongo shell continued to show just one connection. 运行几分钟后,在mongo shell中运行db.serverStatus()继续只显示一个连接。 (That's the connection for the shell session.) (这是shell会话的连接。)

Here's the output from mongod showing the connections being opened and closed every ten seconds. 这是mongod的输出,显示连接每十秒打开和关闭一次。 (You can see that I fired up the shell after the first two connections cycled.) (你可以看到我在前两个连接循环后启动了shell。)

Fri Aug 16 21:36:43.589 [conn1] end connection 127.0.0.1:57722 (0 connections now open)
Fri Aug 16 21:36:53.526 [initandlisten] connection accepted from 127.0.0.1:57723 #2 (1 connection now open)
Fri Aug 16 21:36:53.527 [conn2] end connection 127.0.0.1:57723 (0 connections now open)
Fri Aug 16 21:36:58.421 [initandlisten] connection accepted from 127.0.0.1:57724 #3 (1 connection now open)
Fri Aug 16 21:37:03.529 [initandlisten] connection accepted from 127.0.0.1:57725 #4 (2 connections now open)
Fri Aug 16 21:37:03.529 [conn4] end connection 127.0.0.1:57725 (1 connection now open)
Fri Aug 16 21:37:13.531 [initandlisten] connection accepted from 127.0.0.1:57726 #5 (2 connections now open)
Fri Aug 16 21:37:13.532 [conn5] end connection 127.0.0.1:57726 (1 connection now open)
Fri Aug 16 21:37:23.534 [initandlisten] connection accepted from 127.0.0.1:57727 #6 (2 connections now open)
Fri Aug 16 21:37:23.535 [conn6] end connection 127.0.0.1:57727 (1 connection now open)
Fri Aug 16 21:37:33.537 [initandlisten] connection accepted from 127.0.0.1:57728 #7 (2 connections now open)
Fri Aug 16 21:37:33.538 [conn7] end connection 127.0.0.1:57728 (1 connection now open)
Fri Aug 16 21:37:43.540 [initandlisten] connection accepted from 127.0.0.1:57729 #8 (2 connections now open)
Fri Aug 16 21:37:43.540 [conn8] end connection 127.0.0.1:57729 (1 connection now open)
Fri Aug 16 21:37:53.542 [initandlisten] connection accepted from 127.0.0.1:57730 #9 (2 connections now open)
Fri Aug 16 21:37:53.543 [conn9] end connection 127.0.0.1:57730 (1 connection now open)
Fri Aug 16 21:38:03.545 [initandlisten] connection accepted from 127.0.0.1:57731 #10 (2 connections now open)
Fri Aug 16 21:38:03.545 [conn10] end connection 127.0.0.1:57731 (1 connection now open)
Fri Aug 16 21:38:13.547 [initandlisten] connection accepted from 127.0.0.1:57732 #11 (2 connections now open)
Fri Aug 16 21:38:13.548 [conn11] end connection 127.0.0.1:57732 (1 connection now open)
Fri Aug 16 21:38:23.550 [initandlisten] connection accepted from 127.0.0.1:57734 #12 (2 connections now open)
Fri Aug 16 21:38:23.550 [conn12] end connection 127.0.0.1:57734 (1 connection now open)
Fri Aug 16 21:38:33.552 [initandlisten] connection accepted from 127.0.0.1:57735 #13 (2 connections now open)
Fri Aug 16 21:38:33.553 [conn13] end connection 127.0.0.1:57735 (1 connection now open)
Fri Aug 16 21:38:43.555 [initandlisten] connection accepted from 127.0.0.1:57736 #14 (2 connections now open)
Fri Aug 16 21:38:43.555 [conn14] end connection 127.0.0.1:57736 (1 connection now open)
Fri Aug 16 21:38:53.557 [initandlisten] connection accepted from 127.0.0.1:57737 #15 (2 connections now open)
Fri Aug 16 21:38:53.559 [conn15] end connection 127.0.0.1:57737 (1 connection now open)
Fri Aug 16 21:39:03.560 [initandlisten] connection accepted from 127.0.0.1:57738 #16 (2 connections now open)
Fri Aug 16 21:39:03.561 [conn16] end connection 127.0.0.1:57738 (1 connection now open)
Fri Aug 16 21:39:13.563 [initandlisten] connection accepted from 127.0.0.1:57740 #17 (2 connections now open)
Fri Aug 16 21:39:13.564 [conn17] end connection 127.0.0.1:57740 (1 connection now open)
Fri Aug 16 21:39:23.566 [initandlisten] connection accepted from 127.0.0.1:57741 #18 (2 connections now open)
Fri Aug 16 21:39:23.567 [conn18] end connection 127.0.0.1:57741 (1 connection now open)
Fri Aug 16 21:39:33.569 [initandlisten] connection accepted from 127.0.0.1:57747 #19 (2 connections now open)
Fri Aug 16 21:39:33.570 [conn19] end connection 127.0.0.1:57747 (1 connection now open)
Fri Aug 16 21:39:43.572 [initandlisten] connection accepted from 127.0.0.1:57748 #20 (2 connections now open)
Fri Aug 16 21:39:43.573 [conn20] end connection 127.0.0.1:57748 (1 connection now open)
Fri Aug 16 21:39:53.575 [initandlisten] connection accepted from 127.0.0.1:57750 #21 (2 connections now open)
Fri Aug 16 21:39:53.576 [conn21] end connection 127.0.0.1:57750 (1 connection now open)
Fri Aug 16 21:40:03.579 [initandlisten] connection accepted from 127.0.0.1:57751 #22 (2 connections now open)
Fri Aug 16 21:40:03.579 [conn22] end connection 127.0.0.1:57751 (1 connection now open)
Fri Aug 16 21:40:13.580 [initandlisten] connection accepted from 127.0.0.1:57753 #23 (2 connections now open)
Fri Aug 16 21:40:13.581 [conn23] end connection 127.0.0.1:57753 (1 connection now open)
Fri Aug 16 21:40:23.584 [initandlisten] connection accepted from 127.0.0.1:57754 #24 (2 connections now open)
Fri Aug 16 21:40:23.584 [conn24] end connection 127.0.0.1:57754 (1 connection now open)
Fri Aug 16 21:40:33.586 [initandlisten] connection accepted from 127.0.0.1:57755 #25 (2 connections now open)
Fri Aug 16 21:40:33.586 [conn25] end connection 127.0.0.1:57755 (1 connection now open)
Fri Aug 16 21:40:43.588 [initandlisten] connection accepted from 127.0.0.1:57758 #26 (2 connections now open)
Fri Aug 16 21:40:43.588 [conn26] end connection 127.0.0.1:57758 (1 connection now open)
Fri Aug 16 21:40:53.590 [initandlisten] connection accepted from 127.0.0.1:57760 #27 (2 connections now open)
Fri Aug 16 21:40:53.591 [conn27] end connection 127.0.0.1:57760 (1 connection now open)
Fri Aug 16 21:41:03.592 [initandlisten] connection accepted from 127.0.0.1:57761 #28 (2 connections now open)
Fri Aug 16 21:41:03.593 [conn28] end connection 127.0.0.1:57761 (1 connection now open)

Can you let me know what version of MongoDB (the server) and MongoDB (the driver) you're using? 你能告诉我你正在使用的MongoDB(服务器)和MongoDB(驱动程序)的版本吗?

Change the subroutine as the following, 更改子程序如下,

sub connectMongoDB
{
        my $client;

        eval{
                $client =  MongoDB::MongoClient->new( host => "mongodb://:\@$server",
                      auto_connect => 0); 
                $client->connect;
        };  
        return (!$@)?1:0;
}

The problem was the 'auto_reconnect' option all along. 问题是'auto_reconnect'选项一直以来。

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

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