Files
MaxScale/script/create_grants
Markus Makela df38b4dd50 Add utility scripts to make installation easier
The `create_grants` scripts allow users to be easily "copied" to
MaxScale. It queries the backend for grants for all users and converts
them into similar grants for the MaxScale host.

The `create_roles.sql` is a small set of queries which creates two utility
roles, `proxy_authenticator` and `proxy_monitor`. These roles can be
assigned to the actual service and monitor users with a single grant
command.
2016-09-15 07:13:10 +03:00

62 lines
1.2 KiB
Bash
Executable File

#!/bin/bash
function runQuery(){
mysql -s -s -h $host -P $port -u $user -p$password -e "$1"
if [ $? -ne 0 ]
then
echo "Failed to execute query: $1"
exit
fi
}
# Transform grants to from external hosts to MaxScale's host
function getGrants(){
result=$(runQuery "show grants for $1"|sed -e "s/@[^ ]*/@'$maxscalehost'/" -e "s/ *IDENTIFIED BY.*//" -e "s/$/;/")
echo "$result"
}
user=$(whoami)
host=$(hostname)
port=3306
include_root="and user <> 'root'"
if [ "$1" == "--help" ] || [ $# -eq 0 ]
then
echo "$0 -u USER -p PASSWORD -h HOST -P PORT [-r]"
exit
fi
while getopts "u:p:h:P:r" var
do
case $var in
u)
user=$OPTARG
;;
p)
password=$OPTARG
;;
h)
host=$OPTARG
;;
P)
port=$OPTARG
;;
r)
include_root=""
;;
esac
done
# Get the MaxScale hostname from the backend server
maxscalehost=$(runQuery "select user()")
maxscalehost=${maxscalehost#*@}
# List all the users
runQuery "select concat(\"'\", user, \"'\", '@', \"'\", host, \"'\") from mysql.user where user <> '' and host <> '%' $include_root"|while read i
do
getGrants "$i"
done