web analytics

Category Databases

PHP Warning: mysql_connect(): No such file or directory

I was getting the following error while trying out a basic mysql connection script.

PHP Warning: mysql_connect(): No such file or directory

The script I used was as follows.

<?php
$mysql_hostname = “localhost”;
$mysql_user = “root”;
$mysql_password = “abc123”;
$mysql_database = “testdb”;

$link = mysql_connect($mysql_hostname, $mysql_user, $mysql_password);
mysql_select_db($mysql_database,$link);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
else
echo ‘Connected successfully’;
?>

Executing the script returned the following error.

root@abc:/var/www/html/scripts# php test.php
PHP Warning:  mysql_connect(): No such file or directory in /var/www/html/scripts/test.php on line 7
PHP Warning:  mysql_select_db() expects parameter 2 to be resource, boolean given in /var/www/html/...

Read More

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

[ERROR] Fatal error: Can’t open and lock privilege tables: Table ‘mysql.host’ doesn’t exist

I was working on a mysql server one day and mysql was refusing to start. There were no errors in logs, hence I started mysql with mysqld_safe which displayed the error as follows.

[ERROR] Fatal error: Can’t open and lock privilege tables: Table ‘mysql.host’ doesn’t exist

Upon checking the data dir at /var/lib/mysql, I noticed that the folder for mysql database is missing. Hence I issued the following command and it installed the db just fine. After that I was able to restart mysql without issues.

mysql_install_db

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