Bash script for creating MySQL database by taking the input from the terminal
5/5 - (1 vote)

Last Updated on January 29, 2023 by Vikash Ekka

Here is a Bash script that can be used to create a new MySQL database by taking the input from the terminal:


# Prompt for the database name and credentials
read -p "Enter the database name: " db_name
read -p "Enter the username: " username
read -sp "Enter the password: " password

# Connect to MySQL
mysql -u root -p -e "CREATE DATABASE ${db_name};"

# Create a new user
mysql -u root -p -e "GRANT ALL PRIVILEGES ON ${db_name}.* TO '${username}'@'localhost' IDENTIFIED BY '${password}';"

# Flush privileges
mysql -u root -p -e "FLUSH PRIVILEGES;"

This script will prompt the user to enter the desired values for the database name, username and password. The read -sp command is used to read the password without displaying it on the screen.

This script will:

  1. Connect to MySQL with the root user
  2. Create a new database with the name specified in the db_name variable
  3. Create a new user with the name and password specified in the username and password variables, and grant all privileges on the new database to that user
  4. Flush the privileges to make sure the new user has access to the new database

You’ll need to have MySQL installed and running on your machine, and you’ll also need to have the mysql command-line client available in your path.

You can also add further functionality to the script such as checking if the database already exists or not before creating a new one, also you can add a function to import a dump file to the created database and more.

By Vikash Ekka

Hi All, My name is Vikash Ekka from India. I’m the founder and tech editor of I have completed my Graduation in BCA. I love to write technical articles like Windows, Linux & MAC Tutorials, Tips, Tricks, How To fix, Tutorials About Ethical Hacking & Cyber Security Guide, and Software Review. Currently, I have been working as an IT professional since 2018.

Leave a Reply

Your email address will not be published. Required fields are marked *