This blog post discusses how to use MySQL shell securely from Bash.
The Bourne shell is everywhere. It is part of the most basic Linux install. You will find it on the biggest SPARC machines down to a Raspberry Pi. It is nice to know it will always be there. Unlike other, more complex scripting environments such as Perl and Python, it doesn’t require any additional dependencies to be installed.
Anyone that has automated a MySQL task using a Bourne shell such as Bash will be familiar with the following message:
|
1 |
Warning: Using a password on the command line interface can be insecure. |
This semi-ominous warning describes a security flaw in passing credentials on a process command line. Any unprivileged user on the system can use a command like ps aux to find these credentials. While the MySQL shell has added some additional protections to hide these credentials, other information such as database user names, host names, ports and sockets can still be determined by process scanning.
The recommended approach to get around this warning is to use a configuration file to store these credentials. However, in the case of a self-contained script, we may not want to require the user to create a credential file. It would need to be maintained, and might interfere with other tools that use the MySQL shell. For creating automated tools in the Percona Development Services department, I came up with a couple of methods for integrating the MySQL shell into Bash (or any other Bourne Shell) securely.
This first script demonstrates the shell function mysql_exec(). This is for use with small queries that are normally passed to the MySQL shell via the -e parameter.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#!/bin/sh # call mysql client from shell script without # passing credentials on command line # This demonstrates small single queries using # the -e parameter. Credentials and connection # info are sent through standard input. # david . bennett @ percona . com - 12/27/2016 mysql_user=root mysql_password=password mysql_host=127.0.0.1 mysql_port=3306 mysql_database=test mysql_exec() { local query="$1" local opts="$2" mysql_exec_result=$( printf "%s\n" \ "[client]" \ "user=${mysql_user}" \ "password=${mysql_password}" \ "host=${mysql_host}" \ "port=${mysql_port}" \ "database=${mysql_database}" \ | HOME="/sys" mysql --defaults-file=/dev/stdin "${opts}" -e "${query}" ) } mysql_exec "select 'Hello World' as Message" echo "${mysql_exec_result}" |
The above script allows the specification of credentials and connection information via variables in the script. As with any other shell script, these can be moved into a configuration file and secured with chown/chmod, then included with the source or . command. The mysql_exec() function creates a default my.cnf [client] on the fly and passes it to the MySQL shell via –defaults-file=/dev/stdin. The configuration is never written to disk, which makes this method a bit more secure.
Sometimes, you need to process too many queries to pass on the command line. In this case, there is another technique for passing the credentials.
|
1 |
mysql_exec_from_file() {<br> local query_file="$1"<br> local opts="$2"<br> local tmpcnf="$(mktemp)"<br> chmod 600 "${tmpcnf}"<br> printf "%sn" <br> "[client]" <br> "user=${mysql_user}" <br> "password=${mysql_password}" <br> "host=${mysql_host}" <br> "port=${mysql_port}" <br> "database=${mysql_database}" <br> > "${tmpcnf}" <br> mysql_exec_from_file_result=$(<br> mysql --defaults-file="${tmpcnf}" "$opts" < "${query_file}"<br> )<br> rm "${tmpcnf}"<br>} |
This technique uses a temporary file, which allows the queries to be passed from a file or input device. Restrictive permissions are set on the file before the configuration is written. The temporary configuration is removed immediately after the shell exits.
While other languages may offer cleaner ways to access your MySQL database, you’ll always know that you’ll be able to execute your shell-based MySQL job scripts across all of the Unix machines in your enterprise.
You can download these scripts directly from my github account.
Happy scripting!
Discover Percona Server for MySQL, an enhanced, fully open source alternative to standard MySQL databases. Combining the freedom of MySQL Community Edition with enterprise-grade features akin to MySQL Enterprise Edition, Percona Server for MySQL offers unparalleled flexibility, performance, security, and reliability. Embrace a database solution that meets your business demands without the common lock-in and licensing fees of proprietary software.
Resources
RELATED POSTS