Things to do when you want to commit suicide!

We all in our lifespan sometime had a thinking to give up our life just because we do not want certain things to happen in our life. For example, when we are jobless for more than a year, cheated by lover/wife/husband, lonely, lost motive in our life, got bad marks or failed in exams etc.

The question is why this feeling generated in our mind? Do we really want to quit our life? Don't we ever thought that life is beautiful? or We had dreams to achieve something?

Answer is we thought that sometime!

Just because we are frustrated with our current or past situation we want to end it now. So if we stop believing that nothing 'll change than our life will change. 

If you reading this means you are frustrated with life or looking for knowledge about life. This is a good sign for your life. Yes, it is !

It means you know your present situation and looking for solutions.

So without making more talks just do following things when you thought about committing suicide:
1. Stop yourself for 15 mins & drink a lot of water. Try to say lets have life for 15 mins more as positive.
2. Say life is beautiful and I am strong to face bad and reach to good.
3. See yourself in a mirror and say I am born to succeed. I have my own fair image!
4. Say everything that  happens to me will make me more strong & intelligent to handle bad things.
5. Now just look around yourself and see you have a lot of things that you can use to make your living. Imagine about starving people at road having no access to internet, money, food. Go out try to help them. 
6. Write your like, dislikes, strength & weakness in a paper & paste it in your dairy. Try to remove you weakness and erase if you can overcome after few days. Think of how you can use your likes & strength and do it in your life.
7. Feel the real happiness about life. Sometimes its too bad but it is what make it beautiful
8. Change your current situation or life style. If possible travel to some other place.

LIFE IS BEAUTIFUL! 

MySQL Connection

MySQL Connection using mysql binary:

You can establish MySQL database using mysql binary at command prompt.

Example:

Here is a simple example to connect to MySQL server from command prompt:
[root@host]# mysql -u root -p
Enter password:******
This will give you mysql> command prompt where you will be able to execute any SQL command. Following is the result of above command:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2854760 to server version: 5.0.9

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
In above example, we have used root as a user but you can use any other user. Any user will be able to perform all the SQL operations, which are allowed to that user.
You can disconnect from MySQL database any time using exit command at mysql> prompt.
mysql> exit
Bye

MySQL Connection using PHP Script:

PHP provides mysql_connect() function to open a database connection. This function takes five parameters and returns a MySQL link identifier on success or FALSE on failure.

Syntax:

connection mysql_connect(server,user,passwd,new_link,client_flag);

Parameter
Description
server
Optional – The host name running database server. If not specified, then default value is localhost:3036.
user
Optional – The username accessing the database. If not specified, then default is the name of the user that owns the server process.
passwd
Optional – The password of the user accessing the database. If not specified, then default is an empty password.
new_link
Optional – If a second call is made to mysql_connect() with the same arguments, no new connection will be established; instead, the identifier of the already opened connection will be returned.
client_flags
Optional – A combination of the following constants:
  • MYSQL_CLIENT_SSL – Use SSL encryption
  • MYSQL_CLIENT_COMPRESS – Use compression protocol
  • MYSQL_CLIENT_IGNORE_SPACE – Allow space after function names
  • MYSQL_CLIENT_INTERACTIVE – Allow interactive timeout seconds of inactivity before closing the connection
You can disconnect from MySQL database anytime using another PHP functionmysql_close(). This function takes a single parameter, which is a connection returned by mysql_connect() function.


Syntax:

bool mysql_close ( resource $link_identifier );
If a resource is not specified then last opened database is closed. This function returns true if it closes connection successfully otherwise it returns false.

Example:

Try out the following example to connect to a MySQL server:
<html>
<head>
<title>Connecting MySQL Server</title>
</head>
<body>
<?php
   $dbhost = 'localhost:3036';
   $dbuser = 'guest';
   $dbpass = 'guest123';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   if(! $conn )
   {
     die('Could not connect: ' . mysql_error());
   }
   echo 'Connected successfully';
   mysql_close($conn);
?>
</body>
</html>

Install Specific Version of MongoDB in Ubuntu

