Sunday, May 19, 2013

SQLite3 SQL Commands Explained with Examples

SQLite3 is very lightweight SQL database which focuses on simplicity more than anything else. This is a self-contained serverless database engine, which is very simple to install and use.
While most of the commands in the SQLite are similar to SQL commands of other datbases like MySQL and ORACLE, there are some SQLite SQL commands that are different.
This article explains all the basic SQL commands that you need to know to use the SQLite database effectively.
If you don’t have sqlite installed, execute “yum install sqlite” to install it. You can also install SQLite database from source to get the latest version.

1. Create a SQLite Database (and a Table)

First, let us understand how create a SQLite database with couple of tables, populate some data, and view those records.
The following example creates a database called company.db. This also creates an employee table with 3 columns (id, name and title), and a department table in the company.db database. We’ve purposefully missed the deptid column in the employee table. We’ll see how to add that later.
# sqlite3 company.db
sqlite> create table employee(empid integer,name varchar(20),title varchar(10));
sqlite> create table department(deptid integer,name varchar(20),location varchar(10));
sqlite> .quit
Note: To exit from the SQLite commandline “sqlite>” prompt, type “.quit” as shown above.
A SQLite database is nothing but a file that gets created under your current directory as shown below.

# ls -l company.db
-rw-r--r--. 1 root root 3072 Sep 19 11:21 company.db

2. Insert Records

The following example populates both employee and department table with some sample records.
You can execute all the insert statements from the sqlite command line, or you can add those commands into a file and execute the file as shown below.
First, create a insert-data.sql file as shown below.
# vi insert-data.sql
insert into employee values(101,'John Smith','CEO');
insert into employee values(102,'Raj Reddy','Sysadmin');
insert into employee values(103,'Jason Bourne','Developer');
insert into employee values(104,'Jane Smith','Sale Manager');
insert into employee values(105,'Rita Patel','DBA');

insert into department values(1,'Sales','Los Angeles');
insert into department values(2,'Technology','San Jose');
insert into department values(3,'Marketing','Los Angeles');
The following will execute all the commands from the insert-data.sql in the company.db database
# sqlite3 company.db < insert-data.sql

3. View Records

Once you’ve inserted the records, view it using select command as shown below.
# sqlite3 company.db
sqlite> select * from employee;
101|John Smith|CEO
102|Raj Reddy|Sysadmin
103|Jason Bourne|Developer
104|Jane Smith|Sale Manager
105|Rita Patel|DBA

sqlite> select * from department;
1|Sales|Los Angeles
2|Technology|San Jose
3|Marketing|Los Angeles

4. Rename a Table

The following example renames department table to deptusing the alter table command.
sqlite> alter table department rename to dept;

5. Add a Column to an Existing Table

The following examples adds deptid column to the existing employee table;
sqlite> alter table employee add column deptid integer;
Update the department id for the employees using update command as shown below.
update employee set deptid=3 where empid=101;
update employee set deptid=2 where empid=102;
update employee set deptid=2 where empid=103;
update employee set deptid=1 where empid=104;
update employee set deptid=2 where empid=105;
Verify that the deptid is updated properly in the employee table.
sqlite> select * from employee;
101|John Smith|CEO|3
102|Raj Reddy|Sysadmin|2
103|Jason Bourne|Developer|2
104|Jane Smith|Sale Manager|1
105|Rita Patel|DBA|2

6. View all Tables in a Database

Execute the following command to view all the tables in the current database. The folowing example shows that there are two tables in the current database.
sqlite> .tables
dept employee

7. Create an Index

The following example creates an unique index called empidx on the empid field of employee table.
sqlite> create unique index empidx on employee(empid);
Once an unique index is created, if you try to add another record with an empid that already exists, you’ll get an error as shown below.
sqlite> insert into employee values (101,'James Bond','Secret Agent',1);
Error: constraint failed

8. Create a Trigger

