I have two scripts - The main one that does a few different things and calls the second script, and the second script that installs MySQL.
From my main script I do something like this:
...
read -p "Set the password for the database [min. 4 characters]: " MPASS
# Install MySQL
path/to/setup-mysql.sh "$MPASS"
mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO root@'${IPADDRESS}' IDENTIFIED BY '${MPASS}';"
mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO root@'%' IDENTIFIED BY '${MPASS}';"
service mysql restart
mysql --user="root" --password=${MPASS} -e "SET GLOBAL validate_password_policy = 'LOW'; SET GLOBAL validate_password_length = 4; CREATE USER 'johndoe'@'${IPADDRESS}' IDENTIFIED BY '${MPASS}';"
mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO 'johndoe'@'${IPADDRESS}' IDENTIFIED BY '${MPASS}' WITH GRANT OPTION;"
mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO 'johndoe'@'%' IDENTIFIED BY '${MPASS}' WITH GRANT OPTION;"
mysql --user="root" --password=${MPASS} -e "FLUSH PRIVILEGES;"
And setup-mysql.sh
looks like this:
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive
debconf-set-selections <<< "mysql-community-server mysql-community-server/data-dir select ''"
debconf-set-selections <<< "mysql-community-server mysql-community-server/root-pass password ${1}"
debconf-set-selections <<< "mysql-community-server mysql-community-server/re-root-pass password ${1}"
apt-get install -y mysql-server
# Start mysql on boot
update-rc.d mysql defaults
# Configure Password Expiration
echo "default_password_lifetime = 0" >> /etc/mysql/my.cnf
# Configure Access Permissions For Root
sed -i '/^bind-address/s/bind-address.*=.*/bind-address = */' /etc/mysql/my.cnf
To me this looks like it should work, however, when bash executes this line:
mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO root@'${IPADDRESS}' IDENTIFIED BY '${MPASS}';"
It says that the password is wrong. When I try logging in manually it wont work. And when I skip the password field (mysql -u root
) it works. So the password is not being set at all.
Also, when I do echo $1
in setup-mysql.sh
, it properly shows what MPASS
contains.
Why isn't debconf-set-selections
setting the password properly for my MySQL installation. I'm really confused here.