Creating a user

Next, you will learn about creating a new user using the MySQL command inside shell script. The CREATE USER command creates a new user and we set a password for the newly created user. The command GRANT ALL ON provides privileges to make changes to database by newly created user.

In the next script, we will create a user and we will also grant all of the privileges on the database. Let's create the script mysql_04.sh to create a user:

#!/bin/bash 
mysql -u root -pTraining2@^ <<MY_QUERY 
CREATE USER 'user1'@'localhost' IDENTIFIED BY 'Test623@!@!'; 
GRANT ALL ON testdb.* TO 'user1'@'localhost'; 
select user from mysql.user; 
MY_QUERY 

Save the script and run it as follows:

                    $ chmod +x mysql_04.sh
          $ ./mysql_04.sh  

The output will ...

Get Learning Linux Shell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.