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.
It seems that you are writing a MySQL post-installation initialization shell script.
In my personal opinion: change your shell script design method.
In your script, you use
mysql --user="root" --password=${MPASS}
prompt error info. You could use mysql paramater--defaults-file
, write mysql login userroot
and corresponding password into a temp file likesuppose file path is
/tmp/mysql.cnf
(assign variablemysql_my_cnf
)Then use
mysql --defaults-file="$mysql_my_cnf"
login, it will login successfully.Assign a variable to
mysql --defaults-file="$mysql_my_cnf"
, such asmysql_command
Old method
New method
You can try it.
PS
I wrote a MySQL and its variants installation and initialization shell script, it may be helpful for you.