Mysql error handling in queries bash script - mysql

I need to catch any invalid credentials while connecting to MariaDB, and overwrite the stderr stream explaining the error.
I have tried to use the following since it seemed to be the easiest and shortest code, but the database throws it's own error instead of displaying mine so I do not think the condition is even working.
right after the mysql command
if [ "$?" -eq 0 ]; then
echo "There is something wrong with the arguments provided">&2
exit 2
else
: #some code
fi
TABLES=$(mysql --skip-column-name -u $USER -pPASSWORD $DB -e "SHOW TABLES;" | grep -v '+' | cut -d' ' -f2)
if [ "$?" -eq- 0 ]; then
echo "There is something wrong with the arguments provided">&2
exit 2
else
: #some code
fi
I was expecting to see my stderr message appearing instead it is showing the mariadb error message on the screen.

The exit status of a pipeline is the status of the last command in the pipeline. So in your case, it's the status of cut, not mysql.
You can use the PIPESTATUS array to get the exit status of other commands in the pipeline. However, this is tricky when the pipeline is in a command substitution, because you need PIPESTATUS from the subshell. See Pipe status after command substitution
If you don't want to see the database error message, you need to redirect stderr.
You need to check if the status is not 0. In the shell, 0 means success.
TABLES=$(mysql --skip-column-name -u $USER -pPASSWORD $DB -e "SHOW TABLES;" 2>/dev/null | grep -v '+' | cut -d' ' -f2; echo ": ${PIPESTATUS[0]}")
status=${TABLES##*: }
if [ $status -ne 0 ]
then
echo "There is something wrong with the arguments provided">&2
exit 2
else
# Remove the appended status
TABLES=${TABLES%:*}
TABLES=${TABLES%$'\n'}
fi

Related

How can I execute MySQL commands line by line from bash and capture the output?

If there is an alternative to bash, I will appreciate it too.
I have a large dump of MySQL commands (over 10 GB)
When restoring the dump I get a few warnings and occasionally an error. I need to execute those commands and process all warnings and errors. And, preferibly to do it automatically.
mysql --show-warnings
tee logfile.log
source dump.sql
The logfile will contain many lines telling each command was successful, and will display some warnings, particulartly truncate colums. But the original file has tens of thousands of very large INSERTs, the log is not particularly helpful. Despite it requires some kind of supervised interaction. (I cannot program a crontab, for example.)
#!/bin/bash
echo "tee logfile.log" > script.sql
echo "source $1" > script.sql
mysql --show-warnings < script.sql > tmpfile.log 2>&1
cat tmpfile.log >> logfile.log
The tee command doesn't work in this batch environment. I can capture all the warnings, but I cannot figure out which command produced each warning.
So I came down with this small monstruosity:
#!/bin/bash
ERRFILE=$(basename "$0" .sh).err.log
LOGFILE=$(basename "$1" .sql).log
log_action() {
WARN=$(cat)
[ -z "$WARN" ] || echo -e "Line ${1}: ${WARN}\n${2}" >> "$LOGFILE"
}
echo 0 > "$ERRFILE"
log_error() {
ERNO=$(cat "$ERRFILE")
ERR=$(cat)
[ -z "$ERR" ] || echo -e "*** ERROR ***\nLine ${1}: ${ERR}\n${2}" >> "$LOGFILE"
(( ERNO++ ))
echo $ERNO > "$ERRFILE"
}
COUNT=0
COMMAND=''
echo -e "**** BEGIN $(date +%Y-%m-%d\ %H:%M:%S)\n" > "$LOGFILE"
exec 4> >(log_action $COUNT "$COMMAND")
exec 5> >(log_error $COUNT "$COMMAND")
exec 3> >(mysql --show-warnings >&4 2>&5)
while IFS='' read -r LINE || [[ -n "$line" ]]
do
(( COUNT++ ))
[ ${#LINE} -eq 0 ] && continue # discard blank lines
[ "${LINE:0:2}" = "--" ] && continue # discard comments
COMMAND+="$LINE" # build command
[ "${LINE: -1}" != ";" ] && continue # if not finnished keep building
echo $COMMAND >&3 # otherwise execute
COMMAND=''
done < "$1"
exec 3>$-
exec 5>$-
exec 4>$-
echo -e "**** END $(date +%Y-%m-%d\ %H:%M:%S)\n" >> "$LOGFILE"
ERRS=$(cat "$ERRFILE")
[ "ERRS" = 0 ] || echo "${ERRS} Errors." >&2
This scans the file at $1 and sends the commands to an open MySQL connection at &3. That part is working fine.
The capture of warnings and errors is not working though.
It only records the first error.
It only records the first warning.
I haven't find a good way to pass the line number $COUNT and offending command $COMMAND to the recording functions.
The only error is after the time stamps, and the only warning is after the error, which is not the chronology of the script.

Check if mysql user exists in bash script, getting error

I am working on a bash script to check if a mysql user exists, with following commands:
checkuser=`mysql -u $mysqlroot -p$rootpw -e "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '$user_name') ;"`
lastchr=${checkuser#${checkuser%?}}
if [ $lastchr == 1 ] ; then
clear
echo "ERROR: mysql user exists"
fi
My problem is, that the terminal always gives me following feedback:
/etc/hosting: line 775: syntax error near unexpected token `('
/etc/hosting: line 775: `checkuser=`mysql -u $mysqlroot -p$rootpw -e "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '$user_name') ;"`'
I tried with escaping the "(" and ")", but I didn't success.
What am I doing wrong? I tried all different kinds of escaping, single quotes, ... and it didn't help.
Kind regards
EDIT:
because maybe needed, previous lines:
echo "creating mysql user ..."
echo ""
read -p "enter username: " user_name
if [[ -z "$user_name" ]] ; then
clear
echo "username mustn't be empty!"
create_mysql_user # calls function again
fi
# check username length
if [ ${#my_user} -gt 16 ] ; then
clear
echo "ERROR: the username mustn't be longer than 16 characters"
create_mysql_user
fi
This is an old question but still searchable on internet. I would like to answer it because it doesn't have a proper answer.
To check the existence of mysql user using bash, you can use the following package jq to transform the query result to readable output as list of users in array format:
# Get list of all sql users
sql_users=$(mysql -e "SELECT USER FROM mysql.user;" | jq -rR .)
check_sql_user=$(echo "${sql_users}" | grep "username_to_check")
if [[ -z "${check_sql_user}" ]]; then
echo "No user found"
# Call create user here
else
echo "User found"
fi

Shell script if condition

I'm writing a script which runs the following command
mysql -u root -e "show databases"
and this will display a list of databases.
If this table doesn't contain a database by name "userdb", it should do the following-
if [ ... ]; then
echo "error"
exit
fi
What do i write in the if [ ... ] condition?
You can check with grep if the table name is listed. grep -q will not print anything to the console but will set the exit status according to the result (the exit status will then be checked by if).
if ! mysql -u root -e 'show databases' | grep -q '^userdb$' ; then
echo error
exit
fi
About the regular expression: '^' matches the beginning of the line and '$' matches the end of the line (to avoid a false positive for database names containing userdb, e.g. userdb2)
Try this one:
usedb=DBname
check=`mysql -u root -e "show databases" | grep $userdb`
if [ "$check" != "$userdb" ]; then
echo "error"
exit
fi
But here will be an error if line with database name contain any other information.
Try to workaround it with regexp

BASH: check for errors before testing conditions

I am trying to write a simple Bash script to monitor MySQL replication status. The script is like this:
#!/bin/bash
dbhost=192.168.1.2
repluser=root
replpasswd=password
echo "show slave status\G"|\
mysql -h $dbhost -u $repluser -p$replpasswd > tmpd 2>/dev/null
repl_IO=$(cat tmpd | grep "Slave_IO_Running" | cut -f2 -d':')
repl_SQL=$(cat tmpd | grep "Slave_SQL_Running" | cut -f2 -d':')
if [ $repl_IO != "Yes" -o $repl_SQL != "Yes" ] ; then
echo
echo -e "\033[31m Replication Error."
echo -e "\033[0m"
mail -s "replication error" email#domain.com < tmpd
else
echo
echo -e "\033[32mReplication is working fine"
echo -e "\033[0m"
fi
The problem is that the script only works if both the master and the slave are up. If the master is down, and I run the script, it displays the error message and sends the email.
If both master/slave are up, the script displays "Replication is working fine" which is okay. But when I shutdown the slave and run the script, I get this error:
./monitor.bash: line 9: [: too many arguments
Replication is working fine
I know the problem is that since I'm querying the slave MySQL server, it's not finding it. So it's not checking the conditions Slave_IO_Running and Slave_SQL_Running. How would I go
about checking if the slave server is up BEFORE running those conditions. So in short, I only want "Replication is working fine" to be displayed, if both the master & slave are up and
running and it matches those conditions. Any help would be appreciated. Thank you.
If $repl_IO and $repl_SQL are blank, then this:
if [ $repl_IO != "Yes" -o $repl_SQL != "Yes" ] ; then
is equivalent to this:
if [ != Yes -o != Yes ] ; then
and I think you can see why that doesn't work. You need either to wrap your parameter-expansions in double-quotes, so that they're treated as single arguments no matter what they contain:
if [ "$repl_IO" != "Yes" -o "$repl_SQL" != "Yes" ] ; then
or to use [[...]] instead of [...], since it's a bit smarter with these things:
if [[ $repl_IO != "Yes" -o $repl_SQL != "Yes" ]] ; then
or both:
if [[ "$repl_IO" != "Yes" -o "$repl_SQL" != "Yes" ]] ; then
One problem with your script: experienced shell programmers know that if a variable is empty the statement
if [ $foo = something ]
will look to the shell like
if [ = something ]
You can fix this with
if [ "$foo" = something ]
So in general, put " marks around all variables used inside [ ]
Or use [[ ]] if you use bash, quoting variables isn't needed and it's more powerful than [ ].
greybot on the IRC channel of freenode said :
[[ is a bash keyword similar to (but more powerful than) the [
command. See http://mywiki.wooledge.org/BashFAQ/031 and
http://mywiki.wooledge.org/BashGuide/TestsAndConditionals. Unless
you're writing for POSIX sh, we recommend [[.

MySQL error in Bash

i'm trying to load some tables to mysql into a bash script so i have the followin code
DOMY="$MYSQL --user=xxxxx --password=xxxxx --database=$DBNAME"
for filename in $(cat $HPATH/toload.tables)
do
$DOMY < $filename 2>/dev/null
if [ $? -ne 0 ]
then
echo "#003|Error loading $filename"
exit 1
fi
done
if i see $? (echo $?) it give me 0 (zero) but the exit 1 is executed.
What i'm doin wrong?
You can't see what's wrong because you have /dev/null-ed standard error.
Also, you are using an unnecessarily complex and somewhat error-prone code pattern where you examine $? later. It would be better to just write something like:
for i in "$#"; do
if mysql -u root < $i; then
echo ok # do "ok" processing here
else
echo not so ok # error path
fi
done