SocketTimeoutException

Source: Internet
Author: User
Tags chmod install mongodb mkdir mongodb mongodb query mongodb server socket

CentOS compilation and installation of php extensions for MongoDB and mongoDB

Download the mongo source code package, pcre, epel, and js

1 # wget http://downloads.mongodb.org/src/mongodb-src-r1.8.1.tar.gz
# Wget http://ftp.mozilla.org/pub/mozilla.org/js/js-1.7.0.tar.gz
# Wget http://sourceforge.net/projects/pcre/files/pcre/8.12/pcre-8.12.tar.bz2

Install python:

# Yum install-y python-devel

Install scons: Download scons

Tar zxf scons-2.0.1.tar.gz

Cd scons-2.0.1
Python setup. py install


Install the spidermonkey library and download the js api library that supports c. Click here to download js-1.7.0.tar.gz

Yum install-y boost-devel

Tar zxvf js-1.7.0.tar.gz
Cd js/src/
Export CFLAGS = "-DJS_C_STRINGS_ARE_UTF8"
Make-f Makefile. ref
JS_DIST =/usr gmake-f Makefile. ref export
Cd ../..

 

Install pcre


Tar zxf pcre-8.12.tar.gz

Cd pcre-8.12
./Configure -- enable-utf8 -- enable-unicode-properties
Make & make install
Cd ..


Install MongoDB


Tar zxf mongodb-src-r1.8.1.tar.gz

Cd mongodb-src-r1.8.1
Scons all // scons may fail to find the pcre library (modify/etc/ld. so. conf is useless, is the scons itself), then you need to open the mongodb-src-r1.8.0 under the SConstruct, find [linux2 "= OS. sys. platform:], add the installation path of the pcrecpp Library after LIBPATH, add the pcrecpp Library name after LIBS, and then re-scons all (Operation: vim SConstruct; original: env. append (LIBPATH = ["/usr/lib64", "/lib64"]); after modification, env. append (LIBPATH = ["/usr/lib64", "/lib64", "/usr/local/pcre/lib"]); next in env. append (LIBS = ["pthread"]) followed by env. append (LIBS = ["libpcrecpp"])

Scons -- prefix =/usr/local/mongo install

To install lib and head, use the following method:

Scons -- prefix =/usr/local/mongo -- full install


Create a configuration file

Mkdir-p/usr/local/mongo/etc/usr/local/mongo/data/usr/local/mongo/log // usr/local/mongo/repair

Vim/usr/local/mongo/etc/mongo. conf

Add the following content to mongo. conf.

Dbpath =/usr/local/mongo/data

Logpath =/usr/local/mongo/mongodb. log

Repairpath =/usr/local/mongo/repair

Pidfilepath =/usr/local/mongo/mongodb. pid

Directoryperdb = true

Logappend = true

Noauth = true

Port = 27017

MaxConns = 1024

Fork = true

Rest = true

Quota = true

QuotaFiles = 1024.

Nssize = 16


Start mongodb


Ln-s/usr/local/mongo/bin/mongod/usr/bin/mongod

Mongod-f/usr/local/mongo/etc/mongo. conf

Check whether it is started, but it is unwise to use this method to manage the mongodb server. Let's improve it:

 
Mkdir-p/usr/local/mongo/srv

Vim/usr/local/mongo/srv/mongodb-start

Add the following content



#! /Bin/sh

Mongod-f/usr/local/mongo/etc/mongo. conf



Vim/usr/local/mongo/srv/mongodb-stop

Add the following content


#! /Bin/bash

Pid = 'PS-o pid, command ax | grep mongod | awk '! /Awk /&&! /Grep/{print $1 }'';

If ["$ {pid }"! = ""]; Then

Kill-2 $ {pid };

Fi

Add execution permission



Chmod a + x/usr/local/mongo/srv/mongodb-start

Chmod a + x/usr/local/mongo/srv/mongodb-stop

Vim/etc/rc. d/init. d/mongodb

Add the following content

