我需要为Docker容器中的MongoDB实例创建一个运行状况检查。
虽然我可以使用CLI进行解决方法并使用Mongo Ping,但最好的选择是创建简单的HTTP或TCP测试。标准ping测试中默认的27017端口没有响应。
有没有值得信赖的方法呢?
答案 0 :(得分:15)
我为mongodb创建了一个运行状况检查,它使用<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/rl1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/thumb_detail"
android:layout_width="110dp"
android:layout_height="149dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp" />
<TextView
android:id="@+id/titolo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:layout_toRightOf="@+id/thumb_detail"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:text="Main Title" />
<TextView
android:id="@+id/release_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/titolo"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:layout_toRightOf="@+id/thumb_detail"
tools:text="Data rilascio" />
<TextView
android:id="@+id/author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/release_date"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:layout_toRightOf="@+id/thumb_detail"
tools:text="autore" />
<TextView
android:id="@+id/voto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/author"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:layout_toRightOf="@+id/thumb_detail"
tools:text="Voto" />
<TextView
android:id="@+id/durata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/voto"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:layout_toRightOf="@+id/thumb_detail"
tools:text="Durata" />
<TextView
android:id="@+id/generi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/durata"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:layout_toRightOf="@+id/thumb_detail"
tools:text="Genere" />
</RelativeLayout>
<TextView
android:id="@+id/titDesc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/rl1"
android:layout_marginLeft="2dp"
android:layout_marginTop="3dp"
android:textColor="#000000"
android:textSize="18dp"
tools:text="Descrizione" />
<TextView
android:id="@+id/descr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/titDesc"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp" />
</RelativeLayout>
客户端向服务器发送一个简单的查询请求(例如mongo
)。
db.stats()
您也可以在一行中执行此操作:
$ mongo 192.168.5.51:30000/test
MongoDB shell version: 3.2.3
connecting to: 192.168.5.51:30000/test
mongos> db.stats()
{
"raw" : {
"set1/192.168.5.52:27000,192.168.5.53:27000" : {
"db" : "test",
"collections" : 8,
"objects" : 50,
"avgObjSize" : 73.12,
"dataSize" : 3656,
"storageSize" : 53248,
"numExtents" : 8,
"indexes" : 8,
"indexSize" : 65408,
"fileSize" : 469762048,
"nsSizeMB" : 16,
"dataFileVersion" : {
"major" : 4,
"minor" : 6
},
"extentFreeList" : {
"num" : 28,
"totalSize" : 184807424
},
"ok" : 1
}
},
"objects" : 50,
"avgObjSize" : 73,
"dataSize" : 3656,
"storageSize" : 53248,
"numExtents" : 8,
"indexes" : 8,
"indexSize" : 65408,
"fileSize" : 469762048,
"extentFreeList" : {
"num" : 28,
"totalSize" : 184807424
},
"ok" : 1
}
希望它有所帮助。
<强>更新强>
正如@luckydonald所说,$ echo 'db.stats().ok' | mongo 192.168.5.51:30000/test --quiet
1
命令更好,所以你可以这样做:
ping
感谢@luckydonald。
答案 1 :(得分:3)
如果只需要简单的“ ping”,则也可以使用curl
:
curl --connect-timeout 10 --silent --show-error hostname:27017
如果收到“错误” It looks like you are trying to access MongoDB over HTTP on the native driver port.
,则表明您的MongoDB正在运行并回答答案。
或使用
mongo --norc --quiet --host=hostname:27017 <<< "db.getMongo()"
答案 2 :(得分:2)
一种解决方案是使用最小的以脚本语言编写的MongoDB客户端,您的容器中有相应的解释器。
例如,这是Python中的零依赖关系:mongo_ping_client.py