Part 9: Connecting MySQL to Linux and Useful Linux Commands

In this part, we will discuss how to connect MySQL to Linux, explore some essential Linux commands like systemctl, zip, ps, and kill, and create a shell script to automate file deletion for files older than 15 days.


Using systemctl Command

The systemctl command is used to control and manage system services. It allows you to start, stop, restart, enable, or check the status of services on your Linux system.

Syntax

systemctl [action] [service_name]

Example: Checking the Status of MySQL Service

sudo systemctl status mysql

This checks whether the MySQL service is active, inactive, or failed.

Example: Starting MySQL Service

sudo systemctl start mysql

This starts the MySQL service if it is not already running.


Connecting Nginx and MySQL-Server

To use MySQL with Nginx, both nginx and mysql-server packages must be installed on your Linux system.

Steps:

  1. Install Nginx:

     sudo apt update
     sudo apt install nginx
    
  2. Install MySQL Server:

     sudo apt install mysql-server
    
  3. Verify Installation: Check Nginx status:

     sudo systemctl status nginx
    

    Check MySQL status:

     sudo systemctl status mysql
    

Once installed, Nginx can be configured to work with applications that connect to MySQL.


Useful Linux Commands

1. zip Command

The zip command is used to compress files into a ZIP archive.

Syntax

zip [options] archive_name file1 file2 ...

Example: Compress Files into an Archive

zip backup.zip file1.txt file2.txt

This creates a compressed file named backup.zip containing file1.txt and file2.txt.

2. ps Command

The ps command displays information about running processes.

Syntax

ps [options]

Example: List All Running Processes

ps aux

This lists all processes running on the system with detailed information, including PID (Process ID).

3. kill Command

The kill command is used to terminate processes by their PID.

Syntax

kill [signal] PID

Example: Kill a Process

ps aux | grep "example_process"
kill -9 12345

This finds the PID of example_process and terminates it with signal -9 (force kill).


Shell Script: Delete Files Older Than 15 Days

This shell script will automatically delete files in a specified directory that are older than 15 days.

Script

#!/bin/bash

# Directory to clean up
dir_to_clean="/path/to/directory"

# Find and delete files older than 15 days
find $dir_to_clean -type f -mtime +15 -exec rm -f {} \;

# Print a completion message
echo "Files older than 15 days have been deleted from $dir_to_clean."

Explanation:

  1. /path/to/directory************: Specify the directory to clean up.

  2. find************: Locates files.

    • -type f: Targets only files (not directories).

    • -mtime +15: Finds files modified more than 15 days ago.

    • -exec rm -f {}: Deletes each file found.

  3. Completion Message: Outputs confirmation upon completion.

Running the Script

Save the script to a file, e.g., cleanup.sh, and make it executable:

chmod +x cleanup.sh

Run the script with:

./cleanup.sh

This part covered how to connect MySQL to Linux, manage services with systemctl, and use essential commands like zip, ps, and kill. Additionally, we demonstrated how to create a practical shell script for file management. These tools and techniques are vital for efficient system management and automation.

So thats all for linux for devops series, now i will be covering git and github in my next series, so stay tuned