-
Notifications
You must be signed in to change notification settings - Fork 0
/
mariadb-create-database.sh
executable file
·47 lines (40 loc) · 1.35 KB
/
mariadb-create-database.sh
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
43
44
45
46
47
#!/bin/bash
# Check if the database name is provided
if [ -z "$1" ]; then
echo "Usage: $0 <database_name>"
exit 1
fi
# Set variables
dbname=$1
dbuser=$dbname
dbpass=$(pwgen -s 16 1 | tr -d '\n')
secret_name="${dbname}-db-secret"
mariadburl="mariadb://${dbuser}:${dbpass}@mariadb-primary.mariadb.svc.cluster.local:3306/${dbname}"
# Create the database and user, and grant privileges
mariadb -h mariadb-primary.mariadb.svc.cluster.local -uroot -p"${MARIADB_ROOT_PASSWORD}" <<EOF
CREATE DATABASE IF NOT EXISTS \`${dbname}\`;
CREATE USER IF NOT EXISTS \`${dbuser}\`@'%' IDENTIFIED BY '${dbpass}';
GRANT ALL PRIVILEGES ON \`${dbname}\`.* TO \`${dbuser}\`@'%';
FLUSH PRIVILEGES;
EOF
# Check if the database and user creation was successful
if [ $? -ne 0 ]; then
echo "Failed to create database or user."
exit 1
fi
# Create the Kubernetes secret
kubectl create secret generic $secret_name --namespace admin \
--from-literal=database=$dbname \
--from-literal=mariadb-password=$dbpass \
--from-literal=mariadburl=$mariadburl
# Check if the secret creation was successful
if [ $? -ne 0 ]; then
echo "Failed to create Kubernetes secret."
exit 1
fi
echo "Database and user created successfully."
echo "Kubernetes secret '${secret_name}' created successfully."
echo "MariaDB URL: ${mariadburl}"
echo "Database: ${dbname}"
echo "User: ${dbuser}"
echo "Password: ${dbpass}"