Script to create user and grant in a remote mariadb
This commit is contained in:
parent
51ef2983e9
commit
4f415e6a7b
51
scripts/mariadb-cluster-create-database-and-grant
Executable file
51
scripts/mariadb-cluster-create-database-and-grant
Executable file
|
@ -0,0 +1,51 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
HOST="${1}"
|
||||||
|
DATABASE="${2}"
|
||||||
|
USERNAME="${3}"
|
||||||
|
|
||||||
|
function usage() {
|
||||||
|
echo "Usage: ${0} <hostname> <database> <username>"
|
||||||
|
echo "Example: ${0} db-1.example.com"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
if [ -z "${HOST}" ]; then
|
||||||
|
usage
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "${DATABASE}" ]; then
|
||||||
|
usage
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "${USERNAME}" ]; then
|
||||||
|
usage
|
||||||
|
fi
|
||||||
|
|
||||||
|
PASSWORD=$(openssl rand 32 | base64)
|
||||||
|
|
||||||
|
|
||||||
|
SQL="CREATE DATABASE ${DATABASE}; CREATE USER '${USERNAME}'@'%' IDENTIFIED BY '${PASSWORD}'; GRANT ALL PRIVILEGES ON ${DATABASE}.* TO '${USERNAME}'@'%';"
|
||||||
|
|
||||||
|
echo "Execute the following command on ${HOST} [Y/n]?"
|
||||||
|
echo "$SQL"
|
||||||
|
|
||||||
|
read -r x
|
||||||
|
case $x in
|
||||||
|
y|Y|"")
|
||||||
|
true
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "script aborted"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
ssh -t "${HOST}" "docker exec -ti mariadb-db-1 mariadb -u root -p\$(puppet lookup --render-as s mariadb_root_password 2> /dev/null) -e \"${SQL}\""
|
||||||
|
|
||||||
|
|
||||||
|
echo "Command successful"
|
||||||
|
echo "Database: ${DATABASE}"
|
||||||
|
echo "Username: ${USERNAME}"
|
||||||
|
echo "Password: ${PASSWORD}"
|
Loading…
Reference in a new issue