Last active
May 19, 2026 10:46
-
-
Save bigprof/307ed9e84ac37f45ecb83fe6fc67b178 to your computer and use it in GitHub Desktop.
Interactive CLI wizard for Windows to create a MySQL/MariaDB user with a password and full privileges on a specified database. No SQL knowledge required — just run and follow the prompts. Auto-creates the database if it doesn't exist.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @echo off | |
| setlocal enabledelayedexpansion | |
| REM Check if mysql command exists | |
| where mysql >nul 2>&1 | |
| if errorlevel 1 ( | |
| echo MySQL command not found in PATH. | |
| set /p MYSQL_PATH="Please provide the full path to mysql.exe: " | |
| if not exist "!MYSQL_PATH!" ( | |
| echo Error: MySQL executable not found at !MYSQL_PATH! | |
| exit /b 1 | |
| ) | |
| set MYSQL_CMD=!MYSQL_PATH! | |
| ) else ( | |
| set MYSQL_CMD=mysql | |
| ) | |
| REM Ask for MySQL username (default: root) | |
| set /p MYSQL_USER="Enter MySQL username [root]: " | |
| if "!MYSQL_USER!"=="" set MYSQL_USER=root | |
| REM Ask for MySQL password (default: empty) | |
| echo. | |
| echo Enter MySQL password (press Enter for no password): | |
| REM Using a workaround to read password without echoing | |
| for /f %%A in ('copy /Z "%~f0" nul') do set "BS=%%A" | |
| set MYSQL_PASS= | |
| setlocal DisableDelayedExpansion | |
| set /p MYSQL_PASS="Password: " | |
| setlocal enabledelayedexpansion | |
| REM Set password argument - only include -p if password is not empty | |
| if "!MYSQL_PASS!"=="" ( | |
| set MYSQL_PASS_ARG= | |
| ) else ( | |
| set MYSQL_PASS_ARG=-p!MYSQL_PASS! | |
| ) | |
| REM Connect to MySQL and get list of databases | |
| echo. | |
| echo Connecting to MySQL... | |
| for /f "tokens=*" %%i in ('%MYSQL_CMD% -u!MYSQL_USER! !MYSQL_PASS_ARG! -e "SHOW DATABASES;" 2^>nul') do ( | |
| set "DB_LIST=%%i" | |
| ) | |
| REM Check if connection was successful | |
| %MYSQL_CMD% -u!MYSQL_USER! !MYSQL_PASS_ARG! -e "SELECT 1;" >nul 2>&1 | |
| if errorlevel 1 ( | |
| echo Error: Failed to connect to MySQL. Please check your username and password. | |
| exit /b 1 | |
| ) | |
| echo Connected successfully! | |
| echo. | |
| echo Available databases: | |
| %MYSQL_CMD% -u!MYSQL_USER! !MYSQL_PASS_ARG! -e "SHOW DATABASES;" | |
| REM Ask for new user details | |
| echo. | |
| set /p NEW_USER="Enter new MySQL username to create: " | |
| if "!NEW_USER!"=="" ( | |
| echo Error: Username cannot be empty. | |
| exit /b 1 | |
| ) | |
| set /p NEW_PASS="Enter password for new user: " | |
| if "!NEW_PASS!"=="" ( | |
| echo Error: Password cannot be empty. | |
| exit /b 1 | |
| ) | |
| set /p NEW_DB="Enter database name to grant privileges on: " | |
| if "!NEW_DB!"=="" ( | |
| echo Error: Database name cannot be empty. | |
| exit /b 1 | |
| ) | |
| REM Create database if it doesn't exist | |
| echo. | |
| echo Creating database if it doesn't exist... | |
| %MYSQL_CMD% -u!MYSQL_USER! !MYSQL_PASS_ARG! -e "CREATE DATABASE IF NOT EXISTS !NEW_DB!;" 2>nul | |
| if errorlevel 1 ( | |
| echo Error: Failed to create database. | |
| exit /b 1 | |
| ) | |
| REM Create user and grant privileges | |
| echo Creating user '!NEW_USER!' with access to database '!NEW_DB!'... | |
| %MYSQL_CMD% -u!MYSQL_USER! !MYSQL_PASS_ARG! -e "CREATE USER '!NEW_USER!'@'localhost' IDENTIFIED BY '!NEW_PASS!';" 2>nul | |
| if errorlevel 1 ( | |
| echo Error: Failed to create user. | |
| exit /b 1 | |
| ) | |
| %MYSQL_CMD% -u!MYSQL_USER! !MYSQL_PASS_ARG! -e "GRANT ALL PRIVILEGES ON !NEW_DB!.* TO '!NEW_USER!'@'localhost';" 2>nul | |
| if errorlevel 1 ( | |
| echo Error: Failed to grant privileges. | |
| exit /b 1 | |
| ) | |
| %MYSQL_CMD% -u!MYSQL_USER! !MYSQL_PASS_ARG! -e "FLUSH PRIVILEGES;" 2>nul | |
| if errorlevel 1 ( | |
| echo Error: Failed to flush privileges. | |
| exit /b 1 | |
| ) | |
| REM Verify user creation | |
| %MYSQL_CMD% -u!MYSQL_USER! !MYSQL_PASS_ARG! -e "SELECT User, Host FROM mysql.user WHERE User='!NEW_USER!';" >nul 2>&1 | |
| if errorlevel 1 ( | |
| echo Error: Failed to verify user creation. | |
| exit /b 1 | |
| ) | |
| echo. | |
| echo ======================================== | |
| echo SUCCESS! | |
| echo ======================================== | |
| echo User '!NEW_USER!' has been created with: | |
| echo - Password: !NEW_PASS! | |
| echo - Full privileges on database: !NEW_DB! | |
| echo - Host: localhost | |
| echo ======================================== | |
| exit /b 0 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More details and explanation at the blog post