To Install specific version of MongoDB, We have to pin particular version of MongoDB. In the section we will update you how to install MongoDB v2.6.
1. Import the public key used by package management system.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
2. Create a list file for for MongoDB.
Create the /etc/apt/sources.list.d/mongodb.list list file using the following command:
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
3. Reload local database package.
sudo apt-get update
4. Pin specific version of MongoDB from the command given below.
sudo apt-get install -y mongodb-org=2.6.10 mongodb-org-server=2.6.10 mongodb-org-shell=2.6.10 mongodb-org-mongos=2.6.10 mongodb-org-tools=2.6.10
5. You have successfully installed MongoDB in your Ubuntu machine.
6. Now, Start the services of MongoDB from the command given below.
sudo service mongod start
Thanks for going through the documentation, hope you like our posts.

Migration of MySql Tables to Cassandra table

In this section, we will let you know how to migrate MySql table to Cassandra table.
1. Create database in MySql.
create database mysql_to_cassandra ;
2. Create table in MySql.
CREATE TABLE `history` (  `id` varchar(30) DEFAULT NULL,  `city` varchar(30) DEFAULT NULL,  `province` varchar(30) DEFAULT NULL,  `country` varchar(30) DEFAULT NULL,  `zipCode` varchar(30) DEFAULT NULL,  `type` varchar(30) DEFAULT NULL,  `hasAirCondition` varchar(10) DEFAULT NULL,  `hasGarden` varchar(10) DEFAULT NULL,  `hasPool` varchar(10) DEFAULT NULL,  `isCloseToBeach` varchar(10) DEFAULT NULL,  `dailyPrice` float DEFAULT NULL,  `currency` varchar(10) DEFAULT NULL,  `roomsNumber` int(10) DEFAULT NULL,  `created_at` datetime DEFAULT NULL ) ; 
3. Insert one record in the table history;
insert into history( id ,  city ,  province ,  country ,  zipCode ,  type ,  hasAirCondition ,  hasGarden ,  hasPool ,  isCloseToBeach ,  dailyPrice  ,  currency ,  roomsNumber ,  created_at) values ("zZC8U",  "Mumbai", "province2", "india", "122001M", "studio", true, false, true, true, 14.92, "INR", 231, "2016-06-08 00:23:54" ); 
4. Take backup in csv in MySql console as given below.
select id, city, country, created_at, currency, dailyPrice, hasaircondition, hasgarden, isclosetobeach, province, type, zipcode from history INTO OUTFILE '/tmp/cql_cass.csv' FIELDS ENCLOSED BY '"' TERMINATED BY ',' ESCAPED BY '"' LINES TERMINATED BY '\r\n' ; 
5. Now you have backup of mysql table, and then we will create KEYSPACE(acronym of database) & table in Cassandra and import to this table.
6. Login to cqlsh console and create KeySpace as given below.
CREATE KEYSPACE from_mysql WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}  AND durable_writes = true; 
7. Create table in this keyspace/
use from_mysql ; 
;
CREATE TABLE mysql_table_data (id text PRIMARY KEY, city_name text, country text, created_at timestamp, currency text, dailyprice float, hasaircondition boolean, hasgarden boolean, haspool boolean, isclosetobeach boolean, province text, roomsnumber int, type text, zipcode text) ; 
8. Inserting data from the backup, we have created from MySql.
copy mysql_table_data (id, city, country, created_at, currency, dailyPrice, hasaircondition, hasgarden, isclosetobeach, province, type, zipcode ) from '/tmp/cql_cass.csv' ; 
9, You can verify the data using the command given below.
select * from mysql_table_data ; 

How To change data type in MongoDB

In this article, I will let you know how to change data type from String to Integer and vice versa.
String to Integer data type conversion
1. First check is there any data in string for which you want to convert the data to Integer 64, It’s NumberLong() data type in MongoDB.
db.collection_name.find({"field_name": {$exists: true, $type : 2}  } )
If count is more than 0 then it means data is in String for the specific field_name.
2. Conversion to Integer from String
db.collection_name.find({"field_name": {$exists: true, $type : 2}  }).forEach( function (x) { x.field_name = new NumberLong(x.field_name); db.collection_name.save(x); });
Above java script function will convert String type data to Integer 64.
3. If field has empty string then you can convert that docs to NumberLong(0) as per given below statement.
db.collection_name.find({"field_name": {$eq = "", $type : 2}  }).forEach( function (x) { x.field_name = new NumberLong(0); db.collection_name.save(x); });
Integer data type to String conversion
1. Check whether any docs are there or not from the given below query.
db.collection_name.find({"field_name": {$exists: true, $type : 18}  } )
If Count is more than 0 then follow the next steps.
2. Conversion of Integer data to String.
db.collection_name.find({"field_name": {$exists: true, $type : 18}  }).forEach( function (x) { x.field_name = "" + x.field_name; db.collection_name.save(x); });
Thanks for the going through the article

