-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmysql_securely_mktemp.sh
More file actions
executable file
·43 lines (37 loc) · 1.03 KB
/
mysql_securely_mktemp.sh
File metadata and controls
executable file
·43 lines (37 loc) · 1.03 KB
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
35
36
37
38
39
40
41
42
#!/bin/sh
# call mysql client from shell script without
# passing credentials on command line
#
# this version demonstrates queries read from
# standard input such as mysqldump output.
# It uses a temporary file with restrictive
# permissions to read config information.
# 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_from_file() {
local query_file="$1"
local opts="$2"
local tmpcnf="$(mktemp)"
chmod 600 "${tmpcnf}"
printf "%s\n" \
"[client]" \
"user=${mysql_user}" \
"password=${mysql_password}" \
"host=${mysql_host}" \
"port=${mysql_port}" \
"database=${mysql_database}" \
> "${tmpcnf}"
mysql_exec_from_file_result=$(
HOME="/sys" mysql --defaults-file="${tmpcnf}" "$opts" < "${query_file}"
)
rm "${tmpcnf}"
}
query_file="$(mktemp)"
echo "select 'Hello World' as Message;" > "${query_file}"
mysql_exec_from_file "${query_file}"
echo "${mysql_exec_from_file_result}"
rm "${query_file}"