#! /Bin/sh
#
# Mongodb-this script starts and stops the mongodb daemon
#
# Chkconfig:-85 15
# Description: MongoDB is a non-relational database storage system.
# Processname: mongodb
# Config:/usr/local/mongo/etc/mongo. conf
# Pidfile:/usr/local/mongo/mongodb. pid
PATH =/usr/local/mongo/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME = mongodb
Test-x $ DAEMON | exit 0
Set-e
Case "$1" in
Start)
Echo-n "Starting MongoDB ..."
/Usr/local/mongo/srv/mongodb-start
;;
Stop)
Echo-n "Stopping MongoDB ..."
/Usr/local/mongo/srv/mongodb-stop
;;
*)
N =/etc/init. d/$ NAME
Echo "Usage: $ N {start | stop}"> & 2
Exit 1
;;
Esac
Exit 0

Add Service

Chmod a + x/etc/rc. d/init. d/mongodb

Chkconfig -- add mongodb

Chkconfig -- level 345 mongodb on

/Etc/rc. d/init. d/mongodb start




MongoDB query timeout exception SocketTimeoutException


Aggregate a collection of more than one million records.

DBObject match = (DBObject) JSON. parse ("{$ match: {logType: {'$ in': [5, 9] }}}");
DBObject group = (DBObject) JSON. parse ("{$ group: {'_ id':' $ domainurl', 'count': {'$ sum': 1 }}}");
AggregationOutput output = logCollection. aggregate (match, group );
 

Occasionally, a Read timed out exception occurs.

Com. mongodb. MongoException $ Network: Read operation to server/192.168.10.202: 27017 failed on database adLogTable
At com. mongodb. DBTCPConnector. innerCall (dbtcpconne. java: 253)
At com. mongodb. DB. command (DB. java: 261)
At com. mongodb. DB. command (DB. java: 243 )...
Caused by: java.net. SocketTimeoutException: Read timed out
At java.net. SocketInputStream. socketRead0 (Native Method)
At java.net. SocketInputStream. read (Fig. java: 152)
 

After multiple tests, it is found that the average time for performing an aggregation operation is 5 s. If the time exceeds 5 s, an error is reported!

Then, view the MongoDB configuration information:

Socket-timeout = "5000"/5S

The default socket-timeout configuration is 0, that is, there is no limit.

There is no timeout limit, and it is not easy to find out a problem in the system. A reasonable timeout time should be provided according to the actual situation.

Multiple tests show that the maximum execution time is 6 seconds, and the timeout time is set to 10000.

Socket-timeout = "10000" // 10 s

Note: When MongoDB is integrated with Spring, if you want to configure multiple MongDB sources, only the last <mongo: options> configuration is enabled.

The parameter configuration information should be stored in the properties file.

<Mongo: mongo host = "$ {mongodb. ip}" id = "mongo202" port = "$ {mongodb. port}">
& Lt; mongo: options connections-per-host = "200"
Threads-allowed-to-block-for-connection-Multi-plier = "100"
Connect-timeout = "1000"
Max-wait-time = "1000"
Auto-connect-retry = "true"
Socket-keep-alive = "true"
Socket-timeout = "10000"
Slave-OK = "true"
Write-number = "1"
Write-timeout = "0"
Write-fsync = "true"/>
</Mongo: mongo>

Get configuration parameters through Java API

DBCollection logCollection = custom template. getCollection (collName );
Optional options exist options = logCollection. getDB (). getMongo (). Getmetadata options ();
System. out. println (gradient options. getSocketTimeout ());

Last point: ConnectionTimeOut and SocketTimeOut:

A complete request consists of three phases: 1. Establish a connection 2. Data transmission 3. Disconnect

If the connection time for a server (database) request exceeds ConnectionTimeOut, ConnectionTimeOutException is thrown, that is, the server connection times out and no connection is established within the specified time.

If the connection to the server is successful, data transmission starts.

If the time used by the server to process data exceeds SocketTimeOut, SocketTimeOutExceptin is thrown. That is, the server response times out and the server does not return data to the client within the specified time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.