2021-09-30

How to Run MySQL/MariaDB Query Using Bash Script and Command-Line

#!/bin/bash
#/opt/lampp/script/mysql-stats-AbortedConnection.sh
#LastUpdate: #16:09 2021.09.30
#####################################
source /opt/lampp/LAMPP_VERSION.txt
#####################################
#/opt/lampp/bin/mysql
MYSQL_BIN=$LAMPP_HOME/bin/mysql
#$MYSQL_BIN -u root -p'xxx' -hlocalhost < mysql-AbortedConnection.sql

MySQL_Username="root"
MySQL_Password="xxx"
MySQL_DB="mysql"

#__________[SCRIPT]:BEGIN
cat >temp.sql <<'SQL_STATEMENTS'

use pureftpd;
SHOW GLOBAL STATUS LIKE 'Aborted_connects';
show variables like "Wait_timeout";
show variables like "connect_timeout";

SQL_STATEMENTS
$MYSQL_BIN -u $MySQL_Username -p$MySQL_Password $MySQL_DB -e "SOURCE temp.sql"

\rm -f temp.sql
#__________[SCRIPT]:END

#THE_END

#RESULT:
# +------------------+-------+
# | Variable_name    | Value |
# +------------------+-------+
# | Aborted_connects | 1     |
# +------------------+-------+
# +---------------+-------+
# | Variable_name | Value |
# +---------------+-------+
# | wait_timeout  | 60    |
# +---------------+-------+
# +-----------------+-------+
# | Variable_name   | Value |
# +-----------------+-------+
# | connect_timeout | 10    |
# +-----------------+-------+

#REF: #https://stackoverflow.com/questions/8055694/how-to-execute-a-mysql-command-from-a-shell-script/8055745
#https://www.nitendratech.com/database/sql-query-bash-script/

No comments:

Post a Comment