如何在没有密码提示的情况下安装 MySQL?

如何在没有密码提示的情况下安装 MySQL?

我尝试在 Ubuntu Natty 上安装 MySQL,无需输入密码。然而,在主要安装完成后的某个阶段,系统不断提示我输入密码。

另外,当我输入我认为应该是我的密码 (mymysqlpass) 时,它会给我一个拒绝访问的通知。然后当脚本终止时,我可以不用密码登录 mysql,即 mysql -uroot,这不应该发生。

#!/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."

答案1

以下命令设置strangehat安装mysql-server包时的 MySQL 根密码。

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

答案2

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

答案3

这可能会让它不提示你:

export DEBIAN_FRONTEND=noninteractive

至于脚本,我会尝试将密码放在引号中:

mysql_pass="mymysqlpass"

答案4

如果您想将密码放在引号之间,则此部分需要重新表述:'mysql-server-5.1 mysql-server/root_password 密码'$mysql_pass''

到:

"mysql-server-5.1 mysql-server/root_password password '$mysql_pass'"

这对我有用(空的根密码):

sudo debconf-set-selections <<< "mysql-server-5.5 mysql-server/root_password password ''"
sudo debconf-set-selections <<< "mysql-server-5.5 mysql-server/root_password_again password ''"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password ''"
sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password ''"
export DEBIAN_FRONTEND=noninteractive
sudo -E apt-get install -y -q mysql-server libmysqlclient-dev

相关内容