For this example, first add a datecolumn called “updatedon” on employee table.
sqlite> alter table employee add column updatedon date;
Next, create a file that has the trigger definition. The following trigger will update the “updatedon” date column with the current timestamp whenever you perform an update on this table.
# vi employee_update_trg.sql
create trigger employee_update_trg after update on employee
begin
update employee set updatedon = datetime('NOW') where rowid = new.rowid;
end;
Create the trigger on the company.db database as shown below.
# sqlite3 company.db < employee_update_trg.sql
Now anytime you update any record in the employee table, the “updatedon” date column will be updated with the current timestamp as shown below. The following example updates the “updatedon” timestamp for empid 104 through trigger.
# sqlite3 company.db
sqlite> update employee set title='Sales Manager' where empid=104;

sqlite> select * from employee;
101|John Smith|CEO|3|
102|Raj Reddy|Sysadmin|2|
103|Jason Bourne|Developer|2|
104|Jane Smith|Sales Manager|1|2012-09-15 18:29:28
105|Rita Patel|DBA|2|

9. Create a View

The following example creates a view called “empdept”, which combines fields from both employee and dept table.
sqlite> create view empdept as select empid, e.name, title, d.name, location from employee e, dept d where e.deptid = d.deptid;
Now you can execute select command on this view just like a regular table.
sqlite> select * from empdept;
101|John Smith|CEO|Marketing|Los Angeles
102|Raj Reddy|Sysadmin|Technology|San Jose
103|Jason Bourne|Developer|Technology|San Jose
104|Jane Smith|Sales Manager|Sales|Los Angeles
105|Rita Patel|DBA|Technology|San Jose
After creating a view, if you execute .tables, you’ll also see the view name along with the tables.
sqlite> .tables
dept empdept employee

10. SQLite Savepoint, Rollback, Commit

Currently dept table has the following 3 records.
sqlite> select * from dept;
1|Sales|Los Angeles
2|Technology|San Jose
3|Marketing|Los Angeles
Now, create a savepoint called “major”, and perform some transactions on the dept table. As you see below, we’ve added two records, deleted one record, after creating a savepoint called “major”.
sqlite> savepoint major;
sqlite> insert into dept values(4,'HR','Los Angeles');
sqlite> insert into dept values(5,'Finance','San Jose');
sqlite> delete from dept where deptid=1;
sqlite> select * from dept;
2|Technology|San Jose
3|Marketing|Los Angeles
4|HR|Los Angeles
5|Finance|San Jose
Now for some reason, if we don’t want the above transactions, we can rollback the changes to a particular savepoint. In this example, we are rolling back all the changes we’ve made after the “major” savepoint.
sqlite> rollback to savepoint major;
sqlite> select * from dept;
1|Sales|Los Angeles
2|Technology|San Jose
3|Marketing|Los Angeles
If you don’t want your savepoints anymore, you can erase it using release command.
sqlite> release savepoint major;

11. Additional Date Functions

By default, the date columns values displayed in UTC time. To display in the local time, use the datetime command on the date column as shown below.
sqlite> select empid,datetime(updatedon,'localtime') from employee;
104|2012-09-15 11:29:28
You can also use strftime to display the date column in various output.
sqlite> select empid,strftime('%d-%m-%Y %w %W',updatedon) from employee;
104|19-09-2012 3 38
The following are the possible modifers you can use in the strftime function.
  • %d day of month: 00
  • %f fractional seconds: SS.SSS
  • %H hour: 00-24
  • %j day of year: 001-366
  • %J Julian day number
  • %m month: 01-12
  • %M minute: 00-59
  • %s seconds since 1970-01-01
  • %S seconds: 00-59
  • %w day of week 0-6 with Sunday==0
  • %W week of year: 00-53
  • %Y year: 0000-9999
  • %% %

12. Dropping Objects

You can drop all the above created objects using the appropriate drop command as shown below.
Since we are dropping objects for testing purpose, copy the company.db to a test.db and try these commands on the test.db
# cp company.db test.db

# sqlite3 test.db
sqlite> .tables
dept empdept employee

