我有两个脚本——主要的一个做一些不同的事情并调用第二个脚本,第二个脚本安装 MySQL。
从我的主脚本中,我执行以下操作:
...
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;"
setup-mysql.sh
看起来像这样:
#!/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
对我来说,这看起来应该可以工作,但是,当 bash 执行这一行时:
mysql --user="root" --password=${MPASS} -e "GRANT ALL ON *.* TO root@'${IPADDRESS}' IDENTIFIED BY '${MPASS}';"
它说密码错误。当我尝试手动登录时,它不起作用。当我跳过密码字段 ( mysql -u root
) 时,它会起作用。所以根本没有设置密码。
另外,当我做echo $1
in时setup-mysql.sh
,它会正确显示MPASS
包含的内容。
为什么没有debconf-set-selections
为我的 MySQL 安装正确设置密码。我在这里真的很困惑。