MySql Master Slave Replication Setup

In this article, I will let you know how to set up Master-Slave Replication in MySql.
For this you should have two IPs, one for Master server and another one for Slave server.
Consider we have the below two IPs for Master & Slave server.
10.10.19.20 – Master Server
10.10.19.22 – Slave Server
1. Install MySql in both the Master & Slave Server. You can visit the link given below and download the MySql as per your OS.
http://dev.mysql.com/doc/refman/5.7/en/installing.html
Step 1.
Configure the Master Server :-
Open the file vim /etc/mysql/my.cnf
Change the bind address as
bind-address = 10.10.19.20
Now change the Server-id, It should be unique and does not match with any server-id within the Replication group. I am giving the server-id 1, just for ease.
server-id=1
Now enable the logging of log_bin, our slave servers will copy the data from this bin file. Uncomment this line in /etc/mysql/my.cnf as given below.
log_bin = /var/log/mysql/mysql-bin.log 
Finally, we have to decide which database we want to replicate to slave server, mention the database name.
binlog_do_db = db_name 
Restart the MySql.
sudo service mysql restart
Now, we will log-in to mysql shell to give access to slave-user to replicate data from Master server.
mysql -uroot -p 
To give access to the slave-user, we will grant permission.
GRANT REPLICATION SLAVE ON *.* TO 'slave-user'@'%' IDENTIFIED BY 'password'; 
FLUSH PRIVILEGES; 
Switch to database which you want to replicate.
USE db_name 
FLUSH TABLES WITH READ LOCK; 
SHOW MASTER STATUS; 
Result will look this.
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      110 | db_name      |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec) 
Above given is the position from where the Slave server start replication from Master.
Take the database backup using mysqldump
mysqldump -uroot -p db_name --master-data --single-transaction > db_name.sql 
Now Unlock the database and exit.
UNLOCK TABLES; 
QUIT; 
We are done with Master-Server configuration.
Step 2.
Import the database, which we have previously taken at Master-Server to Slave-Server.
mysql -u root -p db_name < /path/to/db_name.sql 
Configure the Slave-Server( Ip- 10.10.19.22) :-
Open the file /etc/mysql/my.cnf
vi /etc/mysql/my.cnf 
Change the server-id, log_bin, relay-log, binlog_do_db as given below.
server-id = 2 
relay-log = /var/log/mysql/mysql-relay-bin.log 
log_bin = /var/log/mysql/mysql-bin.log 
bin_log_do_db = db_name 
Restart the mysql.
sudo service mysql restart
Log-in to MySql shell in Slave-server and enable replication.
mysql -u root -p 
CHANGE MASTER TO MASTER_HOST='10.10.19.20',MASTER_USER='slave-user', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=  117; 
Above statement do following things.
1. It update the current server as slave of Master-Server.
2. It gives to server the correct login credentials.
3. It inform the Slave-Server replication should start from where; the master log file and log position come from the numbers we note down previously.
Start the slave server from the MySql shell.
START SLAVE; 
You can see the Slave-Server status from MySql shell.
SHOW SLAVE STATUS; 
If getting any issue while connecting start the slave with a command to skip over it.
SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; SLAVE START; 
Your MySql Master-Slave Replication is ready to use.

Inner Join in MySQL

In this Article, I will let you know about Inner Join in MySQL.
Inner Join :-
Inner Join also known as Equi Join, Normal Join, it shows the only the result of two or more tables as condition satisfied.
Let’s say we have two tables EMPLOYEE and DEPARTMENT
Below is the data in both the tables.
mysql> SELECT * FROM EMPLOYEE ;
+------+------+-------------+--------+-----------+--------------------+---------+
| id   | EID  | EMP_NAME    | DEP_ID | Phone     | Email              | City    |
+------+------+-------------+--------+-----------+--------------------+---------+
|    1 | CS01 | RAM         | D01    | 96465646  | abcd1@gmail.com    | Delhi   |
|    2 | CS02 | Mohan       | D02    | 96423246  | abc1d@gmail.com    | Gurgaon |
|    3 | CS03 | Mohi        | D01    | 96464546  | a1bcd@gmail.com    | Noida   |
|    4 | CS04 | Mohit       | D02    | 9611116   | cd@gmail.com       | Delhi   |
|    5 | CS05 | Sanjay      | NULL   | 92222246  | a@gmail.com        | Gurgaon |
|    6 | CS06 | RAJ         | D02    | 963333    | xqwe@gmail.com     | Noida   |
|    7 | CS07 | InderJeet   | D01    | 94444446  | 43abcd@gmail.com   | Gurgaon |
|    8 | CS08 | Rohit sethy | D03    | 66665646  | 565abcd@gmail.com  | Delhi   |
|    9 | CS09 | Arindam     | NULL   | 98888846  | abcd343@gmail.com  | Delhi   |
|   10 | CS10 | Vijay       | D01    | 6677746   | ab3333cd@gmail.com | Delhi   |
|   11 | CS11 | Prashant    | NULL   | 9666666   | abcd232@gmail.com  | Noida   |
|   12 | CS12 | Vikram      | D01    | 98888886  | ab2c3d5@gmail.com  | Delhi   |
|   13 | CS13 | Prateek     | D03    | 923232326 | ab2cd55@gmail.com  | Noida   |
+------+------+-------------+--------+-----------+--------------------+---------+
13 rows in set (0.00 sec)
mysql> SELECT * FROM DEPARTMENT ;
+------+--------+----------+
| id   | Dep_id | dep_name |
+------+--------+----------+
|    1 | D01    | Finance  |
|    2 | D02    | IT       |
|    3 | D03    | HR       |
|    4 | D04    | INFRA    |
+------+--------+----------+
4 rows in set (0.00 sec)
As per definition, Inner Join will print only those records which match the condition.
mysql> SELECT E.EID, E.EMP_NAME, D.DEP_NAME FROM EMPLOYEE E INNER JOIN DEPARTMENT D ON E.DEP_ID = D.DEP_ID ; 
+------+-------------+----------+
| EID  | EMP_name    | dep_name |
+------+-------------+----------+
| CS01 | RAM         | Finance  |
| CS02 | Mohan       | IT       |
| CS03 | Mohi        | Finance  |
| CS04 | Mohit       | IT       |
| CS06 | RAJ         | IT       |
| CS07 | InderJeet   | Finance  |
| CS08 | Rohit sethy | HR       |
| CS10 | Vijay       | Finance  |
| CS12 | Vikram      | Finance  |
| CS13 | Prateek     | HR       |
+------+-------------+----------+
10 rows in set (0.02 sec)
As we can see in above result, it shows the only result which match the condition. It doesn’t print the record where DEP_ID is NULL in Employee table i.e. DEP_ID does not matched.

Profiling in MongoDB

In this tutorial, I will let you know profiling in MongoDB, why is it so important, and when should we enable profiling.
Profiling in MongoDB
Profiling in MongoDB plays a big role in query optimization, find the queries which is not using indexing, it help us to identify the slow queries and fix them. We can make our application more responsive.
Profiling Level
There are three type of profiling level in MongoDB.
+------------+----------+-------------------------------------------------------------------------------------------------------------------------+
| Parameter  | Type |   Description                                                                                                           |
+------------+----------+-------------------------------------------------------------------------------------------------------------------------+
| level      | integer |   Specifies a profiling level, which is either 0 for no profiling, 1 for only slow operations, or 2 for all operations. |
| slowms     | integer |   Optional. Sets the threshold in milliseconds for the profile to consider a query or operation to be slow.             |
+------------+----------+-------------------------------------------------------------------------------------------------------------------------+
To check current profiling level in MongoDB
> db.getProfilingLevel()
0
db.getProfilingStatus()
{ "was" : 0, "slowms" : 100 }
In above, we can see profiling level as 0, which I have already explained different profiling level and their meaning.
To Set Profiling level 
To enable profiling use the below command in the database.
> use db_name
switched to db db_name
db.setProfilingLevel(1,200)
{ "was" : 0, "slowms" : 100, "ok" : 1 }
In above, I have setup profiling level to 1 and treating queries as slow which took more than 200ms.
We can also set profiling level to 2 and print all the queries whatever comes to MongoDB as:-
>db.setProfilingLevel(2)
Note:- Please be careful while enabling Profiling, as it impact the performance of Database, always disable the profiling once find the queries which you have to optimize or check.

CRUD Operations in MongoDB