sqlite> drop index empidx;
sqlite> drop trigger employee_update_trg;
sqlite> drop view empdept;
sqlite> drop table employee;
sqlite> drop table dept;
All the tables and views from the test.db are now deleted.
sqlite> .tables
sqlite>
Note: When you drop a table all the indexes and triggers for that table are also dropped.

13. Operators

The following are the possible operators you can use in SQL statements.
  • ||
  • * / %
  • + -
  • << >> & |
  • < >=
  • = == != <> IS IS NOT IN LIKE GLOB MATCH REGEXP
  • AND OR
For example:
sqlite> select * from employee where empid >= 102 and empid  select * from dept where location like 'Los%';
1|Sales|Los Angeles
3|Marketing|Los Angeles

14. Explain Query Plan

Execute “explain query plan”, to get information about the table that is getting used in a query or view. This is very helpful when you are debugging a complex query with multiple joins on several tables.
sqlite> explain query plan select * from empdept;
0|0|TABLE employee AS e
1|1|TABLE dept AS d
For a detailed trace, just execute “explain” followed by the query to get more performance data on the query. This is helpful for debugging purpose when the query is slow.
sqlite> explain select empid,strftime('%d-%m-%Y %w %W',updatedon) from employee;
0|Trace|0|0|0||00|
1|Goto|0|12|0||00|
2|OpenRead|0|2|0|4|00|
3|Rewind|0|10|0||00|
4|Column|0|0|1||00|
5|String8|0|3|0|%d-%m-%Y %w %W|00|
6|Column|0|3|4||00|
7|Function|1|3|2|strftime(-1)|02|
8|ResultRow|1|2|0||00|
9|Next|0|4|0||01|
10|Close|0|0|0||00|
11|Halt|0|0|0||00|
12|Transaction|0|0|0||00|
13|VerifyCookie|0|19|0||00|
14|TableLock|0|2|0|employee|00|
15|Goto|0|2|0||00|

15. Attach and Detach Database

When you have multiple database, you can use attach command to execute queries across database.
For example, if you have two database that has the same table name with different data, you can create a union query across the database to view the combined records as explained below.
In this example, we have two company database (company1.db and company2.db). From the sqlite prompt, attach both these database by giving alias as c1 and c2 as shown below.
# sqlite3
sqlite> attach database 'company1.db' as c1;
sqlite> attach database 'company2.db' as c2;
Execute “.database” command which will display all the attached databases.
sqlite> .database
seq name file
--- --------------- ------------------
0 main
2 c1 /root/company1.db
3 c2 /root/company2.db
Now, you can execute a union query across these databases to combine the results.
sqlite> select empid, name, title from c1.employee union select empid, name, title from c2.employee;
101|John Smith|CEO
102|Raj Reddy|Sysadmin
103|Jason Bourne|Developer
104|Jane Smith|Sales Manager
105|Rita Patel|DBA
201|James Bond|Secret Agent
202|Spider Man|Action Hero
After attaching a database, from the current sqlite session, if you want to detach it, use detach command as shown below.
sqlite> detach c1;

sqlite> .database
seq name file
--- --------------- -----------------
0 main
2 c2 /root/company2.db

Monday, April 29, 2013

RAID 1+0 Configuration in HP Servers

Let’s see what is RAID1+0 and how can we configure RAID1+0 in a HP Server. RAID 1+0 is mixture of RAID 0 (Stripping) and RAID 1 (Mirroring). It also delivers the benefits of both in a single system. As you can see in the picture, it stripes the mirrored drives.  Thus, it will have the security of mirroring all data on another set of drives while striping of each set of drives to increases the data transfer speed. RAID 1+0 is the perfect solution for the servers running databases and high availability applications which demands data read/write efficiency and high fault tolerance. RAID 1+0 requires minimum four drives.
Power on the server and wait for the Array Controller ROM utility to prompt you to press F8 to run RAID configuration.
Press F8 to enter the utility then you will get the Array controller utility to create logical drives. Select Create logical drive and press Enter
Press Enter to create the logical drive.

Press F8 to Save the configuration. This will create the RAID 0 Logical Drive.
Press Enter to continue. That will take you to the main menu again. It is good to verify the Logical drive we created. For that select View Logical Drive and press Enter

That will show you the logical drive you have just created. Since I have selected all eight Physical disks (146GB each disk) available, for this logical drive we have got 546 TB capacity. Since it will mirror you will get only the half size of the total capacity of the drives. You can select the Logical Drive and press  Enter to see the physical disk array(total number of disk used in this RAID 1+0 local drive.When you quit from the utility server will continue the boot process.

Setup a logon banner for ssh clients to see when they login

SSH (Secure Shell) is a protocol for creating a secure connection between two computers. The secure SSH connection provides authentication and encryption. In the SSH server, it is possible to configure logon or welcome banner with the use of the Banner directive in /etc/ssh/sshd_config file.The Banner directive is only available for ssh protocol version 2 and by default there are no banner configured.
Configutration
1.       Create a banner file. (e.g. /etc/banner) using vi editor.
# vi /etc/banner
**********************************
                                ******** WELCOME TO REDHAT *******
**********************************
  1. Configure the ssh configuration file  /etc/ssh/sshd_config file and add the line Banner /etc/banner  to it.
# vi /etc/ssh/sshd_config
Banner /etc/banner
  1. Restart sshd daemon for the changes to take effect
# service sshd restart
Example
If we are accessing server using ssh , message will appear as follows:
[root@mail ~]# ssh 10.30.32.206
**********************************
*********WELCOME TO REDHAT********
**********************************
root@10.30.32.206's password:

Sunday, April 28, 2013

Monitor user activity using sudo and sudosh2

Note: I used UBUNTU 12.04TLS 64Bit machine to test this.

1. Change the password of ALEXANDER and do not share with anyone.
2. Now download sudosh2-1.0.4.tgz and install as instructed below

# tar zxvf sudosh2-1.0.4.tgz
# cd sudosh2-1.0.4
# CFLAGS="-D_GNU_SOURCE" ./configure
# make
# sudo make install

3. Create four users SURESH,NARESH,YOGESH and NAGESH
 
# sudo adduser SURESH
# sudo adduser NARESH
# sudo adduser YOGESH
# sudo adduser NAGESH

4. Create one GROUP say DEVELOPERS


# sudo groupadd DEVELOPERS

5. Add users SURESH,NARESH,YOGESH and NAGESH to Secondary Group DEVELOPERS


#sudo usermod -a -G DEVELOPERS SURESH
#sudo usermod -a -G DEVELOPERS NARESH
#sudo usermod -a -G DEVELOPERS YOGESH
#sudo usermod -a -G DEVELOPERS NAGESH

6. Open /etc/sudoers file and make below changes


Defaults        env_reset
Defaults        syslog=auth
Defaults>root   !set_logname
Defaults        log_year, logfile=/var/log/sudo.log
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

Cmnd_Alias SUDOSH = /usr/local/bin/sudosh


root    ALL=(ALL:ALL) ALL


%DEVELOPERS
  ALL=(ALEXANDER) SUDOSH

%sudo   ALL=(ALL:ALL) ALL


This will enable the sudo log which can be found at /var/log/sudo.log

Allowing users /usr/local/bin/sudosh command only to use with ALEXANDER user.


Note: Be very careful with this file especially when you are in UBUNTU system. If you mess up with this file, there is no other option than rebooting the system and booting it in recovery mode to fix the file if you dont have policykit-1 installed.For safer side please install...
# sudo apt-get install policykit-1

7. That's it done! We are good to test this...
Login as SURESH or any one from DEVELOPERS group.
Once you get the prompt Enter..

# sudo -u ALEXANDER sudosh

will be asked for the SURESH passwd. provide it and you are in.

check with id command you should see ALEXANDER details.

# id  
uid=1000(ALEXANDER) gid=1000(ALEXANDER) groups=1000(ALEXANDER),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),111(lpadmin),112(sambashare)

