-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_installer.sh
74 lines (59 loc) · 2.1 KB
/
mysql_installer.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
# MySQL Auto Installer for Ubuntu 22.04 https://github.com/CodeMeZone/mysql-auto-installer-ubuntu
# Check if script is run as root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root or using sudo"
exit 1
fi
# Function to print section headers
print_header() {
echo "===================================="
echo "$1"
echo "===================================="
}
# Update system
print_header "Updating System"
apt update && apt upgrade -y
# Install MySQL Server
print_header "Installing MySQL Server"
apt install mysql-server -y
# Start MySQL service
systemctl start mysql.service
# Secure MySQL installation
print_header "Securing MySQL Installation"
# Generate a random root password
ROOT_PASSWORD=$(openssl rand -base64 12)
# Set root password and remove anonymous users
mysql --user=root <<_EOF_ 2>/dev/null
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '${ROOT_PASSWORD}';
DELETE FROM mysql.user WHERE User='';
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
FLUSH PRIVILEGES;
_EOF_
# Remove test database
mysql --user=root --password="${ROOT_PASSWORD}" -e "DROP DATABASE IF EXISTS test;" 2>/dev/null
# Prompt for new database and user creation
read -p "Enter new database name: " DB_NAME
read -p "Enter new database username: " DB_USER
DB_PASS=$(openssl rand -base64 12)
# Create new database and user
print_header "Creating New Database and User"
mysql --user=root --password="${ROOT_PASSWORD}" <<_EOF_ 2>/dev/null
CREATE DATABASE ${DB_NAME};
CREATE USER '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PASS}';
GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'localhost';
FLUSH PRIVILEGES;
_EOF_
# Print installation summary
print_header "Installation Complete"
echo "MySQL has been installed and secured."
echo "Root password: ${ROOT_PASSWORD}"
echo "New database: ${DB_NAME}"
echo "New user: ${DB_USER}"
echo "New user password: ${DB_PASS}"
echo ""
echo "Please save these credentials securely and delete this script."
# Restart MySQL service
systemctl restart mysql.service
print_header "MySQL Status"
systemctl status mysql.service