Last active
June 10, 2019 13:22
-
-
Save mouradev/8077273480a934003fa44c9b97673051 to your computer and use it in GitHub Desktop.
This script can help to create a mysql database, a user and import a sql file.
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 | |
# Bash script written by Lucas Moura mouradev.com | |
mysql_host="localhost" | |
read -p "Please enter the MySQL root user: " db_user | |
read -p "Please enter root user MySQL password: " root_password | |
read -p "Do you want to create a new database? (y/n): " create_db | |
if [ "$create_db" != "${create_db#[Yy]}" ] ;then | |
read -p "Please enter the NAME of the new database!: " db_name | |
echo "Creating new database..." | |
mysql -u${db_user} -p${root_password} -e "CREATE DATABASE ${db_name};" | |
echo "Database successfully created!" | |
echo "Showing existing databases..." | |
mysql -u${db_user} -p${root_password} -e "show databases;" | |
else | |
read -p "Please enter the NAME of the database that you want to use: " db_name | |
fi | |
echo "" | |
read -p "Do you want to create a new MySQL user? (y/n) " create_user | |
if [ "$create_user" != "${create_user#[Yy]}" ] ;then | |
read -p "Please enter the NAME of the new database user: " username | |
read -p "Please enter the PASSWORD for the new database user: " user_password | |
echo "Creating new user..." | |
mysql -u${db_user} -p${root_password} -e "CREATE USER ${username}@${mysql_host} IDENTIFIED BY '${user_password}';" | |
echo "User successfully created!" | |
echo "" | |
echo "Granting ALL privileges on ${db_name} to ${username}!" | |
mysql -u${db_user} -p${root_password} -e "GRANT ALL PRIVILEGES ON ${db_name}.* TO '${username}'@'${mysql_host}';" | |
mysql -u${db_user} -p${root_password} -e "FLUSH PRIVILEGES;" | |
fi | |
read -p "Do you want to import a sql file to ${db_name}? (y/n) " import_sql | |
if [ "$import_sql" != "${import_sql#[Yy]}" ] ;then | |
echo "=== Importing the sql file to ${db_name} ===" | |
read -p "Please enter the sql filename: " sql_file_name | |
mysql -u${db_user} -p${root_password} ${db_name} < ${sql_file_name} | |
echo "showing the tables" | |
mysql -u${db_user} -p${root_password} ${db_name} -e "SHOW TABLES" | |
fi | |
echo "Your database is ready :)" | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do not forget to give execute permission to this file
chmod +x install_database.sh