You will find the logs at below location
Open a new session and login as ALEXANDER or a user who belongs to sudo group.
# cd /var/log/sudosh
# sudo ls -ltrh
-rw------- 1 ALEXANDER ALEXANDER   81 Oct 12 10:33 SURESH-ALEXANDER-time-1350063234-YTvv0lB4sLtImfI3
-rw------- 1 ALEXANDER ALEXANDER  196 Oct 12 10:33 SURESH-ALEXANDER-script-1350063234-YTvv0lB4sLtImfI3

Now you got two options to see this file either you use

# tail -f SURESH-ALEXANDER-script-1350063234-YTvv0lB4sLtImfI3

you will get all details that to live if SURESH is still logged into ALEXANDER.
or use

# sudo sudosh-replay SURESH-ALEXANDER-script-1350063234-YTvv0lB4sLtImfI3 1 2

You will see Action Replay!

Httpd Status Codes

Informational   1xx
Successful      2xx
Redirection     3xx
Client Error    4xx
Server Error    5xx

100 Continue
101 Switching Protocols
102 Processing (WebDAV) (RFC 2518)
103 Checkpoint
122 Request-URI too long (Microsoft/IE7)
--------------------------------------------
200 OK
201 Created (+ etag)
202 Accepted
203 Non-Authoritative Information
204 No Content (no body)
205 Reset Content (reset view)
206 Partial Content (+ range header)
207 Multi-Status (WebDAV) (RFC 4918)
226 IM Used (RFC 3229)
--------------------------------------------
300 Multiple Choices
301 Moved Permanently
302 Found
303 See Other (since HTTP/1.1)
304 Not Modified
305 Use Proxy (since HTTP/1.1)
306 Switch Proxy (no longer used)
307 Temporary Redirect (since HTTP/1.1)
308 Resume Incomplete
--------------------------------------------
400 Bad Request
401 Unauthorized
402 Payment Required (future)
403 Forbidden
404 Not Found
405 Method Not Allowed
406 Not Acceptable
407 Proxy Authentication Required
408 Request Timeout
409 Conflict (with the resource)
410 Gone
411 Length Required
412 Precondition Failed
413 Request Entity Too Large
414 Request-URI Too Long
415 Unsupported Media Type
416 Requested Range Not Satisfiable
417 Expectation Failed
418 I'm a teapot (RFC 2324)
420 Enhance Your Calm (Twitter API)
422 Unprocessable Entity (WebDAV) (RFC 4918)
423 Locked (WebDAV) (RFC 4918)
424 Failed Dependency (WebDAV) (RFC 4918)
425 Unordered Collection (RFC 3648)
426 Upgrade Required (RFC 2817)
428 Precondition Required (RFC 2616 pending)
429 Too Many Requests (RFC 2616 pending)
431 Request Header Fields Too Large
444 No Response (Nginx)
449 Retry With (Microsoft)
450 Blocked by Windows Parental Controls (Microsoft)
499 Client Closed Request (Nginx)
--------------------------------------------
500 Internal Server Error
501 Not Implemented
502 Bad Gateway
503 Service Unavailable
504 Gateway Timeout
505 HTTP Version Not Supported
506 Variant Also Negotiates (RFC 2295)
507 Insufficient Storage (WebDAV)(RFC 4918)
509 Bandwidth Limit Exceeded (Apache)
510 Not Extended (RFC 2774)
511 Network Authentication Required  (RFC 2616 pending)
598 Network read timeout error (Informal convention) 
599 Network connect timeout error (Informal convention)

Friday, April 26, 2013

Send mail when cron fails in Linux server

$ crontab -e
At the top of the file, enter:
MAILTO=”support@urmail.com”

Setting Root login email alert on a Linux server

How to set up a root login alert

 vi /root/.bashrc
 echo ‘ALERT – Root Shell Access MY  SERVER on:’ `date` `who` | mail -s “Alert: Root Access from `who | cut -d”(” -f2 | cut -d”)” -f1`” email@gmail.com