I want to use this script to run with Ansible. My requirements were to read the maximum connections in mysql server. Also I want to write the output from mysql client in to a file.
You can use
mysql -e "show status like 'max_used_connections';"
Or you can use
mysql <<EOF
tee /tmp/mysql_max.txt
show variables like 'max_connections';
show status like 'max_used_connections';
EOF
output
Logging to file '/tmp/mysql_max.txt'
Variable_name Value
max_connections 100
Variable_name Value
Max_used_connections 10
Found here
https://www.shellhacks.com/mysql-run-query-bash-script-linux-command-line/
https://alvinalexander.com/mysql/how-save-output-mysql-query-file/