Created
October 22, 2021 20:01
-
-
Save pietrorea/823f2ecb2c0dfc55ed65f41a8be918ae to your computer and use it in GitHub Desktop.
Installs mysql 8, secures it, creates a user and db
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
#!/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