Wednesday, March 29, 2023

Turning On Mysql Slow Query Log

Slow query log is a mechanism in mysql which will track and record queries that take long time to complete, for database administrator to analyze and optmize to increase mysql performance.


To check if this feature is already turned on, we can run below command in mysql console:
mysql> show variables like 'slow_query_log';

We will see something like below if it is already turned on: 


The query will be deemed slow, of the query takes longer than 'long_query_time' to complete. We can also check this using below command:
mysql> show variables like 'slow_query_log';

In this case, our long_query_time is set to 10 seconds:

We can check the location of the slow_query_log_file by using below command:
mysql> show variables like 'slow_query_log_file';

In this example, the log file is located in /bitnami/mysql/data/5653c641c26b-slow.log

The above variables, can be set in the runtime using below commands:

To turn off slow_query_log
mysql> set global slow_query_log = off;

To set long_query_time to 5 seconds
mysql> set long_query_time = 5;

All the changes that we made in mysql console, is only for the current runtime, and would not survive mysql service restart. To make the changes permanent, we need to modify /etc/my.cnf, under [mysqld], like below
[mysqld]
slow_query_log=1
long_query_time=5
slow_query_log_file=/var/log/mysql/slow.log

And then restart mysql
$ sudo systemctl restart mysql

















Thursday, March 23, 2023

Checking Disk Read & Write Speed in Linux

We are going to use 2 tools for this, one is called hdparm to test read speed, and the second one is called dd, to test write speed.


First, install hdparm
$ sudo apt install hdparm -y

Then, get the disk name. The name of the disk in below example is /dev/sda.
$ lsblk







Test read speed using hdparm. -t option is to record the timing for the read process. In below example, the read speed is around 700MB/sec.
$ sudo hdparm -t /dev/sda








To test write speed, we will use dd command. dd will write a 1M file called speedtest.img to disk, and the "oflag=direct" option will make dd use direct IO, skipping any filesystem cache. dd will show the disk write speed after it finishes writing the file to disk. In below example, the write speed is 100MB/s.
$ dd if=/dev/zero of=speedtest.img bs=1M count=1 oflag=direct


Wednesday, March 1, 2023

Extract Text From Image Using Command Line

The tool to use for this task is called tesseract.


To install tesseract in any ubuntu derivatives, just run below command
$ sudo apt install tesseract-ocr -y
To extract text from an image file called 007.png, run below command
$ tesseract 007.png 007output
007output is the output file, and an extension of .txt will always be put to the output file.

To view the output, just use cat like below
$ cat 007output.txt

Wednesday, February 15, 2023

Print Column Until End of Line in Linux Terminal

I have a document named mydoc.txt which contains below lines

local STAFF
local STUDENTS
local CONTRACT TEACHERS

I need to print all except the first column. To accomplish that, I can use this command
$ cut -f2- -d ' ' mydoc.txt

Where -d is delimiter, and -f is field number. The hyphen after -f2 is to tell 'cut' to print field number 2 until the end of line.

Wednesday, February 8, 2023

Remove Defunct (Zombie) Process Linked to Cinnamon in Linux Mint

I encountered this situation before, whereby my Linux Mint (cinnamon) is showing that it has 20 zombie processes, linked to the cinnamon process as parent process.


How to check for zombie processes?
$ ps -ef | grep defunct

Or you can run top, and view the second line for "zombie" keyword
$ top
To exit from top, just press q

Calculate how many zombie processes inside a system
$ ps -ef | grep defunct | grep -v grep | wc -l

One of the way to remove zombie processes is to restart the parent process. How to check which is the parent process of the zombie processes?
$ ps -ef | grep defunct | awk '{print $3}'

Once we get the PID of the parent process, check what is the actual process that is using the PID
$ ps -ef | grep <PIDNUMBER>

For example, if the PID number is 2323
$ ps -ef | grep 2323

Once we know what is the process, we can safely restart the parent process. 
In the case of cinnamon, a restart can be accomplished by simply pressing ctrl + alt + esc. 

If that is not working, kill the cinnamon PID first, then do ctrl + alt + esc.
$ kill 2323

Your desktop will freeze after you kill cinnamon, but you can restart it by pressing ctrl + alt +esc.