February 2014

català Welcome to LSI.


This document will cover how to establish connection with the MongoDB server. It will show how to access in text mode through the command line and connection examples for PHP and JAVA.

Service access information

Server name:
Puerto: 27017

You can get a username and password by mail at or by web at

NOTE: Database name will be the same as your username.

Text mode connection

In order to connect in textual mode (terminal or Command Line Interface) we can type this command while in a Unix system:


This way a connection with the server will be established, but it is still necessary to authenticate against the database:

use <database>

With “show collections;” we can check if we can list the contents of our database:

show collections

Php connection example

Prior to establishing the connection , it is necessary to prepare the system and install the MongoDB PHP library

  1. Install the system manager modules PHP (PEAR), the PHP development libraries and library libpcre3-dev

    sudo apt-get install php-pear php5-dev libpcre3-dev

    This step is necessary to install later MongoDB libraries for PHP.
    You may apt-get install additional libraries. This is not a problem.

  2. Install MongoDB for PHP with PEAR package manager:

    sudo pecl install mongodb

    If the process is completed, it will indicate the next step:

    You should add "" to php.ini

  3. Open the php.ini file and edit it. By default usually one for the command line (CLI) and one for Apache in the following PATH


If we want to establish connection from a PHP environment, we can do it by using the next code snippet:

$host = "";
$username = "<username>";
$password = "<password>";
$database = "<database>";

$manager = new MongoDB\Driver\Manager("mongodb://$username:$password@$host:27017/$database");

$query = new MongoDB\Driver\Query(array(), array());
$rows = $manager->executeQuery("$database.system.users", $query);
foreach($rows as $r){

If it works, running the script will produce the following output:

stdClass Object
     [_id] => MongoDB\BSON\ObjectID Object
        [oid] => orh9qufhsdkfakfiasfhkjff
     [user] => <username>
     [readOnly] =>
     [pwd] => 9847ifhif9238rf923ry9fisdhf938yr

Java connection example

To establish connection from a java project, it is necessary to download the driver mongo-java-driver-2.10.1.jar, add it to the CLASSPATH and import it with the commands:

import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.ServerAddress;

Then, execute this snipped to connect:

String server = "";
String username = "<username>";
String password = "<password>";
String database = "<database>";

MongoClient mongoClient = new MongoClient(server);
DB db = mongoClient.getDB(database);
boolean auth = db.authenticate(username,password.toCharArray());

In order to know if it has worked correctly, we can try to list our collections:

Set<String> colls = db.getCollectionNames();
for (String s : colls) {


  1. mongoDB manual:
  2. mongoDB connection from PHP:
  3. mongoDB connection from Java: