Shantanu's Blog

Corporate Consultant

July 22, 2014


Install ipython notebook

Install python environment using anaconda on Ubuntu system.
Now configure and start ipython notebook from the URL that will look something like this...

log in to your ubuntu server and first cange to root user.

sudo bash
sudo su

cd /root/

Now follow the steps mentioned below:

1) Create SSL

openssl req -x509 -nodes -days 3650 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem

2) Downlaod and install anaconda



vi .bashrc
export PATH=/root/anaconda/bin:$PATH

3) Create a password:

>>> python
In [1]: from IPython.lib import passwd
In [2]: passwd()
Enter password:
Verify password:
Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'

4) Configure nbserver

ipython profile create nbserver

vi ~/.ipython/profile_nbserver/

c = get_config()

# Kernel config
c.IPKernelApp.pylab = 'inline'  # if you want plotting support always

# Notebook config
c.NotebookApp.certfile = u'/root/mycert.pem'
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
c.NotebookApp.password = u'sha1:d9bc2672358b:2fe102ad068357f63f4173f8d52b469d7a9e75ce'

# It is a good idea to put it on a known, fixed port
c.NotebookApp.port = 9999

May 20, 2014


Why mongoDB ?

1) MongoDB does not support ACID compliance
ACID compliance is necessary for financial database. Most of the data that we usually store needs to be fast without any dead-locks. MySQL is slow due to various locks those are completely eliminated in mongoDB.

2) MongoDB is schema-less
Before adding a row to MySQL table, you need to use "create table" statement to generate an empty table. MongoDB creates and updates the table on the fly as you go on adding rows. You can add 2 columns initially and then add 20 columns for the next row in mongoDB.
In other words problem of matching column count and data type is eliminated in mongoDB. This type of flexible, schema-less table structure is very useful for Entity - Attribute - Value (EAV) model. For e.g. while developing an application for hospital, I may need to save patient data. This data may vary from patient to patient and I can not decide the fixed column structure. Some patient may need blood_pressure while others may need to record heart_beat. These parameters needs to be time-stamped in order to check the progress of the patients. Such data is very difficult to save in MySQL. Right? see how it can be done in mongoDB

> db.patient.insert({"name": "shantanu", created: new Date() , "blood_pressure":  {high: 120, low: 80} })
> db.patient.insert({"name": "ram", created: new Date() , "blood_pressure":  {high: 160, low: 95} })

Now the doctor has measured the heart-rate of the patient "ram" and that record needs to be updated along with current date-time. mongoDB makes it easy:

> db.patient.update({"name": "ram"},{$set: {updated: new Date(), heart_beat:105}})

In other words "Attributes" like heart-beat and blood-pressure may be added on the fly for the "Entities" like patients and the "values" can be queried like this:

db.patient.find( { "blood_pressure": { "high": { $gt: 150 }}})

This query will find all the patients with high blood pressure more than 150.

3) MongoDB works best with non-normalized data

There are times when applying second or third normalized form to the MySQL database structure is not possible nor feasible.

Let's take an example of a table where I am saving all the phone numbers of customers:

create table customer(name varchar(255), phone varchar(255));
insert into customer values ("shantanu", "27848226, 26788485");
insert into customer values ("ram", "1278482269, 1267884859");

If I am searching for a phone number "26788485", I can execute a query like the following:

mysql> select * from customer where phone like ('%26788485%');
| name     | phone                  |
| shantanu | 27848226, 26788485     |
| ram      | 1278482269, 1267884859 |
2 rows in set (0.00 sec)

You can see there are 2 problems in the above query:
I) The like query with wild-card (%) sign will not use indexes. The query is slow.
II) The query is also returning the record for "ram" that is wrong.

Now let's see how mongoDB will handle this efficiently and elegantly.

# Start added records to "customer" collection. Each of the row is called "document"
> db.customer.insert({"name": "shantanu", "phone":["27848226", "26788485"]})
> db.customer.insert({"name": "ram", "phone":["1278482269", "1267884859"]})

# add an index on phone column
> db.customer.ensureIndex( { phone: 1 } )

# Check the explain plan to make sure the index is being used:
> db.customer.find({"phone":"26788485"}).explain()

As you can see from the explain plan, mongoDB is using index to find the phone number that is part of the comma separated string. This query will be many times faster compared to MySQL.

May 19, 2014


unique key in mongodb

There is a function called "last_insert_id" in mysql that will return the auto incremented id of the record recently added. MongoDB by default returns the object ID of the last inserted record. Interesting :)

>>> import pymongo
>>> client=pymongo.MongoClient()
>>> db=client.mydb
>>> db.testData.insert({"x": 211, "phone":["27848226", "26788485"]})


April 21, 2014


Install Joomla on centOS

Here are the steps those needs to be followed in order to install Joomla on centOS system. Once installed, we can disable InnoDB and restore the database dump backup to convert all tables to default TokuDB engine.

1) install php and http
2) Download and extract joomla
3) Export path
4) Create symbolic link for mysql socket file
5) Disable selinux
6) Disable firewall
7) Start http deamon
8) Start TokuDB mysql

yum install php php-mysql http

cd /var/www/html/
mkdir joomla
cd joomla



export PATH=$PATH:/home/ec2-user/percona-5.5.30-tokudb-7.0.1-fedora-x86_64/bin
ln -s  /var/lib/mysql/mysql.sock /tmp/mysql.sock

echo 0 >/selinux/enforce

vi /etc/sysconfig/selinux

service iptables stop
/etc/init.d/httpd start

cd /home/ec2-user/percona-5.5.30-tokudb-7.0.1-fedora-x86_64/
./bin/mysqld_safe &

mysql>grant all on *.* to 'joomla'@'localhost' identified by 'joomla' with grant option;
grant all on *.* to 'joomla'@'%' identified by 'joomla' with grant option;

Labels: ,

April 07, 2014


Logical Snapshots

1) In order to use LVM we need to store the mysql data on LVM partition. So the /etc/my.cnf file should point to new data directory and not the default /var/lib/mysql

vi /etc/my.cnf

2) The /data folder should be hosted by lvm managed disk. In order to create partition in LVM format, use the following steps when you add a new disk or when you format the server for the first time.

# make sure the new disk is detected
server1:~# fdisk -l

# change the format of the new disk - use the options shown below when ever prompted
fdisk /dev/xvdj


# check the new name of the LVM disk
fdisk -l

# add it to LVM
pvcreate /dev/xvdj1

# name the LVM
vgcreate lvm /dev/xvdj1

# create logical volume
lvcreate --name=mysql_data   --size=2000M lvm

# format the newly created volume
mkfs.ext3 /dev/lvm/mysql_data
# mount the volume
mkdir /data
mount /dev/lvm/mysql_data /data

# make sure that the data directory is pointing to the /data folder in my.cnf
# restore all the data from backup sql files

3) Create a lvm snapshot when mysql service is stopped for a few minutes:

# check the VG status

# create a snapshot of data directory
lvcreate --name=mysql_snap  --snapshot --size=2100M  /dev/lvm/mysql_data

# mount the snapshot
mkdir /mnt/mysql-snap
mount /dev/mapper/lvm-mysql_data /mnt/mysql-snap/

4) Create snapshots with timestamp and also mount them for easy access.

lvcreate --name=mysql_snap_7apr14  --snapshot --size=2100M  /dev/lvm/mysql_data

mkdir /mnt/mysql-snap_7apr14
mount /dev/mapper/lvm-mysql_snap_7apr14 /mnt/mysql-snap_7apr14/

It is however not possible to take a snapshot of snapshot.

# lvcreate --name=mysql_snap_7apr14b  --snapshot --size=24000M  /dev/lvm/mysql_snap1
  Snapshots of snapshots are not supported yet.

So you can only take a snapshot of the original drive.

# lvcreate --name=mysql_snap_7apr14a  --snapshot --size=21000M  /dev/lvm/mysql_data
  Logical volume "mysql_snap_7apr14a" created

//compress and unzip data

umount /original_data
dd if=/dev/mapper/lvm_new-mysql_original_data | gzip > snap01.gz

lvcreate --name=mysql_data_new_pratik   --size=20000M lvm_new
time gzip -d -c snap01.gz | dd of=/dev/mapper/mysql_data_new_pratik


// change the disk allocated to any volume

# vgchange -an lvm
  Can't deactivate volume group "lvm" with 3 open logical volume(s)

# Unmount any snapshots in order to disable them
umount /mnt/mysql-snap

# lvextend  -L+10G /dev/mapper/lvm-mysql_data
  Extending logical volume mysql_data to 111.56 GiB
  Logical volume mysql_data successfully resized


June 2001   July 2001   January 2003   May 2003   September 2003   October 2003   December 2003   January 2004   February 2004   March 2004   April 2004   May 2004   June 2004   July 2004   August 2004   September 2004   October 2004   November 2004   December 2004   January 2005   February 2005   March 2005   April 2005   May 2005   June 2005   July 2005   August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006   January 2007   February 2007   March 2007   April 2007   June 2007   July 2007   August 2007   September 2007   October 2007   November 2007   December 2007   January 2008   February 2008   March 2008   April 2008   July 2008   August 2008   September 2008   October 2008   November 2008   December 2008   January 2009   February 2009   March 2009   April 2009   May 2009   June 2009   July 2009   August 2009   September 2009   October 2009   November 2009   December 2009   January 2010   February 2010   March 2010   April 2010   May 2010   June 2010   July 2010   August 2010   September 2010   October 2010   November 2010   December 2010   January 2011   February 2011   March 2011   April 2011   May 2011   June 2011   July 2011   August 2011   September 2011   October 2011   November 2011   December 2011   January 2012   February 2012   March 2012   April 2012   May 2012   June 2012   July 2012   August 2012   October 2012   November 2012   December 2012   January 2013   February 2013   March 2013   April 2013   May 2013   June 2013   July 2013   September 2013   October 2013   January 2014   March 2014   April 2014   May 2014   July 2014  

This page is powered by Blogger. Isn't yours?