Showing posts with label Ubuntu. Show all posts
Showing posts with label Ubuntu. Show all posts

Tuesday, September 23, 2014

NodeJs mongoDB : With sample application

Hi,

NodeJs connectivity with the MongoDB Database.
Here, i am assuming that you have NodeJS, NPM and socket.io installed on your machine. If not kindly follow the below URL to install the above tools:
http://tarunlinux.blogspot.in/2014/09/nodejs-basics.html

Lets start to create sample appication on NodeJs with mongoDB connectivity.

Step 1: Install mongoDB on your machine... Please follow the below link to setup mongoDB..
http://tarunlinux.blogspot.in/2014/09/mongodb-basics.html

Step 2: Install NodeJS mongoDB dependency with the below command:

1
# sudo npm install mongodb



It will install mongodb module in your application.

Step 3: Create server.js file for the nodejs:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
var mongo = require('mongodb').MongoClient;
var client = require('socket.io').listen(8080).sockets;  //Load socket.io module

mongo.connect('mongodb://localhost/tarundb', function(err, db){
    client.on('connection', function(socket){
        var col = db.collection('employee'); //connect with collection of tarundb

        //Insert data into mongodb
        col.insert({name: 'Tarun', age:26}, function() {
            console.log("Inserted");
        });
        
        //emit all the records
        col.find().limit(100).toArray(function(err, res){
            if(err) throw err;
            
            socket.emit('all-data', res);
        });
    });
});


Step 4: Create Index.html file for the frontend:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<html>
    <head>
        <title> NodeJs MongoDB APP </title>
    </head>
    <body>
        <div class="data-set"></div>
        <script src="http://localhost:8080/socket.io/socket.io.js" type="text/javascript"></script>
        <script src="http://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
        <script>
        (function(){
            try{
                var socket = io.connect('http://localhost:8080');
            } catch(e){
                console.log(e);
            }
            
            if(socket !== undefined) {
                socket.on('all-data', function(data){
                    for(var i=0; i < data.length; i++) {
                        $(".data-set").append('<li>'+ data[i].name +'</li>');
                    }
                });
            }
        })();
        </script>
    </body>
</html>


Step 5: Now you have to run server.js file
# nodejs server.js

Then open your browser, with url: http://localhost:8080/
Where you will see the mongoDB connectivity and their data.

I hope it will help you for NodeJs Connectivity with MongoDB.. If you have any query, please put your comments, i highly appreciate that ...

Bye..

Monday, September 22, 2014

NodeJS MySql Connectivity : With Sample Application

Hi,

Here, i will share with you on NodeJS sample application with MySql Database connectivity.

I am assuming that  you already installed the NodeJS, NPM and Socket.io on your machine.
If not, you can follow below link to install the above requirements
http://tarunlinux.blogspot.in/2014/09/nodejs-basics.html

Here, I will show you in step-wise :

Step 1: Install the mysql module... here also i am assuming that mysql is already installed in your machine.
Now install mysql module for the NodeJs dependency.
# sudo npm install mysql

That will install the MySql dependency in your machine.

Step 2: Now Create the server.js file for the nodejs:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var mysql = require('mysql'); // load mysql module
var client = require('socket.io').listen(8080).sockets;  //Load socket.io module

//Database connectivity
var db = mysql.createConnection({
    host: 'localhost'
    , user: 'root'
    , password: 'root'
    , database: 'zfskel'
    , port: '3306'    
});

db.connect(function(err){
    console.log(err);
});

//Create connection with the client and emit data on socket
client.on('connection', function(socket){
    db.query('SELECT * FROM employee')
      .on('result', function(data){

        socket.emit('all-data', [data]);
    });
});


Step 3: Create Index.html file for the front-end:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<html>
    <head>
        <title> NodeJs Mysql APP </title>
        <link rel="stylesheet" href="">
    </head>
    <body>
        <div class="data-set"></div>
    <script src="http://localhost:8080/socket.io/socket.io.js" type="text/javascript"></script>
    <script src="http://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
    <script>
    (function(){
        try{
            var socket = io.connect('http://localhost:8080');
        } catch(e){
            console.log(e);
        }
        
        if(socket !== undefined) {
            socket.on('all-data', function(data){
                for(var i=0; i < data.length; i++) {
                    $(".data-set").append('<li>'+ data[i].name +'</li>');
                }
            });
        }
    })();
    </script>
    </body>
</html>



Now your have 2 files i.e. server.js and index.html file.