Hello All, After a long time writing this article, many people asked about CRUD operations of MongoDB.
CRUD Operations- CRUD Operations stands for Creation, Reading, Update and Deletion of Database.
I am giving information regarding how can we create Database, Collection (aka table in RDBMS World), insert data in collection, then read the collection, will do some find query, then update and deletion of documents.
1. Creation Of Database & Collection
To create a database:-
$ mongo
MongoDB Enterprise > use demo;
switched to db demo
MongoDB Enterprise > db
demo
Now, if you see above, I have connected to mongo shell from Linux terminal, then use database name demo. Here if we don’t have database called “demo” then it will create a database named “demo“, else it will use demo database considering it’s an existing database.
To create a collection and insert some record:- 
If we think about RDBMS world, then creation of table means you have to define schema of table and record which you want to insert should be same as of data-type defined during table creation.
But MongoDB give us flexibility, we don’t have to bother about the schema of collection, you want to insert a document (record in RDBMS), just insert don’t have to define that collection and their data-type. Let’s look at below example.
MongoDB Enterprise > db.firstSession.insert({ "article" : "CRUD Operation", created_at : new Date(), author : "Mukesh Kumar", domain : "blogfordba.org" })
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > 
MongoDB Enterprise > db.firstSession.insert({ "article" : "Profiling in MongoDB", created_at : new Date(), author : "Mukesh Kumar", domain : "blogfordba.org" })
WriteResult({ "nInserted" : 1 })
I have inserted 1 record in collection “firstSession“. If we see above, I have not defined any schema, not data-type.
Let’s look for the document.
MongoDB Enterprise > db.firstSession.findOne()
{
 "_id" : ObjectId("57ab77ffcbfc6713771fd376"),
 "article" : "CRUD Operation",
 "created_at" : ISODate("2016-08-10T18:52:47.917Z"),
 "author" : "Mukesh Kumar",
 "domain" : "blogfordba.org"
}
We can see 1 document is there in the collection “firstSession“.
2. Read Operations in MongoDB:-
To read a collection or fetch document from a collection, we will use find query and then where condition like we use in RDBMS database.
MongoDB Enterprise > db.firstSession.find({ author : "Mukesh Kumar" }).pretty()
{
 "_id" : ObjectId("57ab77ffcbfc6713771fd376"),
 "article" : "CRUD Operation",
 "created_at" : ISODate("2016-08-10T18:52:47.917Z"),
 "author" : "Mukesh Kumar",
 "domain" : "blogfordba.org"
}
{
 "_id" : ObjectId("57ab7940cbfc6713771fd377"),
 "article" : "Profiling in MongoDB",
 "created_at" : ISODate("2016-08-10T18:58:08.187Z"),
 "author" : "Mukesh Kumar",
 "domain" : "blogfordba.org"
}
As we can see, I find an author where name is “Mukesh Kumar”, search in MongoDB is case sensitive, if you want to search in any case use regular expression. I will give another article on that.
3. Update in MongoDB:- 
We can update a document as given below-
db.firstSession.update({ author : "Mukesh Kumar" }, { $set : { domain : "www.blogfordba.org" } }, false, true )
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
Let me explain, how update of a document works-
I did search operation first( like where clause in RDBMS), and then setting(updating) domain to “www.blogfordba.org“. Next I have mentioned false and then true.
First false indicate, if search document not found in the collection, then don’t insert a record in the collection with domain : “www.blogfordba.org”. If it’s true then it will insert.
Second true indicate, if it’s true update all matching search documents, if it’s false it will update only 1 document.
4. Deletion in MongoDB:- 
We can delete a document, delete or drop complete collection or a database.
Delete a document:- 
To delete a document, you can choose you want to delete a document or many document.
MongoDB Enterprise > db.firstSession.remove({ author : "Mukesh Kumar" }, { justOne : true } )
WriteResult({ "nRemoved" : 1 })
Here in above, justOne is indicating, we want to remove only one document, if you want to remove all matching documents, then omit the justOne and by default it’s false.
MongoDB Enterprise > db.firstSession.remove({ author : "Mukesh Kumar" } )
WriteResult({ "nRemoved" : 2 })
Delete all documents from a collection:-
MongoDB Enterprise > db.firstSession.remove({ } )
WriteResult({ "nRemoved" : 2 }) 
To Drop a collection in MongoDB:-
MongoDB Enterprise > db.firstSession.drop()
true
To Drop Database in MongoDB:-
MongoDB Enterprise > db.dropDatabase()
{ "dropped" : "demo", "ok" : 1 }