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


26

我试图在没有密码提示的情况下在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."

我会很危险的!你需要这个吗?
Pascal Fares 2013年

6
设置虚拟机时,一方面。如果您真的很担心,我敢肯定您可以做一些事情,例如创建一个随机的密码安全密码,并让脚本创建一个cron作业,以在流量很少且系统被阻塞时告诉您密码时间。只需确保在5秒钟内看着计算机,然后计算机消失即可。
trysis

OP表示“我不断收到提示输入密码的信息”,但在他的示例代码中,我们以两行开头debconf-set-selections可以避免这种情况,但由于我相信问题和示例代码是在两个不同的时间点进行编辑的,因此请降低投票率。问题不再有意义。
mastazi'5

Answers:


46

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

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

11
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


7

这可能会使它不提示您:

export DEBIAN_FRONTEND=noninteractive

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

mysql_pass="mymysqlpass"

1

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

至:

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

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

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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.