Step 4: Now you need the sql file for the database connectivity:

1
2
3
4
5
6
7
CREATE TABLE `employee` (
  `id` int(11) NOT NULL,
  `name` varchar(45) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `employee` VALUES (1,'tarun',26),(2,'varun',34);


Step 5: Now you have to run only server.js file form command line:
# nodejs server.js

Then open your browser and run the below url:
http://localhost:8080/

Here, you will see the MySql data on your browser...

I hope it will help you to understand NodeJs and its connectivity with the MySql connectivity.

Bye.. :)

NodeJS Basics

Hi,

I like to share some information on NodeJs.

What is NodeJs.
NodeJs is a cross-platform runtime environment for server-side and networking applications.
Node.js applications are written in JavaScript, and can be run within the Node.js runtime on OS X, Microsoft Windows and Linux with no changes.
Node.js internally uses the Google V8 JavaScript engine to execute code, and a large percentage of the basic modules are written in JavaScript. 

In this section, I will tell you on the below points:
1. How to install NodeJs
2. How to install NPM
3. How to install Socket.io
4. How to run nodejs from CLI

1) Installation:
I install the NodeJs on Ubuntu 14.04 O.S with the below command:

# sudo apt-get install nodejs

2) Install NPM(Node Package Manager):

# sudo apt-get install npm

Here, NPM basically a package manager.
NPM is helpful to maintain the NodeJS  dependency in your development.

3) Install Socket.io
Basically it is useful to maintain the socket connection with your frontend and backend. Or you can say..
Its provide you a real time communication between your node.js server and clients.
Installation command:
# sudo npm install socket.io

4) Run the NodeJs:
Applications are executed from the command line with the command:

# nodejs <application name>.js

In my next article, i will demonstrate your how to use it in real time communication.

Thursday, September 18, 2014

mongoDB Basics

Hi,

From last few days i started on mongoDB. It looks interesting as i never work on noSQL type database. Before that i worked on SQL, MySQL and Oracle Database.

mongoDB is the different one from the above DBs. Here, i like to share with you a quick intro on mongoDB.

mongoDB: is a cross-platform document-oriented database.
Classified as a NoSQL database, MongoDB eschews the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas (MongoDB calls the format BSON)
making the integration of data in certain types of applications easier and faster.

I use the mongoDB in one of my project... I installed this on my machine Whose OS is Ubuntu 14.04 version.

Installation
# apt-get install mongodb
It will install the client and server and other library associated to it.

Open the port in mongod.conf,
# sudo vi /etc/mongodb.conf
Just uncomment the below line in conf file
port = 27017

Start of mongoDB
# sudo service mongodb start

Its a pretty simple to setup on Ubuntu... :)

mongo is a part of the standard mongoDB distribution and provides a full JavaScript environment with complete access to the JavaScript language and all standard functions as well as a full database interface for mongoDB.

Now Start the mongo
# mongo
It will take you on mongo prompt....

Some basic commands for mongoDB

> db  // show current db

> show dbs //show all db with their size

> use db-name  //shitch to your selected db

> help  //show help for the mongodb

> show collections //show the collections used in your selected db

> db.collection-name.find()  // give you all records in your collection

> db.collection-name.insert({name:'Tarun', message:'Welcome'})  //insert record into the collection

> db.collection-name.find({name:'Tarun'})  //give the particular record from your collection
Here, important point is, this is case-sensitive.
if your search as
> db.collection-name.find({name:'tarun'})  // it will not show records whose name as Tarun  

> db.collection-name.findOne()  //to get the one document from the collection
 
> db.collection-name.find().limit(3)  // to get 3 document from all document


Wednesday, September 3, 2014

PHPUnit Pear Install In Ubuntu

Hi,

To install the PHPUnit in ubuntu, Please follow the below instruction:

If your system does not have Pear installed then:

# sudo apt-get install php-pear

# sudo pear channel-update pear.php.net

Now, You have to install the PHPUnit, with the help of pear easily:


# sudo pear channel-discover pear.phpunit.de

# sudo pear install -a phpunit/PHPUnit

If you want the code-coverage analysis of your application then you have to install the PHP extension i.e. Xdebug , That will help you create the html formatted report where you can see:
i) Code coverage percentage
ii) Which test case run on your application code

Command, to install Xdebug:
# sudo apt-get install php5-xdebug

NOTE: The above process of installation via PEAR would be outdated on Dec, 2014.
Please try to use Composer instead of PEAR





