1

I need to suppress all error messages that the mysql command prints to stdout. I saw many other similar questions but all answers suggest workarounds to avoid these messages (check database/table/column exists before executing query). But I need the mysql command to return a failure exit code on error and don't print anything to output except the data explicitly requested in a query in a successful run. The -s key doesn't help in hiding error messages.

My task is to execute a MySQL query in a script and get either the requested data (printed with the -s key) or a non-zero exit code. I don't want to check each and every table/column/etc existence before executing a target query. How can I achieve this?

UPD: I tried this but it didn't help:

mysql ... db -s -N -e "SELECT config_id FROM core_config_data LIMIT 1;" 2> /dev/null

1 Answer 1

1

To sum it up:

You want the mysql command to:

  • not print any error
  • exit with a non-success code when a query fails

Then I've got good news for you! The output of errors will always be on stderr. Therefore you can just redirect the output to null or whatever you like.

root@icarus ~/so # mariadb -Dmaio290sql1 -e 'SELECT * FROM wp_users' -s 2> bla.txt
[actual content]
root@icarus ~/so # echo $?
0
root@icarus ~/so # mariadb -Dmaio290sql1 -e 'SELECT * FROM nope' -s 2> bla.txt
root@icarus ~/so # echo $?
1

The last query is throwing an error and therefore the exit code is not 0. This was tested on MariaDB though: 10.3.27-MariaDB-0+deb10u1 Debian 10.

Sign up to request clarification or add additional context in comments.

1 Comment

Could you confirm this with the mysql executable, not mariadb?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.