137 lines
3.4 KiB
Bash
Executable File
137 lines
3.4 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# MySQL Setup Script for Bike Service Application
|
|
# This script will create the database and user for the application
|
|
|
|
set -e
|
|
|
|
echo "🗄️ Setting up MySQL database for Bike Service Application..."
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m' # No Color
|
|
|
|
print_status() {
|
|
echo -e "${GREEN}[INFO]${NC} $1"
|
|
}
|
|
|
|
print_warning() {
|
|
echo -e "${YELLOW}[WARNING]${NC} $1"
|
|
}
|
|
|
|
print_error() {
|
|
echo -e "${RED}[ERROR]${NC} $1"
|
|
}
|
|
|
|
# Check if MySQL is running
|
|
if ! systemctl is-active --quiet mysql; then
|
|
print_error "MySQL is not running. Please start MySQL first:"
|
|
echo "sudo systemctl start mysql"
|
|
exit 1
|
|
fi
|
|
|
|
# Get database credentials
|
|
echo "Enter MySQL root password:"
|
|
read -s MYSQL_ROOT_PASSWORD
|
|
|
|
# Get application database credentials
|
|
echo "Enter database name (default: bikeservice_db):"
|
|
read DB_NAME
|
|
DB_NAME=${DB_NAME:-bikeservice_db}
|
|
|
|
echo "Enter database username (default: bikeservice_user):"
|
|
read DB_USER
|
|
DB_USER=${DB_USER:-bikeservice_user}
|
|
|
|
echo "Enter database password:"
|
|
read -s DB_PASSWORD
|
|
|
|
# Validate password strength
|
|
if [ ${#DB_PASSWORD} -lt 8 ]; then
|
|
print_warning "Password is less than 8 characters. Consider using a stronger password."
|
|
fi
|
|
|
|
print_status "Creating database and user..."
|
|
|
|
# Create SQL script
|
|
cat > /tmp/mysql_setup.sql << EOF
|
|
-- Create database
|
|
CREATE DATABASE IF NOT EXISTS ${DB_NAME}
|
|
CHARACTER SET utf8mb4
|
|
COLLATE utf8mb4_unicode_ci;
|
|
|
|
-- Create user
|
|
CREATE USER IF NOT EXISTS '${DB_USER}'@'localhost'
|
|
IDENTIFIED BY '${DB_PASSWORD}';
|
|
|
|
-- Grant privileges
|
|
GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'localhost';
|
|
|
|
-- Grant additional privileges for Laravel
|
|
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON ${DB_NAME}.* TO '${DB_USER}'@'localhost';
|
|
|
|
-- Flush privileges
|
|
FLUSH PRIVILEGES;
|
|
|
|
-- Show created database and user
|
|
SHOW DATABASES;
|
|
SELECT User, Host FROM mysql.user WHERE User = '${DB_USER}';
|
|
|
|
-- Display success message
|
|
SELECT 'Database and user created successfully!' as Status;
|
|
EOF
|
|
|
|
# Execute SQL script
|
|
if mysql -u root -p"$MYSQL_ROOT_PASSWORD" < /tmp/mysql_setup.sql; then
|
|
print_status "Database and user created successfully!"
|
|
|
|
# Create .env database configuration
|
|
cat > /tmp/database_config.env << EOF
|
|
# Database Configuration
|
|
DB_CONNECTION=mysql
|
|
DB_HOST=127.0.0.1
|
|
DB_PORT=3306
|
|
DB_DATABASE=${DB_NAME}
|
|
DB_USERNAME=${DB_USER}
|
|
DB_PASSWORD=${DB_PASSWORD}
|
|
EOF
|
|
|
|
print_status "Database configuration saved to /tmp/database_config.env"
|
|
print_warning "Copy these settings to your Laravel .env file:"
|
|
echo
|
|
cat /tmp/database_config.env
|
|
echo
|
|
|
|
# Test database connection
|
|
print_status "Testing database connection..."
|
|
if mysql -u "$DB_USER" -p"$DB_PASSWORD" -e "USE ${DB_NAME}; SELECT 1;" > /dev/null 2>&1; then
|
|
print_status "Database connection test successful!"
|
|
else
|
|
print_error "Database connection test failed!"
|
|
exit 1
|
|
fi
|
|
|
|
else
|
|
print_error "Failed to create database and user!"
|
|
exit 1
|
|
fi
|
|
|
|
# Clean up
|
|
rm -f /tmp/mysql_setup.sql
|
|
|
|
print_status "MySQL setup completed!"
|
|
echo
|
|
echo "Next steps:"
|
|
echo "1. Copy the database configuration to your Laravel .env file"
|
|
echo "2. Run Laravel migrations: php artisan migrate"
|
|
echo "3. Test your application"
|
|
echo
|
|
echo "Database credentials:"
|
|
echo "- Database: ${DB_NAME}"
|
|
echo "- Username: ${DB_USER}"
|
|
echo "- Password: [hidden]"
|
|
echo "- Host: 127.0.0.1"
|
|
echo "- Port: 3306"
|