Bash: testing if executed command was succesfull - mysql

I have the following script:
if [ `mysql -u root -p < test.sql` ]; then
echo "success"
else
echo "some error"
fi
How can I check in the if if the command went well or there was some sql error?

You don't need the test command, i.e. [ here.
Assuming that mysql produces a non-zero exit code in case of failure and 0 on success, then you can say:
if mysql -u root -p < test.sql; then
echo "success"
else
echo "some error"
fi

Another way this is typically done is with the "&&" and "||" separators. For example:
mysql -u root -p < test.sql && echo "success" || echo "some error"
If the mysql command exits with status 0, then "success" will be printed. If it doesn't, "some error" will be printed.

Related

How to run a MySQL procedure from a Linux shell script and store its output in a file

I have a mysql procedure which I want to run from a shell script in linux and store the output in a log file. But my script is not working. Below is my script:
#!/bin/bash
source CX20-PIM-properties.prop
status=$(mysql -u $user -h $host -D $database -se "call fetchFromPAsIsToPIDX()")
if [ $status ]; then
echo "Procedure executed successfully" | tee procedure_output.log
else
echo "Procedure execution failed" | tee procedure_output.log
fi
And the output is below:
[anurag#pimdev0 ~]$ ./load-from-AsIs-to-IDX.sh
Procedure execution failed
You should store the return value of cmd in status not stdout of cmd.
#!/bin/bash
source CX20-PIM-properties.prop
if [ `mysql -u $user -h $host -D $database -se "call fetchFromPAsIsToPIDX()" &> /dev/null` ]; then
echo "Procedure executed successfully" | tee procedure_output.log
else
echo "Procedure execution failed" | tee procedure_output.log
fi

Catching exit code after executing mysql query

I'm working on a bash script that will query mysql. I would like to add some error checking. Let say if the following query for some reason fails I want to catch the exit error and exit the script. For example this is part of my script.
QUERY="SELECT DISTINCT `TABLE_SCHEMA`, `TABLE_NAME`
FROM `information_schema`.`TABLES`
WHERE table_schema NOT IN ( 'mysql', 'information_schema', 'performance_schema' )"
mysql -u user -pPASSWD --batch -N -e "$QUERY" | while read DATABASE TABLE;
do
...
...
...
done
How could i catch the exit code after the scripts run the "$QUERY". I was thinking something like this. But it doesn't seem to work.
mysql -u user -pPASSWD --batch -N -e "$QUERY" echo $? | while read DATABASE TABLE;
Any ideas
You are in the good way: $? is the flag to check:
$ mysql -h mydb <<< "SELECT * FROM MyDB.some_table_that_exists;"
$ echo $?
0
$ mysql -h mydb <<< "SELECT * FROM MyDB.asdfasdfasdf;"
ERROR 1146 (42S02) at line 1: Table 'MyDB.asdfasdfasdf;' doesn't exist
$ echo $?
1
So what you can do is to execute the query and then:
if [ $? ... ]; then ...

syntax error near unexpected token `done' in bash code

I am looking for some working shell code for auto restart mysql servers if it is not working. here are some code witch i foound in google search, listen 3306 port, if it is not working, restart. if can not restart, reboot the server.
Can this code work? If not, is anyone can share me a working code? If yes, i met syntax error near unexpected tokendone' in bash code`, how to solve it? thanks.
PORT=`netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $5}'`
MYSQLIP=`ifconfig eth0|awk '/inet/{print $2}'|cut -c 6-`
while [ `whoami` == "root" ]
do
if [ "$PORT" == "3306" ];then
echo "mysql is running......"
else
service mysql restart
if [ "$PORT" == "3306" ];then
else
reboot
fi
fi
break
done
Modify code:
PORT=`netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $5}'`
MYSQLIP=`ifconfig eth0|awk '/inet/{print $2}'|cut -c 6-`
while [ `whoami` == "root" ]
do
if [ "$PORT" == "3306" ];then
echo "mysql is running......"
else
service mysql restart
if [ "$PORT" == "3306" ];then
:
else
reboot
fi
fi
break
done
Your condition test must execute some sort of code if true.
It looks like you want to reboot if "$PORT" is not "3306".
You should only use break when testing a condition, otherwise the loop will only execute once.
The PORT variable will not update unless you call the code that sets it again after you need it to update.
Also, you don't need to use grep when you are using awk.
#!/bin/bash
callport ()
{
PORT=`netstat -na|awk -F[:" "]+ '/LISTEN/ && /3306/ {print $5}'`
}
MYSQLIP=`ifconfig eth0|awk '/inet/{print $2}'|cut -c 6-`
while [ `whoami` == "root" ]
do
callport
if [ "$PORT" == "3306" ];then
echo "mysql is running......"
break
else
service mysql restart
callport
if [ "$PORT" != "3306" ];then
reboot
fi
fi
done

Syntax error in mysql backup script

i am using a script in my macbook pro to backup Mysql databases of my development apps, and its working fine.
But i tried to use it in my ubuntu server test and gave this error :
Syntax error: "(" unexpected (expecting "done")
On this line of code:
table_types=($($MYSQL -u $DB_USER -p$DB_PASS -e
"show table status from $DB" | awk '{ if ($2 == "MyISAM" || $2 == "InnoDB") print $1,$2}'))
Can someone help me?
You forgot to open a bracket "(". Try this:
table_types=($($MYSQL -u $DB_USER -p$DB_PASS -e "show table status from $DB" | awk '({ if ($2 == "MyISAM" || $2 == "InnoDB") print $1,$2}'))

Shell and MySQL custom connection success message

How can I print a custom success message if connected to mysql via shell? Instead of displaying mysql error messages.
Something like this
if [ "$mysql_connected" ]; then
echo "connected"
# do mysql stuff
else
echo "connection failed"
fi
Thanks in advance!
Use exit status as shown below:
mysql -uroot -pabc -e"show databases;"
if [ $? -eq 0 ];then
echo "success";
else
echo "failed to connect";
fi