Implement remote MySQL automatic query using Shell script

  • 2020-05-27 07:23:33
  • OfStack

The following script is a simple command to perform a remote database query, which I believe you can understand. It is very helpful for those who need to automatically check the database or execute some statements every day, as long as you modify it


#!/bin/sh
HOST=192.168.5.40
USER=abc
PASS=123456
QUERY=`mysql -h$HOST -u$USER -p$PASS << EOF
use testdb;
select * from person where name='LiMing';
exit
EOF`
echo $QUERY


For example, change this script into a script to safely delete the mysql2 process log. Mysql master-slave synchronized database binary log is often very large, and manually deleting files is not very safe


#!/bin/sh
Host=192.168.5.30
User=abc
PW=123456
MSG=`mysql -h$Host -u$User -p$PW <<eof< font="">
show master status;
exit
EOF`
LOG=`echo $MSG |awk '{print $5}'`
mysql -h$Host -u$User -p$PW << FOE
purge master logs to  " $LOG " ;
exit
FOE


Related articles: