Skip to content

Instantly share code, notes, and snippets.

@pietrorea
Created October 22, 2021 20:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pietrorea/823f2ecb2c0dfc55ed65f41a8be918ae to your computer and use it in GitHub Desktop.
Save pietrorea/823f2ecb2c0dfc55ed65f41a8be918ae to your computer and use it in GitHub Desktop.
Installs mysql 8, secures it, creates a user and db
#!/bin/bash
set -e
# Sets up an new myql instalation (Ubuntu 20.04)s
# 1. Downloads mysql-server if necessary
# 2. Secures the installation
# 3. Sets the root password
# 4. Creates an app related DB
# 5. Creats an app related user.
# Usage
# ./db.sh
APP_USER=CHANGEME
DB_NAME=CHANGEME
if [[ "$EUID" -ne 0 ]]; then
echo "Error: Please run as root with sudo."
exit
fi
echo "mysql root password:"
read -s ROOT_PASSWORD
echo
echo "Password for ${APP_USER}:"
read -s APP_USER_PASSWORD
echo
# Installation
apt-get install mysql-server
# mysql_secure_installation
mysql -u root <<EOF
SET PASSWORD FOR root@localhost = '${ROOT_PASSWORD}';
FLUSH PRIVILEGES;
EOF
mysql_secure_installation -u root --password="${ROOT_PASSWORD}" --use-default
# create DB
mysql -u root --password="${ROOT_PASSWORD}" <<EOF
CREATE DATABASE ${DB_NAME};
EOF
# create user
mysql -u root --password="${ROOT_PASSWORD}" <<EOF
CREATE USER '${APP_USER}'@'localhost' IDENTIFIED BY '${APP_USER_PASSWORD}';
GRANT ALL ON ${DB_NAME}.* TO '${APP_USER}'@'localhost'
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment