Install Ubuntu MySQL client
I assume you already installed MySQL client in your Ubuntu 16.04. If you had not, then to install MySQL client in Ubuntu, type in the following commands. Having a MySQL client is useless if you do not have a MySQL server to query. To install MySQL server, you can refer to my Ubuntu install WordPress blog post.
sudo apt-get update sudo apt-get install mysql-client -y
Execute MySQL statement in Bash
To execute a MySQL statement in Bash, simply type in the following. I assume your MySQL server is hosted locally using the user “root” and with the password “password”.
mysql --host=localhost --user=root --password=password --execute='select now()'
Alternatively, you can use the shorter version to execute the same statement with the syntax below.
mysql -hlocalhost -uroot -ppassword -e'select now()'
Suppress Bash MySQL warning
Executing your MySQL statement in Bash with your password revealed in plain text will cause a warning to appear each time you execute a statement. It is quite annoying if you are running a loop executing MySQL statements with its output in stdout.
To suppress the warning, type the following commands. Key in your password when it prompts you for the password.
mysql_config_editor set --login-path=localhost --host=localhost --user=root --password
After that execute your MySQL statements using the syntax below. Both statements are the same, one long and one short depending on your preference. By executing it this way, MySQL warning is no longer there.
mysql --login-path=localhost --execute='select now()' mysql --login-path=localhost -e'select now()'