Skip to content

Instantly share code, notes, and snippets.

@Helianthella
Last active September 19, 2018 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Helianthella/a2fa9915a069d086fa67fb98d1678585 to your computer and use it in GitHub Desktop.
Save Helianthella/a2fa9915a069d086fa67fb98d1678585 to your computer and use it in GitHub Desktop.
@echo off
setlocal
echo This script will automatically install MariaDB and configure it for you.
echo You may interrupt the installation by pressing CTRL+C or closing this window.
echo.
pause
echo.
:: check windows version (we need a recent PowerShell version for chocolatey)
for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j
if "%version:~0,2%" NEQ "10" if "%version%" NEQ "6.3" if "%version%" NEQ "6.2" (
echo WARNING: this script was made for Windows 8, 8.1 and 10
echo It might not work with your version of Windows.
echo.
pause
echo.
)
:: check admin permissions
net session >nul 2>&1
if %errorLevel% NEQ 0 (
echo ERROR: admin permissions are required to install MariaDB.
echo please right-click on mariadb.bat and choose "Run as administrator"
pause >nul
goto END
)
:: check existing configuration
findstr /m "\"ragnarok\"" %~dp0\conf\global\sql_connection.conf >nul 2>&1
if %errorLevel% NEQ 0 (
echo WARNING: it seems you already configured the sql connection for your server.
echo If you decide to continue, your settings will be overwritten.
echo.
pause
echo.
)
:: install chocolatey
where /q choco
if %errorLevel% NEQ 0 (
"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" >nul 2>&1
set "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
)
:: install maria
for /f "delims=" %%i in ('where /R "%PROGRAMFILES%" mariabackup') do set installed=%%i
set installed=%installed:~0,-16%
if "x%installed:MariaDB=%" NEQ "x%installed%" (
echo WARNING: MariaDB is already installed!
echo If you decide to continue, your root password will be overwritten.
echo.
pause
echo.
) else (
choco install mariadb -y
for /f "delims=" %%i in ('where /R "%PROGRAMFILES%" mariabackup') do set installed=%%i
set installed=%installed:~0,-16%
)
:: set the new root password
set rootpw=%random%%random%%random%%random%
echo ALTER USER 'root'@'localhost' IDENTIFIED BY '%rootpw%'; > %~dp0\rootpw.sql
net stop MySQL >nul 2>&1
start "DO NOT CLOSE THIS == PLEASE WAIT ==" "%installed%\mysqld.exe" --console --init-file="%~dp0\rootpw.sql"
echo Please wait...
timeout /T 10 /NOBREAK >nul
"%installed%\mysqladmin.exe" -u root -p%rootpw% shutdown
timeout /T 4 /NOBREAK >nul
del %~dp0\rootpw.sql
echo.
net start MySQL
:: set up the new database
set userpw=%random%%random%%random%%random%
echo CREATE DATABASE IF NOT EXISTS hercules; > %~dp0\init.sql
echo DROP USER IF EXISTS 'hercules'@'localhost'; >> %~dp0\init.sql
echo DROP USER IF EXISTS 'hercules'@'127.0.0.1'; >> %~dp0\init.sql
echo CREATE USER 'hercules'@'localhost' IDENTIFIED BY '%userpw%'; >> %~dp0\init.sql
echo CREATE USER 'hercules'@'127.0.0.1' IDENTIFIED BY '%userpw%'; >> %~dp0\init.sql
echo GRANT ALTER,CREATE,SELECT,INSERT,UPDATE,DELETE,DROP,INDEX ON `hercules`.* TO 'hercules'@'localhost'; >> %~dp0\init.sql
echo GRANT ALTER,CREATE,SELECT,INSERT,UPDATE,DELETE,DROP,INDEX ON `hercules`.* TO 'hercules'@'127.0.0.1'; >> %~dp0\init.sql
echo FLUSH PRIVILEGES; >> %~dp0\init.sql
"%installed%\mysql.exe" -u root -p%rootpw% < %~dp0\init.sql
set "rootpw="
del %~dp0\init.sql
:: import the sql files
"%installed%\mysql.exe" -u hercules -p%userpw% hercules < %~dp0\sql-files\main.sql
"%installed%\mysql.exe" -u hercules -p%userpw% hercules < %~dp0\sql-files\logs.sql
:: update configuration files
echo sql_connection: { > %~dp0\conf\global\sql_connection.conf
echo db_username: "hercules" >> %~dp0\conf\global\sql_connection.conf
echo db_password: "%userpw%" >> %~dp0\conf\global\sql_connection.conf
echo db_database: "hercules" >> %~dp0\conf\global\sql_connection.conf
echo } >> %~dp0\conf\global\sql_connection.conf
echo.
echo.
echo All done! You should now be able to start using Hercules.
echo.
echo Press any key to finish . . .
pause >nul
:END
endlocal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment