Shantanu's Blog

Corporate Consultant

December 31, 2009

 

Conquer the Konqueror

KDE is nothing but another TV serial started by Star Channel.
Everything starts with "K" like "kusum", "Kaun banega Karodpati", "Saas bhi kabhi bahu thi" etc. My favorite is "KDE ka Konqueror" in which K. Kapoor can "fish" for files from another server and simply right click to Kopy.

Here is how:

1) Start Konqueror
# konqueror &

2) Use Ctrl + tab to start a new tab
3) Use fish protocol to connect to ssh sever.
fish://111.222.333.444
You can connect to ftp server using the ftp protocol.
ftp://ftp.saraswaticlasses.net

4) Now navigate to the folder and then right click on the file to copy, paste it anywhere.

Labels:


 

Backup Cron

A single line from for daily backups:
58 13 * * * /usr/bin/mysqldump -uroot -pPassWord --compact --databases db1 db2 db3 db4 |
zip > /home/develop/backup/central_mydt$(date +'\%d\%m\%y').zip
2>> /home/develop/backup/central_err.txt

After unzipping the file there will a file with the name -
You have to rename it to be more useful
mv \- somefile.txt

Labels:


December 29, 2009

 

Read binary files

mysqlbinlog is the standard command that can be used at the command prompt to read the binary contents.
But you can also use show binlog events to read the binary file as shown below:

mysql> system ls /var/log/mysql/ -lh
total 76M
-rw-rw---- 1 root root 15K Dec 26 14:07 mysql-bin.000001
-rw-rw---- 1 root root 575K Dec 26 14:07 mysql-bin.000002
-rw-rw---- 1 root root 254 Dec 26 14:07 mysql-bin.000003
-rw-rw---- 1 root root 54M Dec 26 22:00 mysql-bin.000004
-rw-rw---- 1 root root 1.9K Dec 27 15:43 mysql-bin.000005
-rw-rw---- 1 root root 117 Dec 27 16:15 mysql-bin.000006
-rw-rw---- 1 root root 22M Dec 29 19:40 mysql-bin.000007
-rw-rw---- 1 root root 224 Dec 28 11:31 mysql-bin.index
-rw-r----- 1 root mysql 4.5K Dec 29 19:43 mysqld.log
-rw-rw---- 1 root root 3.5K Dec 28 14:28 mysql-slow.log


mysql> show binlog events in 'mysql-bin.000007' from 4 limit 10;

| mysql-bin.000007 | 928 | Query | 99 | 407 | use `umr`; insert into master values("55063","A","JC","umrd","ok","P","Tambarbge","OnService","6163899","Naur","1974-11-04","Mr","NAUR","NGP","UMR","UMRD") |
| mysql-bin.000007 | 1335 | Xid | 99 | 1362 | COMMIT /* xid=488 */

Labels:


December 28, 2009

 

lsof - list open files

# List all open files.
lsof

# lists all the processes, which are using the file in some way.
lsof /path/to/file

# This will list all the files that are open by users rms and root.
lsof -u root

# Find all open files by program’s name.
lsof -c apache

# print all open files by all users who are not root.
lsof -u ^root

# List all open files by the process with PID.
lsof -p 1
lsof -p 450,980,333

# List all network connections.
lsof -i

# List all TCP network connections.
lsof -i tcp

# Find who’s using a port.
# The :25 option to -i makes lsof find processes using TCP or UDP port 25.
lsof -i :25

# to find who’s using a TCP port, use:
lsof -i tcp:80

# Find all network activity by user.
# Here the -a option combines -u and -i to produce listing of network file usage by user hacker.
lsof -a -u hacker -i

# List all NFS (Network File System) files.
lsof -N

# List all files associated with specific file descriptors.
# This lists all files that have been opened as file descriptor 2.
lsof -d 2

# txt for programs loaded in memory and executing:
lsof -d txt

# Output PIDs of processes using some resource.
lsof -t -i

# The -t option outputs only PIDs of processes. Used together with -i it outputs PIDs of all processes with network connections. It’s easy to kill all processes that use network:
kill -9 `lsof -t -i`

Labels:


 

InnoDB setting

