Bash and MySQL

Hello Folks,

I am having trouble running mysql from within bash.

I have tried <<MYQUERY method and -e method, nothing seems to work.

I am trying to use it to update the default password on the root account. It seems when you install mysql-server, it installs with a root password. Not unless I’m missing something…

To get round this I have been inserting skip-grant-tables in the my.cnf file then changing the root password with an UPDATE statement to mysql.user

= this works, but I am trying to automate this. It will not work during a sudo bash sh run

Thank you in advance.

Never mind. Fixed the issue.

MySQL installs secure, meaning that I wasn’t suppose to be even trying to change the root password.

If you login to the root account it authenticates the user rather than the password. So going into sudo su solved the issue of logging in. From there I just created a new user with the same permissions.

#!/bin/bash
sudo su <<BEEPBOOP
apt update
apt upgrade -y
apt install mysql-server -y
echo "************************MYSQL INSTALLED************************"
mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' IDENTIFIED BY 'password'; FLUSH PRIVILEGES;"
echo "************************MYSQL USER CREATED************************"
BEEPBOOP

This then gives you a user called admin with the password set to just password.

Now if you

mysql -u admin -p

You can just login using password and do all the actions of root.