123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- #!/bin/bash
-
-
- read -sp "Enter MySQL root password: " MYSQL_PASSWORD
- echo
-
- DEFAULT_INSTANCENAME="cihsr"
- echo "Enter instance name (default: $DEFAULT_INSTANCENAME):"
- read instancename
- INSTANCENAME=${instancename:-$DEFAULT_INSTANCENAME}
-
-
-
- read -p "Enter the database name (default: cihsr): " DB_NAME
- DB_NAME=${DB_NAME:-$INSTANCENAME}
-
-
- SQL_FILE="elxr/create.mysql.db.sql"
-
-
- if [[ ! -f "$SQL_FILE" ]]; then
- echo "SQL file '$SQL_FILE' does not exist in the current directory. Please check the file path and try again."
- exit 1
- fi
-
-
- MYSQL_USER="root"
-
-
- DB_EXISTS=$(mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES LIKE '$DB_NAME';")
-
- if [[ -z "$DB_EXISTS" ]]; then
-
- echo "Database '$DB_NAME' does not exist. Creating database..."
- mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "CREATE DATABASE IF NOT EXISTS $DB_NAME;"
- fi
-
-
- TEMP_SQL_FILE=$(mktemp)
- sed "s/{{DB_NAME}}/$DB_NAME/g" "$SQL_FILE" > "$TEMP_SQL_FILE"
-
-
- mysql -u $MYSQL_USER -p$MYSQL_PASSWORD $DB_NAME < "$TEMP_SQL_FILE"
-
-
- rm "$TEMP_SQL_FILE"
-
- mysql -u $MYSQL_USER -p$MYSQL_PASSWORD $DB_NAME < $SQL_FILE -- Create Schema
- mysql -u $MYSQL_USER -p$MYSQL_PASSWORD $DB_NAME < $DB_NAME.sql -- Load data
-
- echo "SQL script executed successfully on database $DB_NAME"
|