问题描述
我试图在没有密码提示的情况下在Ubuntu Natty上安装MySQL。但是,在主安装后的某个阶段,我总是不断提示输入密码。
另外,当我输入我认为应该是我的密码(mymysqlpass)的密码时,它会给我一个拒绝访问的通知。然后,当脚本终止时,我可以不使用密码即mysql -uroot登录mysql,这应该不会发生。
#!/bin/bash
#This script installs mysql (latest build)
#Install MYSQL Server
mysql_pass=mymysqlpass
export DEBIAN_FRONTEND=noninteractive
debconf-set-selections <<< 'mysql-server-5.1 mysql-server/root_password password '$mysql_pass''
debconf-set-selections <<< 'mysql-server-5.1 mysql-server/root_password_again password '$mysql_pass''
apt-get -y install mysql-server
#Configure Password and Settings for Remote Access
cp /etc/mysql/my.cnf /etc/mysql/my.bak.cnf
ip=`ifconfig eth0 | grep "inet addr"| cut -d ":" -f2 | cut -d " " -f1` ; sed -i "s/\(bind-address[\t ]*\)=.*/\1= $ip/" /etc/mysql/my.cnf
mysql -uroot -e "UPDATE mysql.user SET Password=PASSWORD('"$mysql_pass"') WHERE User='root'; FLUSH PRIVILEGES;"
sleep 10
mysql -uroot -p$mysql_pass -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '"$mysql_pass"'; FLUSH PRIVILEGES;"
#Restart
service mysql restart
echo "MySQL Installation and Configuration is Complete."
最佳解决办法
安装mysql-server
软件包时,以下命令将MySQL根密码设置为strangehat
。
echo "mysql-server mysql-server/root_password password strangehat" | sudo debconf-set-selections
echo "mysql-server mysql-server/root_password_again password strangehat" | sudo debconf-set-selections
请注意,这将在/var/cache/debconf/passwords.dat
中创建密码的明文副本(通常只有root用户可以读取,并且成功安装mysql-server
软件包后,软件包管理系统会删除该密码)。
如果在Dockerfile中使用引号,请确保使用引号。
现在,您可以安装mysql-server
,并且不会出现密码提示:
sudo apt-get install mysql-server
次佳解决办法
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password my_password'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password my_password'
sudo apt-get -y install mysql-server
这将在没有任何干预的情况下安装mysql
第三种解决办法
这可能会使它不提示您:
export DEBIAN_FRONTEND=noninteractive
至于脚本,我会尝试将密码放在引号中:
mysql_pass="mymysqlpass"