Created
November 15, 2024 06:00
-
-
Save ilovefreesw/0b1a7da8e17497fa0a09a24ea517176b to your computer and use it in GitHub Desktop.
A shell script to install MySQL server on Ubuntu VPS with specified user and password.
This file contains 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 | |
# Check if MySQL is already installed | |
if ! command -v mysql &> /dev/null; then | |
echo "MySQL is not installed. Installing MySQL Server..." | |
apt update | |
apt install -y mysql-server | |
else | |
echo "MySQL is already installed." | |
fi | |
# Run MySQL secure installation (optional step to improve security) | |
echo "Running MySQL secure installation..." | |
mysql_secure_installation | |
# Read user input for MySQL username and password | |
read -p "Enter the MySQL username you want to create: " MYSQL_USER | |
read -sp "Enter the password for $MYSQL_USER: " MYSQL_PASSWORD | |
echo | |
# Log in to MySQL and create the user with full privileges | |
echo "Creating MySQL user and granting privileges..." | |
mysql -e " | |
CREATE USER IF NOT EXISTS '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD'; | |
GRANT ALL PRIVILEGES ON *.* TO '$MYSQL_USER'@'%' WITH GRANT OPTION; | |
FLUSH PRIVILEGES; | |
" | |
# Allow remote connections by updating the bind address | |
echo "Configuring MySQL to allow remote connections..." | |
sed -i "s/^bind-address\s*=.*/bind-address = 0.0.0.0/" /etc/mysql/mysql.conf.d/mysqld.cnf | |
# Restart MySQL service to apply changes | |
echo "Restarting MySQL service..." | |
systemctl restart mysql | |
# Confirm changes | |
echo "MySQL setup complete. User '$MYSQL_USER' with remote access has been created." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Explanation of the Script
mysql_secure_installation
to help secure the MySQL setup.'%'
).bind-address
to0.0.0.0
, allowing connections from any IP address.Usage
mysql_installer.sh
.This script will install MySQL, create a user with full remote access, and configure MySQL to accept remote connections.