Friday, November 4, 2016

How do I display shell command history with date and time under UNIX or Linux operating systems?

If the HISTTIMEFORMAT is set, the time stamp information associated with each history entry is written to the history file, marked with the history comment character. Defining the environment variable as follows
root@linuxforfreshers.com:~$ vim .bash_profile

$ HISTTIMEFORMAT="%d/%m/%y %T "
:wq
Or
root@linuxforfreshers.com:~$  echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile
root@linuxforfreshers.com:~$ source .bash_profile
Where,
%d – Day
%m – Month
%y – Year
%T – Time
root@linuxforfreshers.com:~$  history
Sample outputs:

986  10/03/1104:31:36 memcached-tool  10.10.28.22:11211 stats
  987  10/03/1104:31:36 w
  988  10/03/1104:31:37 iostat
  989  10/03/1104:31:37 top
  990  10/03/1104:31:37 at
  991  10/03/1104:31:38 atop
  992  10/03/1104:31:40 collectl
  993  10/03/1104:31:41 grep CPU /proc/cpuinfo
  994  10/03/1104:31:45 vmstat 3 100
  995  10/03/1104:31:55 sar -W -f /var/log/sa/sa12

References:
For more info type the following commands:
man bash
help history
man 3 strftime



Thursday, November 3, 2016

How to check which port number mysql server is running on linux ?

Method 1:

To find a listener on a port, do this:

root@linuxforfreshers.com:~$netstat -tln | grep 3306
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN     

Or

root@linuxforfreshers.com:~$netstat -tlpn | grep mysql
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      870/mysqld


Method 2:

root@linuxforfreshers.com:~$grep port /etc/mysql/my.cnf
# One can use all long options that the program supports.
# It has been reported that passwords should be enclosed with ticks/quotes
port                 = 3306
port                 = 3306

Method 3:

Using Mysql Query
root@linuxforfreshers.com:~$  mysql –u username –p password

mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |


+---------------+-------+

how to check mysql server uptime in linux ?


Method 1:
Using command line

root@linuxforfreshers.com:~$mysqladmin  version | grep Uptime

Uptime:                                   30 sec

Method 2:

root@linuxforfreshers.com:~$mysqladmin  status
Uptime: 121  Threads: 1  Questions: 116  Slow queries: 0  Opens: 46  Flush tables: 1  Open tables: 41  Queries per second avg: 0.958

Method 3:

Show MySQL server uptime using a query
root@linuxforfreshers.com:~$ mysql –u username –p password
mysql> SHOW GLOBAL STATUS LIKE 'Uptime';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Uptime        | 223   |
+---------------+-------+


(The uptime is given in seconds)

how to check if mysql server is running or not in linux ?


Method 1:

root@linuxforfreshers.com:~$ service mysql status
Or
root@linuxforfreshers.com:~$ /etc/init.d/mysql status
mysql start/running, process 13749

Method 2:
root@linuxforfreshers.com:~$ps aux | grep mysql
mysql    13749  0.2  1.3 492660 52336 ?        Ssl  13:05   0:38 /usr/sbin/mysqld

Method 3:
root@linuxforfreshers.com:~$netstat -vulntp |grep -i mysql
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      13749/mysqld    
Method 4:
root@linuxforfreshers.com:~$lsof -i :3306
COMMAND   PID  USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
mysqld  13749 mysql   10u  IPv4 5037327      0t0  TCP localhost:mysql (LISTEN)

Method 5:
root@linuxforfreshers.com:~$mysqladmin ping

mysqld is alive