I am currently working on some personal tests and benchmarks to compare the workflow and efficiency between using MongoDB and MySQL with real world example data.
To setup my data in each database I am doing several thousand loops and randomly creating data objects to insert into the database.
However I am having some issues using the Mongo class in PHP which I cannot solve. The problem is like so:
I have a loop which creates a new Mongo instance and connection, inserts a small array into a collection and then closes the connection. This loop should run 20000 times. However it is always failing around the 16300nd loop (with a min of 16200 and max of 16350 I’d say after a few runs) when it attempts to create the instance/make a connection.
The code in the loop is below:
$data = get_random_user_data();
$mongo = new Mongo('mongodb://admin:password@localhost:27017/test');
if ($mongo->test->users->insert($data)) {
$users[] = array('id' => $data['_id'], 'name' => $data['username']);
echo $i." - Added user: ".$data['username'].'<br/>';
}
$mongo->close();
get_random_user_data() just returns a simple associative array.
The error I get is:
Fatal error: Uncaught exception 'MongoConnectionException' with message 'Unknown error'
On the line:
$mongo = new Mongo('mongodb://admin:password@localhost:27017/test');
Any ideas? Is there something fundamental I am missing like some security or spam-prevention?
Thanks in advance.
Extra info:
The script dies at about 114.9797 seconds. It’s not a PHP memory or time based issue as all the limits are raised and I ran my MySQL benchmarks yesterday inserting 120000 rows (with the same method of looping open connection, insert, close connection) over about an hour with no problems.
Running PHP Version 5.3.5
phpinfo Mongo info:
MongoDB Support enabled
Version 1.2.0-
Directive Local Value Master Value
mongo.allow_empty_keys 0 0
mongo.allow_persistent 1 1
mongo.auto_reconnect 1 1
mongo.chunk_size 262144 262144
mongo.cmd $ $
mongo.default_host localhost localhost
mongo.default_port 27017 27017
mongo.long_as_object 0 0
mongo.native_long 0 0
mongo.no_id 0 0
mongo.utf8 1 1
Your operating system has a limited number of sockets it’s willing to open. When you open a socket and then close it, the OS doesn’t immediately put it back in the “available” pool, it hangs out for a while in “time wait” state, that Nat mentions in his answer.
You can increase the number of sockets your OS will open, see http://www.mongodb.org/display/DOCS/Too+Many+Open+Files (each socket is an open “file”).
Also, you’re using a pretty old version of the driver, you might want to consider upgrading.