mysql_dbu_create.sh
· 6.9 KiB · Bash
Raw
#!/usr/bin/env bash
#
# Script to create MySQL/MariaDB db + user
#
# @author Karel Wintersky <karel.wintersky@gmail.com>
# @version 0.4
# mysql_config_editor set --login-path=proftpd --host=localhost --user=proftpd --password
# version this: https://gist.github.com/KarelWintersky/72021fe214f9d2f6ddc6a28c732baac1
# version 0.2: https://gist.github.com/KarelWintersky/9cb12557873ebc59b5cb94cb37bb6913
#
#
_bold=$(tput bold)
_underline=$(tput sgr 0 1)
_reset=$(tput sgr0)
_purple=$(tput setaf 171)
_red=$(tput setaf 1)
_green=$(tput setaf 76)
_tan=$(tput setaf 3)
_blue=$(tput setaf 38)
_cyan=$(tput setaf 6)
_yellow=$(tput setaf 3)
function _success()
{
printf "${_green}✔ %s${_reset}\n" "$@"
}
function _warning()
{
printf "${_yellow}⚠ %s${_reset}\n" "$@"
}
function _error()
{
printf "${_red}✖ %s${_reset}\n" "$@"
}
function _info()
{
printf "${_blue}ℹ %s${_reset}\n" "$@"
}
function _debug()
{
printf "${_cyan}🔧 %s${_reset}\n" "$@"
}
function _printPoweredBy()
{
local db_type=$(detectDBType)
local db_title="${_bold}${_blue}MySQL${_reset}"
if [ "$db_type" = "mariadb" ]; then
db_title="${_bold}${_purple}MariaDB${_reset}"
fi
echo ""
echo "${_bold}################################################################${_reset}"
echo "${_bold}${db_title} :: Create database, user and password${_reset}"
echo "${_bold}(c) Karel Wintersky <karel.wintersky@gmail.com>, 2018${_reset}"
echo "${_bold}################################################################${_reset}"
echo ""
}
function detectDBType()
{
local mysql_bin=$(which mysql 2>/dev/null)
if [ -z "$mysql_bin" ]; then
_error "MySQL client not found!"
exit 1
fi
# Проверяем, является ли /usr/bin/mysql символической ссылкой
if [ -L "/usr/bin/mysql" ]; then
local link_target=$(readlink -f "/usr/bin/mysql")
if [[ "$link_target" == *"mariadb"* ]]; then
echo "mariadb"
return
fi
fi
# Дополнительная проверка через which mariadb
local mariadb_bin=$(which mariadb 2>/dev/null)
if [ -n "$mariadb_bin" ] && [ "$mysql_bin" = "$mariadb_bin" ]; then
echo "mariadb"
return
fi
echo "mysql"
}
function getDBBinary()
{
local db_type=$(detectDBType)
if [ "$db_type" = "mariadb" ]; then
which mariadb
else
which mysql
fi
}
function generatePassword()
{
echo "$(openssl rand -base64 12)"
}
function getCredentials()
{
_info "Please enter database credentials:"
echo ""
read -e -p "${_bold}${_blue}?${_reset} ${_bold}Enter the NAME of the new database:${_reset} " DBNAME
read -e -p "${_bold}${_blue}?${_reset} ${_bold}Enter HOST for user access (% for remote access): ${_reset} " -i "localhost" USERHOST
read -e -p "${_bold}${_blue}?${_reset} ${_bold}Enter the database CHARACTER SET (latin1, utf8): ${_reset} " -i "utf8mb4" CHARSET
read -e -p "${_bold}${_blue}?${_reset} ${_bold}Enter the NAME of the user:${_reset} " -i "$DBNAME" USERNAME
read -e -p "${_bold}${_blue}?${_reset} ${_bold}Enter the PASSWORD for the user:${_reset} " -i "$PASSWORD" PASSWORD
}
function getRootPassword()
{
echo ""
_warning "Root password required for database operations"
read -e -p "${_bold}${_yellow}?${_reset} ${_bold}Enter MySQL/MariaDB root user password:${_reset} " -s ROOTPASSWORD
echo ""
ROOTACCESS="--user=root --password=${ROOTPASSWORD}"
}
function checkDBExist()
{
local FOUND
FOUND=$(${DB_BINARY} ${ROOTACCESS} -e "SHOW DATABASES LIKE '${DBNAME}';" | grep ${DBNAME})
echo ${FOUND}
}
function checkUserExist()
{
local FOUND
FOUND=$(${DB_BINARY} ${ROOTACCESS} -e "SELECT COUNT(*) FROM mysql.user WHERE user = '${USERNAME}';" | grep 1)
if [ "${FOUND}" = "1" ]; then
echo "1"
else
echo "0"
fi
}
function create()
{
if [ ! -f ~/.my.cnf ]; then
getRootPassword
fi
echo ""
_info "Starting database creation process..."
echo ""
if [[ -n $(checkDBExist) ]]; then
_warning "Database ${_bold}${DBNAME}${_reset} already exists!";
else
_info "Creating database ${_bold}${DBNAME}${_reset}..."
${DB_BINARY} ${ROOTACCESS} -e "CREATE DATABASE ${DBNAME} /*\!40100 DEFAULT CHARACTER SET ${CHARSET} */;"
_success "Database ${_bold}${DBNAME}${_reset} successfully created!"
fi
echo ""
if [ $(checkUserExist) = "0" ]; then
_info "Creating new user ${_bold}${USERNAME}${_reset}..."
${DB_BINARY} ${ROOTACCESS} -e "CREATE USER ${USERNAME}@'${USERHOST}' IDENTIFIED BY '${PASSWORD}';"
_success "User ${_bold}${USERNAME}${_reset} successfully created!"
else
_warning "User ${_bold}${USERNAME}${_reset} already exists!";
fi
echo ""
_info "Granting ALL privileges on ${_bold}${DBNAME}${_reset} to ${_bold}${USERNAME}${_reset}..."
${DB_BINARY} ${ROOTACCESS} -e "GRANT ALL PRIVILEGES ON ${DBNAME}.* TO '${USERNAME}'@'${USERHOST}';"
${DB_BINARY} ${ROOTACCESS} -e "FLUSH PRIVILEGES;"
_success "Privileges successfully granted!"
echo ""
}
function printSuccessMessage()
{
local db_type=$(detectDBType)
local db_name="${_bold}${_blue}MySQL${_reset}"
if [ "$db_type" = "mariadb" ]; then
db_name="${_bold}${_purple}MariaDB${_reset}"
fi
_success "${db_name} database creation completed!"
echo ""
echo "${_bold}${_green}################################################################${_reset}"
echo ""
echo " ${_green}✓${_reset} ${_bold}Database Type:${_reset} ${db_name}"
echo " ${_green}✓${_reset} ${_bold}Database:${_reset} ${_green}${DBNAME}${_reset}"
echo " ${_green}✓${_reset} ${_bold}User:${_reset} ${_green}${USERNAME}${_reset}"
echo " ${_green}✓${_reset} ${_bold}Password:${_reset} ${_green}${PASSWORD}${_reset}"
echo " ${_green}✓${_reset} ${_bold}Host:${_reset} ${_green}${USERHOST}${_reset}"
echo " ${_green}✓${_reset} ${_bold}Charset:${_reset} ${_green}${CHARSET}${_reset}"
echo ""
echo "${_bold}${_green}################################################################${_reset}"
}
################################################################################
# Main
################################################################################
export LC_CTYPE=C
export LANG=C
VERSION="0.3"
# Определяем бинарник для работы с БД
DB_BINARY=$(getDBBinary)
DB_TYPE=$(detectDBType)
if [ -z "$DB_BINARY" ]; then
_error "No database client found (mysql or mariadb)"
exit 1
fi
ROOTACCESS=
ROOTPASSWORD=
CHARSET='utf8mb4';
DBNAME=
USERNAME=
USERHOST='localhost'
PASSWORD=$(generatePassword);
function main()
{
_printPoweredBy
getCredentials
echo ""
echo "${_bold}${_blue}################################################################${_reset}"
echo ""
create
printSuccessMessage
}
main
exit 0
| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # |
| 4 | # Script to create MySQL/MariaDB db + user |
| 5 | # |
| 6 | # @author Karel Wintersky <karel.wintersky@gmail.com> |
| 7 | # @version 0.4 |
| 8 | # mysql_config_editor set --login-path=proftpd --host=localhost --user=proftpd --password |
| 9 | # version this: https://gist.github.com/KarelWintersky/72021fe214f9d2f6ddc6a28c732baac1 |
| 10 | # version 0.2: https://gist.github.com/KarelWintersky/9cb12557873ebc59b5cb94cb37bb6913 |
| 11 | # |
| 12 | # |
| 13 | |
| 14 | _bold=$(tput bold) |
| 15 | _underline=$(tput sgr 0 1) |
| 16 | _reset=$(tput sgr0) |
| 17 | |
| 18 | _purple=$(tput setaf 171) |
| 19 | _red=$(tput setaf 1) |
| 20 | _green=$(tput setaf 76) |
| 21 | _tan=$(tput setaf 3) |
| 22 | _blue=$(tput setaf 38) |
| 23 | _cyan=$(tput setaf 6) |
| 24 | _yellow=$(tput setaf 3) |
| 25 | |
| 26 | function _success() |
| 27 | { |
| 28 | printf "${_green}✔ %s${_reset}\n" "$@" |
| 29 | } |
| 30 | |
| 31 | function _warning() |
| 32 | { |
| 33 | printf "${_yellow}⚠ %s${_reset}\n" "$@" |
| 34 | } |
| 35 | |
| 36 | function _error() |
| 37 | { |
| 38 | printf "${_red}✖ %s${_reset}\n" "$@" |
| 39 | } |
| 40 | |
| 41 | function _info() |
| 42 | { |
| 43 | printf "${_blue}ℹ %s${_reset}\n" "$@" |
| 44 | } |
| 45 | |
| 46 | function _debug() |
| 47 | { |
| 48 | printf "${_cyan}🔧 %s${_reset}\n" "$@" |
| 49 | } |
| 50 | |
| 51 | function _printPoweredBy() |
| 52 | { |
| 53 | local db_type=$(detectDBType) |
| 54 | local db_title="${_bold}${_blue}MySQL${_reset}" |
| 55 | |
| 56 | if [ "$db_type" = "mariadb" ]; then |
| 57 | db_title="${_bold}${_purple}MariaDB${_reset}" |
| 58 | fi |
| 59 | |
| 60 | echo "" |
| 61 | echo "${_bold}################################################################${_reset}" |
| 62 | echo "${_bold}${db_title} :: Create database, user and password${_reset}" |
| 63 | echo "${_bold}(c) Karel Wintersky <karel.wintersky@gmail.com>, 2018${_reset}" |
| 64 | echo "${_bold}################################################################${_reset}" |
| 65 | echo "" |
| 66 | } |
| 67 | |
| 68 | function detectDBType() |
| 69 | { |
| 70 | local mysql_bin=$(which mysql 2>/dev/null) |
| 71 | |
| 72 | if [ -z "$mysql_bin" ]; then |
| 73 | _error "MySQL client not found!" |
| 74 | exit 1 |
| 75 | fi |
| 76 | |
| 77 | # Проверяем, является ли /usr/bin/mysql символической ссылкой |
| 78 | if [ -L "/usr/bin/mysql" ]; then |
| 79 | local link_target=$(readlink -f "/usr/bin/mysql") |
| 80 | if [[ "$link_target" == *"mariadb"* ]]; then |
| 81 | echo "mariadb" |
| 82 | return |
| 83 | fi |
| 84 | fi |
| 85 | |
| 86 | # Дополнительная проверка через which mariadb |
| 87 | local mariadb_bin=$(which mariadb 2>/dev/null) |
| 88 | if [ -n "$mariadb_bin" ] && [ "$mysql_bin" = "$mariadb_bin" ]; then |
| 89 | echo "mariadb" |
| 90 | return |
| 91 | fi |
| 92 | |
| 93 | echo "mysql" |
| 94 | } |
| 95 | |
| 96 | function getDBBinary() |
| 97 | { |
| 98 | local db_type=$(detectDBType) |
| 99 | |
| 100 | if [ "$db_type" = "mariadb" ]; then |
| 101 | which mariadb |
| 102 | else |
| 103 | which mysql |
| 104 | fi |
| 105 | } |
| 106 | |
| 107 | function generatePassword() |
| 108 | { |
| 109 | echo "$(openssl rand -base64 12)" |
| 110 | } |
| 111 | |
| 112 | function getCredentials() |
| 113 | { |
| 114 | _info "Please enter database credentials:" |
| 115 | echo "" |
| 116 | |
| 117 | read -e -p "${_bold}${_blue}?${_reset} ${_bold}Enter the NAME of the new database:${_reset} " DBNAME |
| 118 | read -e -p "${_bold}${_blue}?${_reset} ${_bold}Enter HOST for user access (% for remote access): ${_reset} " -i "localhost" USERHOST |
| 119 | read -e -p "${_bold}${_blue}?${_reset} ${_bold}Enter the database CHARACTER SET (latin1, utf8): ${_reset} " -i "utf8mb4" CHARSET |
| 120 | read -e -p "${_bold}${_blue}?${_reset} ${_bold}Enter the NAME of the user:${_reset} " -i "$DBNAME" USERNAME |
| 121 | read -e -p "${_bold}${_blue}?${_reset} ${_bold}Enter the PASSWORD for the user:${_reset} " -i "$PASSWORD" PASSWORD |
| 122 | } |
| 123 | |
| 124 | function getRootPassword() |
| 125 | { |
| 126 | echo "" |
| 127 | _warning "Root password required for database operations" |
| 128 | read -e -p "${_bold}${_yellow}?${_reset} ${_bold}Enter MySQL/MariaDB root user password:${_reset} " -s ROOTPASSWORD |
| 129 | echo "" |
| 130 | |
| 131 | ROOTACCESS="--user=root --password=${ROOTPASSWORD}" |
| 132 | } |
| 133 | |
| 134 | function checkDBExist() |
| 135 | { |
| 136 | local FOUND |
| 137 | FOUND=$(${DB_BINARY} ${ROOTACCESS} -e "SHOW DATABASES LIKE '${DBNAME}';" | grep ${DBNAME}) |
| 138 | echo ${FOUND} |
| 139 | } |
| 140 | |
| 141 | function checkUserExist() |
| 142 | { |
| 143 | local FOUND |
| 144 | FOUND=$(${DB_BINARY} ${ROOTACCESS} -e "SELECT COUNT(*) FROM mysql.user WHERE user = '${USERNAME}';" | grep 1) |
| 145 | |
| 146 | if [ "${FOUND}" = "1" ]; then |
| 147 | echo "1" |
| 148 | else |
| 149 | echo "0" |
| 150 | fi |
| 151 | } |
| 152 | |
| 153 | function create() |
| 154 | { |
| 155 | if [ ! -f ~/.my.cnf ]; then |
| 156 | getRootPassword |
| 157 | fi |
| 158 | |
| 159 | echo "" |
| 160 | _info "Starting database creation process..." |
| 161 | echo "" |
| 162 | |
| 163 | if [[ -n $(checkDBExist) ]]; then |
| 164 | _warning "Database ${_bold}${DBNAME}${_reset} already exists!"; |
| 165 | else |
| 166 | _info "Creating database ${_bold}${DBNAME}${_reset}..." |
| 167 | ${DB_BINARY} ${ROOTACCESS} -e "CREATE DATABASE ${DBNAME} /*\!40100 DEFAULT CHARACTER SET ${CHARSET} */;" |
| 168 | _success "Database ${_bold}${DBNAME}${_reset} successfully created!" |
| 169 | fi |
| 170 | |
| 171 | echo "" |
| 172 | |
| 173 | if [ $(checkUserExist) = "0" ]; then |
| 174 | _info "Creating new user ${_bold}${USERNAME}${_reset}..." |
| 175 | ${DB_BINARY} ${ROOTACCESS} -e "CREATE USER ${USERNAME}@'${USERHOST}' IDENTIFIED BY '${PASSWORD}';" |
| 176 | _success "User ${_bold}${USERNAME}${_reset} successfully created!" |
| 177 | else |
| 178 | _warning "User ${_bold}${USERNAME}${_reset} already exists!"; |
| 179 | fi |
| 180 | |
| 181 | echo "" |
| 182 | |
| 183 | _info "Granting ALL privileges on ${_bold}${DBNAME}${_reset} to ${_bold}${USERNAME}${_reset}..." |
| 184 | ${DB_BINARY} ${ROOTACCESS} -e "GRANT ALL PRIVILEGES ON ${DBNAME}.* TO '${USERNAME}'@'${USERHOST}';" |
| 185 | ${DB_BINARY} ${ROOTACCESS} -e "FLUSH PRIVILEGES;" |
| 186 | _success "Privileges successfully granted!" |
| 187 | echo "" |
| 188 | } |
| 189 | |
| 190 | function printSuccessMessage() |
| 191 | { |
| 192 | local db_type=$(detectDBType) |
| 193 | local db_name="${_bold}${_blue}MySQL${_reset}" |
| 194 | |
| 195 | if [ "$db_type" = "mariadb" ]; then |
| 196 | db_name="${_bold}${_purple}MariaDB${_reset}" |
| 197 | fi |
| 198 | |
| 199 | _success "${db_name} database creation completed!" |
| 200 | echo "" |
| 201 | |
| 202 | echo "${_bold}${_green}################################################################${_reset}" |
| 203 | echo "" |
| 204 | echo " ${_green}✓${_reset} ${_bold}Database Type:${_reset} ${db_name}" |
| 205 | echo " ${_green}✓${_reset} ${_bold}Database:${_reset} ${_green}${DBNAME}${_reset}" |
| 206 | echo " ${_green}✓${_reset} ${_bold}User:${_reset} ${_green}${USERNAME}${_reset}" |
| 207 | echo " ${_green}✓${_reset} ${_bold}Password:${_reset} ${_green}${PASSWORD}${_reset}" |
| 208 | echo " ${_green}✓${_reset} ${_bold}Host:${_reset} ${_green}${USERHOST}${_reset}" |
| 209 | echo " ${_green}✓${_reset} ${_bold}Charset:${_reset} ${_green}${CHARSET}${_reset}" |
| 210 | echo "" |
| 211 | echo "${_bold}${_green}################################################################${_reset}" |
| 212 | } |
| 213 | |
| 214 | ################################################################################ |
| 215 | # Main |
| 216 | ################################################################################ |
| 217 | export LC_CTYPE=C |
| 218 | export LANG=C |
| 219 | VERSION="0.3" |
| 220 | |
| 221 | # Определяем бинарник для работы с БД |
| 222 | DB_BINARY=$(getDBBinary) |
| 223 | DB_TYPE=$(detectDBType) |
| 224 | |
| 225 | if [ -z "$DB_BINARY" ]; then |
| 226 | _error "No database client found (mysql or mariadb)" |
| 227 | exit 1 |
| 228 | fi |
| 229 | |
| 230 | ROOTACCESS= |
| 231 | ROOTPASSWORD= |
| 232 | CHARSET='utf8mb4'; |
| 233 | DBNAME= |
| 234 | USERNAME= |
| 235 | USERHOST='localhost' |
| 236 | PASSWORD=$(generatePassword); |
| 237 | |
| 238 | function main() |
| 239 | { |
| 240 | _printPoweredBy |
| 241 | |
| 242 | getCredentials |
| 243 | |
| 244 | echo "" |
| 245 | echo "${_bold}${_blue}################################################################${_reset}" |
| 246 | echo "" |
| 247 | |
| 248 | create |
| 249 | printSuccessMessage |
| 250 | } |
| 251 | |
| 252 | main |
| 253 | |
| 254 | exit 0 |