Friday, February 21, 2014

Hide User Names on the Login Screen in Ubuntu

Hi,

This is for to clean the login screen i.e. multiple users listing should not show on login screen.

It can be accomplish with some changes in LightDM desktop manager configuration.

It brings the new layer of security that any intruder should use right user account to login into your machine.

Steps to make changes it in LightDM desktop manager.

Step 1: Open lightdm.conf file i.e.
# vi /etc/lightdm/lightdm.conf

Step 2: Add below 2 lines in the lightdm.conf file

greeter-hide-users=true
allow-guest=false

Step 3: Above line will hide the user listing on login screen.

I hope above steps will helpful to enable the clear Login screen.


 


Ubuntu 12.04 Grub to boot into single user mode

Hi,

Steps to boot in single user mode in Ubuntu 12.04:

Step 1: When you start your system, press "shift" key continuously to get the grub loader screen.

Step 2: In Grub 2 menu, select the menu with Linux 3.2.0.23-generic-pae highlighted.

Step3: Press 'e' to edit the grub2 menu.

Step 4:  Move the cursor to the line that starts with "linux /boot/vmlinuz-3.2.0-23-generice-pae".

Step 5: Change the content "ro quiet spalsh $vt_handoff" To "rw init=/bin/bash".

Step 6: Press "Ctrl+x" to continue boot to in single user mode.

Step 7: Now you will get prompt of the root user.

Step 8: Change root user password,
# passwd root

Step 9: Now sync and reboot the system i.e.
# sync
# reboot -f

I hope, above steps are helpful to change the root user password in single user mode.

Thursday, October 17, 2013

GIT : Basic Command

1. How to color the Git console in Ubuntu?

# git config --global color.ui auto

The color.ui is a meta configuration that includes all the various color.* configurations available with git commands. This is explained in-depth in git help config.


color.ui: This variable determines the default value for variables such as color.diff and color.grep that control the use of color per command family. Its scope will expand as more commands learn configuration to set a default for the --color option. Set it to always if you want all output not intended for machine consumption to use color, to true or auto if you want such output to use color when written to the terminal, or to false or never if you prefer git commands not to use color unless enabled explicitly with some other configuration or the --color option.


2. How to keep password in memory?
# git config --global credential.helper cache
which tells git to keep your password cached in memory for (by default) 15 mins
# git config --global credential.helper "cache --timeout=3600"
which tells git to keep your password cached in memory for 3600 seconds

3. Get diff in file
# git diff
will return the file differences

4. How to resolving file conflicting after Git Pull?
Step 1> Identify which files are in conflict (Git should tell you this)
Step 2> Open each file and examine the diffs; Git demarcates them. Hopefully it will be obvious which version of each block to keep. You may need to discuss it with fellow developers who committed the code.
Step 3> Once you've resolved the conflict in a file, then apply below command in console
# git add file-name
Step 4> Then commit the conflict
# git commit -m "Conflicts Resolved"

5. How to add new file on Git?
# git add
# git commit -m "new file added"


6. How to check the file status in your repository?
# git status

7. How to push you committed file on the remote server?
# git push origin master

8. How to get the Git Update from remote server?
# git pull origin master

9. How to increase Git performance?
# git gc
will give significant speed on your local repository.
Basically, git-gc : it is for cleanup unnecessary files and optimize the local repository.

10. How to get the Git Log?
# git log

11. How to get log on specific file?
# git log "file-name"
ex:
# git log test.txt

12. How to get file from Git of specific version?
# git show commit-no:file-name

ex:
# git show abcdefghijklmnop:test.txt

13. How to know file list from Git of specific version?
# git show commit-no --name-only

ex:
# git show 0ba1a6177178f77cd711bfe1db43fac80f70f3e2 --name-only

14. How to use GUI visualize for Git log?
Please install gitk tool in your OS
# sudo apt-get install gitk
It will install in your OS, after that you are able to view the git log in GUI, where everybody easy to identify the following:
  • Commit with the name, and respective committed files
  • file change in every commit
  • Text search in committed file
  • beautiful file comparison
  • file navigation  
 Run below command to run GUI interface:
# gitk

15. How to remove file/folder from GIT
if you want to remove file, then

# git rm file-name
if you want to remove folder, then
# git rm folder-name -r

# git commit -m "your message"
#git push origin master

16. How to change message after GIT commit
# git commit --amend -m "New commit message"