If you are using InnoDB table types (who doesn't?) and if the database size is huge (more than 10 GB), you MUST configure the Innodb related settings in your my.cnf file correctly.

innodb_data_file_path=ibdata1:2000M:autoextend #(default: 10M)
#50-80% of memory is a safe bet. I set it to 12G on 16GB box
innodb_buffer_pool_size=12G #(default: 8 MB)
innodb_additional_mem_pool_size=100M
innodb_log_file_size=256M #(default: 5 MB)
innodb_log_buffer_size=8M #(default: 1 MB)
innodb_flush_log_at_trx_commit=2

Labels:


December 18, 2009

 

Finding PHP syntax errors

There is a very easy way to find PHP syntax errors.
If all your .php files are in /var/www/html then you can type the following command.

# find /var/www/html -name '*.php' -exec php -l {} \; | grep -v '^No syntax'

You can save the output to a text file by appending "> tofile.txt" at the end of the above command.
The output will look something like this...

Parse error: syntax error, unexpected '<' in ./reports/extra/rc7bkp2.php on line 265
Errors parsing ./reports/extra/rc7bkp2.php

Parse error: syntax error, unexpected $end in ./reports/extra/rc6bkp.php on line 525
Errors parsing ./reports/extra/rc6bkp.php

Parse error: syntax error, unexpected '}' in ./login/login.php on line 53
Errors parsing ./login/login.php

Parse error: syntax error, unexpected $end in ./commom/bkp/25_nov/Process1.php on line 93
Errors parsing ./commom/bkp/25_nov/Process1.php

_____

If you want to check if a file for e.g. reserve.php has any errors, use the following command....

$ tail -f /var/log/httpd/error_log | grep 'reserve.php'
[Fri Dec 18 16:46:30 2009] [error] [client 59.95.20.15] PHP Notice: Undefined index: ucd in /var/www/html/login.php on line 531, referer: http://111.222.333.444/reserve.php
[Fri Dec 18 16:46:33 2009] [error] [client 117.195.35.82] PHP Notice: A session had already been started - ignoring session_start() in /var/www/html/checkPass.php on line 2, referer: http://666.777.888.999/reserve.php

Labels:


December 16, 2009

 

Issues with SELinux firewall

You can turn on the firewall using SELinux. Use the following command to enable.
lokkit

But this will almost surely stop mysql from starting on the next boot if you have changed the datadir path. You will get an error something like this...

091216 23:51:41 mysqld started
091216 23:51:42 [Warning] Can't create test file /mysql/localhost.lower-test
091216 23:51:42 [Warning] Can't create test file /mysql/localhost.lower-test
/usr/libexec/mysqld: Can't change dir to '/mysql/' (Errcode: 13)
091216 23:51:42 [ERROR] Aborting

091216 23:51:42 [Note] /usr/libexec/mysqld: Shutdown complete

091216 23:51:42 mysqld ended

You can either disable it by using the same command again. If you want to use SELinux and still start mysql from a different location, use the following tip:

If you start 'system-config-securitylevel' (RHEL/CentOS) in a graphical environment and click on the SELinux tab, then right down at the bottom of the 'Modify SELinux Policy' section there is a menu item 'SELinux Service Protection'. Expand this and one of the items is 'Disable SELinux protection for mysqld daemon'. Check the box next to this and click 'OK'.

/usr/sbin/setenforce 0
The above will disable selinux.
But you need to either specify the appropriate setenforce parameter in /etc/sysconfig/selinux or pass the parameter selinux=0 to the kernel, either in /etc/grub.conf or at boot time.

Labels:


December 13, 2009

 

Using PHP to avoid filesort

The "order by" clause has one column in ascending order and the other descending.
Such queries are very slow and hence PHP needs to be used.

mysql> explain SELECT * FROM ABCD WHERE a=1 AND b=1 ORDER BY c DESC, d ASC\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: ABCD
type: ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 8
ref: const,const
rows: 2
Extra: Using where; Using index; Using filesort
1 row in set (0.00 sec)

So, we changed 1 query into 11 queries (10 parents per page) to make the page load happen faster, by getting rid of the filesort.

FOREACH($C_parent as $i => $c_id) {
$C_parent[$i] = SELECT SQL_CALC_FOUND_ROWS * FROM ABCD WHERE A=? AND B=? AND C=$c_id ORDER BY D ASC LIMIT 1;
}

Calculating the running total is another example where we need to use PHP as writing complex query is time consuming as well as very slow.

Labels: ,


 

PHP at command prompt

1) Run php page with the ini settings
# php -c /etc/php.ini /var/www/html/TablesList.php
Warning: mysql_pconnect(): Access denied for user 'sink'@'localhost' (using password: YES) in /var/www/html/dbconnect.php on line 6
Error Occurred while communicating with the database!

2) Check Syntax error
# php -l /var/www/html/TablesList.php
No syntax errors detected in /var/www/html/TablesList.php

3) Strip the source code of comments and whitespace
# php –w example.php
Can be useful if you need to reduce the file size.

4) Run php code from the command line
# php -r "$foo = 'hello!'; $foo = strtoupper($foo); echo $foo;"
Directly run php code from the command line without using the php start and end tags.

Labels: ,


December 06, 2009

 

UNIX case study - 22

It is possible to have a two dimensional array in the shell script. I just have to have two nested for - do, done loops. It was really simple but it took me a while to figure it out.

#!/bin/sh
set -x

for i in "yahoo.com" "facebook.com" "google.com" "reddit.com" "cnet.com" "bbc.co.uk"
do
for j in "4.2.2.2" "8.8.8.8" "208.67.222.222"
do
echo $j $i `dig @$j $i | grep Query | awk -F ":" '{print $2}'`
done
done

Labels: ,


Archives

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   August 2014   September 2014   October 2014   November 2014   December 2014   January 2015   February 2015   March 2015   April 2015   May 2015   June 2015   July 2015   August 2015   September 2015   January 2016   February 2016   March 2016   April 2016   May 2016   June 2016   July 2016   August 2016   September 2016   October 2016   November 2016   December 2016   January 2017   February 2017   April 2017   May 2017   June 2017   July 2017   August 2017   September 2017  

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