web analytics

Category MongoDB

Fatal error: Uncaught exception ‘MongoCursorException’ with message ‘localhost:27017: not authorized for query on admin.system.namespaces’ – Moadmin error after enabling authentication in mongodb

If you enabled authentication for mongodb, then moadmin will fail to show the databases. This is because moadmin is  not authenticated to list the databases. You might get the following error.

Fatal error: Uncaught exception ‘MongoCursorException’ with message ‘localhost:27017: not authorized for query on admin.system.namespaces’

I am considering a scenario where I enabled authentication as said in the link here. In that case open your moadmin.php file and make the following change, so that it authenticates itself.

You need to edit MONGO_CONNECTION parameter.

root@abc:~# vi /home/abc/public_html/moadmin.php

#define(‘MONGO_CONNECTION’, ”);        // Comment out the old connection parameter
define(‘MONGO_CONNECTION’, ‘mongodb://admin:admin@127.0.0...

Read More

Authentication in mongodb

Mysql databases allow username and password. Similarly if you require to keep authentication for your mongodb database as well, please do the following. Suppose, I have a database named abc, and I need to create a user named abc with password abc123.

First, create an admin user.

root@abc:~# mongo
MongoDB shell version: 2.4.10
connecting to: test
> use admin
> db.addUser("admin", "admin")

> use abc                   (Switch to the database abc)

> db.addUser("abc", "abc123")

Quit the mongoshell and stop mongo

root@abc:~# /etc/init.d/mongodb stop

root@abc:~# vi /etc/mongodb.conf    and make the following change

auth = true

root@abc:~#/etc/init.d/mongodb start

Now we need to test if authentication works...

Read More

Sharding in MongoDB

Sharding refers to the process of distributing data across multiple servers which helps in retrieving it easily. When the data set is huge, it is said that sharding significantly improves the query response time.

Let us see how we can implement sharding. Sharding requires a  minimum of three servers for test purpose. In production scenario, sharding may require more servers.

Sharding is implemented through the following :-

1) Config Servers – These servers contain the metadata as to where the data belongs to. Means, it holds data as to which shard each data goes. A minimum of 2 config servers in a huge databases system is recommended.

2) Query Router Servers – These servers are the ones that communicate with the application directly...

Read More

Fatal error: Uncaught exception ‘MongoCursorTimeoutException’ with message ‘hostname:27017: Read timed out after reading 0 bytes, waited for 30.000000 seconds’

I was trying to insert a 7 lakh data into a collection and I got the following error.

Fatal error: Uncaught exception ‘MongoCursorTimeoutException’ with message ‘hostname:27017: Read timed out after reading 0 bytes, waited for 30.000000 seconds’

To fix this, add the following to the top of your PHP page.

MongoCursor::$timeout = -1;

 

Read More

ERROR: Error creating index db.collection-101613_nno.csv: 16633 err: “text search not enabled”

I was trying to restore a database onto a new server, when I got the following error.

mongorestore -d db /backup/db

ERROR: Error creating index db.collection-101613_nno.csv: 16633 err: “text search not enabled”

In order to correct this, you need to enabled text search. Simply stop the mongo process and start it with text search enabled.

root@abc# /etc/rc.d/init.d/mongod stop

root@abc# /usr/bin/mongod -f /etc/mongod.conf –setParameter textSearchEnabled=true

Now try restoring again and it works.

Read More

MongoDB backup/restore

Want to know how the backup of mongo database can be taken. Read below…

Four tools are described here.

1) mongoexport

mongoexport can be used to take the backup of a mongo database. It takes the backup to a CSV, TSV or JSON files. The syntax is as follows.


mongoexport -d db_name -c test -o db_name.csv

where -d is the database name
-c is the collection name
-o is the file name to which the backup is to be taken


2) mongoimport

mongoimport can be used to import a backup to a database. You can import from the backup file as follows.


mongoimport -d db_restore -c test –file db_name.csv

where -d is the name of the database to which the file is to be restored
-c is the collection name
–file is the backup file

3) mongodump

For those who need a complete backup of the database, you can use mongod...

Read More

PHP Script to create a test database, collection and data in mongo for testing purpose

If you need an immediate mongo database with data for test purpose, you can use the following script.

The script to be put in a nutshell – it will create a database named commandtest, create a collection named customer inside it. The collection customer has two fields – customerid, company

root@abc# vi createdb.php

<?php
$m = new MongoClient();    // connect
$db = $m->commandtest;            // create database commandtest
$chars = “abcdefghijklmnopqrstuvwxyz”;
$arraykeys = range(‘a’, ‘z’);
$collection = $db->customer;   // create collection customer
for($i=0;$i<100;$i++)
{
$string = substr(str_shuffle($chars), 0, 5);
$document = array(“customer_id” => $i.”_”.$string,”company” => $string );
$collection->insert($document);
}
?>

Now try executing the script from backend its...

Read More

Getting started with MONGO shell

New to mongo and have trouble starting???? I shall help with a few basic commands.

Inorder to go to the shell just type mongo


root@abc# mongo
MongoDB shell version: 2.4.5
connecting to: test
>

The default database that will be selected is test, but you need to work with your own databases right, let’s start.

1) Create database.

Creating database is very easy in mongo. You can just create it with the use command. The ‘use’ command selects a database if it exists, or creates a new one if it doesn’t. It is not necessary to provide any data other than db name while creating database.


> use commandtest
switched to db commandtest

2) Create tables (collection)

Unlike mysql, database tables are called collections in mongodb...

Read More

“WARNING: You are running on a NUMA machine We suggest launching mongod like this to avoid performance problems: ** numactl –interleave=all mongod [other options]

Mongo was working fine in my Ubuntu machine, but whenever I access mongo shell, it will display the following warning.

root@abc:~# mongo
MongoDB shell version: 2.4.5
connecting to: test
Server has startup warnings:
Tue Aug 13 03:47:13.764 [initandlisten]
Tue Aug 13 03:47:13.764 [initandlisten] ** WARNING: You are running on a NUMA machine.
Tue Aug 13 03:47:13.764 [initandlisten] **          We suggest launching mongod like this to avoid performance problems:
Tue Aug 13 03:47:13.764 [initandlisten] **              numactl –interleave=all mongod [other options]
Tue Aug 13 03:47:13.764 [initandlisten]

First I checked, whether my hardware is NUMA(Non-Uniform Access Memory) enabled. I got the following output which said the answer is yes, it is NUMA enabled.

root@abcl# ...

Read More

PHP cannot access MongoDB, you need to install the Mongo extension for PHP.

When you access the phpMoAdmin to manage your mongo databases, sometimes you may get the following error.

PHP cannot access MongoDB, you need to install the Mongo extension for PHP.”

This is because mongo is installed, but the mongo extension for php is not installed. You can install it using pecl.

pecl install mongo

Once it finishes, add the mongo.so file to your php.ini and restart apache.

You can find your php.ini as follows.

php -i | grep ini and check for the following line

“Loaded Configuration File => /usr/local/lib/php.ini”

Now open that file and make the changes

vi /usr/local/lib/php.ini

Add the following down to any already present extensions

extension=”mongo.so”

/etc/rc.d/init.d/httpd restart

Now clear the browser cache and try accessing the phpMoAdmin once again, it shou...

Read More