| Server IP : 104.21.95.231 / Your IP : 104.23.243.226 Web Server : Apache System : Linux vm3310.eosdns.net 4.18.0-553.132.1.el8_10.x86_64 #1 SMP Thu Jun 11 14:27:43 UTC 2026 x86_64 User : carnalmedia ( 1000) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /mojo/ |
Upload File : |
#!/bin/bash
#
# Script: mjact_mysql
#
# Usage: mjact_mysql -D <DB name> -U <DB username> -H <hostname> -P -G
#
# Description: A tool for creating MySQL/MariaDB databases, database users and granting privileges
#
# Author: Nikita Shchetinin
#
# Changelog:
# * 05.03.2021: Nikita Shchetinin
# - Initial write
if [ "$(whoami)" != 'root' ]
then
echo "You must execute this script as the root user!"
exit 1
fi
ADD () {
if ! systemctl is-active --quiet mysqld
then
echo "MySQL/MariaDB is not running!"
exit 1
fi
if [ -z "$hostname" ]
then
hostname="localhost"
fi
if [ -z "$database" ]
then
echo "The database name is required!"
exit 1
else
if [[ ! $database =~ ^[0-9a-zA-Z_]+$ ]]
then
echo "The database name contains ivalid characters!"
exit 1
elif [ ${#database} -gt 64 ]
then
echo "The database name contains more than 64 characters!"
exit 1
fi
if [ -z "$user" ]
then
user=$database
else
if [[ ! $user =~ ^[0-9a-zA-Z_]+$ ]]
then
echo "The username contains ivalid characters!"
exit 1
elif [ ${#user} -gt 80 ]
then
echo "The useranme contains more than 80 characters!"
exit 1
fi
fi
fi
if [ "$passprompt" == "YES" ]
then
read -p "Enter the password: " password
while [ -z "$password" ]
do
echo "The password is required!"
read -p "Enter the password: " password
done
else
password=$(</dev/urandom tr -dc A-Za-z0-9 | head -c16)
fi
if [ $(mysql -e "SELECT Host,User FROM mysql.user WHERE User='$user' AND Host='$hostname'" | wc -l) != 0 ]
then
echo "The $user@$hostname username is already added to this server!"
exit 1
fi
if [ -z "$grantonly" ]
then
if mysql -e "SHOW DATABASES" | grep -qw "$database"
then
echo "The $database database is already added to this server!"
exit 1
else
mysql -e "CREATE DATABASE $database"
mysql -e "CREATE USER '$user'@'$hostname' IDENTIFIED BY '$password'"
mysql -e "GRANT ALL PRIVILEGES on $database.* TO '$user'@'$hostname'"
fi
else
mysql -e "CREATE USER '$user'@'$hostname' IDENTIFIED BY '$password'"
mysql -e "GRANT ALL PRIVILEGES on $database.* TO '$user'@'$hostname'"
fi
echo "Hostname: localhost"
echo "Database name: $database"
echo "Database username: $user"
echo "Database username's password: $password"
}
HELP () {
echo "A tool for creating MySQL/MariaDB databases, database users and granting privileges"
echo "usage: mjact_mysql -D <DB name> -U <DB username> -H <hostname> -P (to submit password using a password prompt) -G (to create a user and add grants only)"
}
if [[ "${1}" == "" ]] || [[ "${1}" == "-h" ]] || [[ "${1}" == "--help" ]]
then
HELP
else
while getopts ":D:U:H:PG" OPTION
do
case "${OPTION}" in
D)
database="${OPTARG}"
;;
U)
user="${OPTARG}"
;;
H)
hostname="${OPTARG}"
;;
P)
passprompt="YES"
;;
G)
grantonly="YES"
;;
esac
done
ADD
fi