如何将密码从 bash 脚本传递给 aptitude 以安装 mysql?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1202347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I pass a password from a bash script to aptitude for installing mysql?
提问by
I am writing a simple bash script to install MySQL on Ubuntu.
我正在编写一个简单的 bash 脚本来在 Ubuntu 上安装 MySQL。
#!/bin/bash
apt-get update
# Install MySQL5
aptitude -y install mysql-server mysql-client libmysqlclient15-dev
However MySQL prompts for a password and confirmation. How do I pass along a root password. Is there an echo I can use?
但是 MySQL 会提示输入密码并进行确认。如何传递 root 密码。有我可以使用的回声吗?
回答by Shears
This is so much easier ..
这容易多了..
install mysql on ubuntu without password prompt
sudo debconf-set-selections <<< 'mysql-server-5.1 mysql-server/root_password password your_password'
sudo debconf-set-selections <<< 'mysql-server-5.1 mysql-server/root_password_again password your_password'
sudo apt-get -y install mysql-server
If your shell doesn't support here-strings(zsh, ksh93 and bash support them), use:
如果您的 shell 不支持here-strings(zsh、ksh93 和 bash 支持它们),请使用:
echo ... | sudo debconf-set-selections
回答by Shears
Thank you for the tip on expect. I couldn't find anything by searching Ubuntu admin forums so I went with expect. As you can see by the timestamp of this post, it took me 3 hours to get it working. Here is the code, I hope it can help someone:
谢谢你的期望提示。我通过搜索 Ubuntu 管理论坛找不到任何东西,所以我期待。正如您从这篇文章的时间戳所看到的,我花了 3 个小时才让它工作。这是代码,我希望它可以帮助某人:
#!/bin/bash
apt-get update
apt-get install expect
VAR=$(expect -c '
spawn apt-get -y install mysql-server
expect "New password for the MySQL \"root\" user:"
send "PasswordHere\r"
expect "Repeat password for the MySQL \"root\" user:"
send "PasswordHere\r"
expect eof
')
echo "$VAR"
apt-get -y install mysql-client libmysqlclient15-dev
#For some reason important to restart - otherwise possible errors
/etc/init.d/mysql stop
/etc/init.d/mysql start
回答by Mnebuerquo
This is an excerpt from my setup script for new servers. You should be able to copy it word-for-word except for the password.
这是我的新服务器设置脚本的摘录。除了密码之外,您应该能够逐字复制它。
You'll need to run this using sudo if you're not already root.
如果您还不是 root,则需要使用 sudo 运行它。
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive
apt-get -q -y install mysql-server
echo "Give mysql server time to start up before we try to set a password..."
sleep 5
mysql -uroot -e <<EOSQL "UPDATE mysql.user SET Password=PASSWORD('yourpasswordhere') WHERE User='root'; FLUSH PRIVILEGES;"
EOSQL
echo "Done setting mysql password."
Other answers have used the -y which makes apt-get always answer yes to questions. The -q hides some progress indicators so you can send the output to a log. You could also use -qq, which automatically gives you a -y. This is in the man page for apt-get.
其他答案使用了 -y 这使得 apt-get 总是对问题回答是。-q 隐藏了一些进度指示器,以便您可以将输出发送到日志。您也可以使用 -qq,它会自动为您提供 -y。这是 apt-get 的手册页。
The <<EOSQLis a bash heredoc syntax for readability.
这<<EOSQL是为了提高可读性的 bash heredoc 语法。
I got the heredoc part of this solution from this guy: http://padwasabimasala.posterous.com/non-interactive-scripted-mysql-install-on-ubu
我从这个人那里得到了这个解决方案的 heredoc 部分:http: //padwasabimasala.posterous.com/non-interactive-scripted-mysql-install-on-ubu
The thing to remember with the heredoc is that whitespace before the closing string breaks it. So don't indent that line. Here is a page about the heredoc syntax: http://tldp.org/LDP/abs/html/here-docs.html
使用heredoc 要记住的事情是在结束字符串打破它之前的空白。所以不要缩进那一行。这是关于 heredoc 语法的页面:http: //tldp.org/LDP/abs/html/here-docs.html
回答by cstar
The easiest way to do this is to use the DEBIAN_FRONTEND environment variable and the aptitude -q and -y flags:
最简单的方法是使用 DEBIAN_FRONTEND 环境变量和 aptitude -q 和 -y 标志:
sudo DEBIAN_FRONTEND=noninteractive aptitude install -q -y mysql-server
Or more generically, assuming sudo password has been catered for some-how:
或者更笼统地说,假设 sudo 密码已满足某种方式:
#!/bin/bash
INSTALLER_LOG=/var/log/my-installer.log
installnoninteractive(){
sudo bash -c "DEBIAN_FRONTEND=noninteractive aptitude install -q -y $* >> $INSTALLER_LOG"
}
installnoninteractive mysql-server
回答by Mickael L
This script was successful when launched as superuser:
此脚本在以超级用户身份启动时成功:
#!/bin/bash
export temp_mysql_password="**********"
export DEBIAN_FRONTEND=noninteractive
debconf-set-selections <<< "mysql-server mysql-server/root_password password $temp_mysql_password"
debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $temp_mysql_password"
debconf-set-selections <<< "mysql-server-5.6 mysql-server/root_password password $temp_mysql_password"
debconf-set-selections <<< "mysql-server-5.6 mysql-server/root_password_again password $temp_mysql_password"
apt-get update
apt-get -y upgrade
apt-get -y install mysql-server
回答by ennuikiller
回答by Dirk Eddelbuettel
Expect is probably overkill. Look on one of the Debian or Ubuntu admin forums -- things like FAI et al have long used preseeding for debconf questions. You should be able to use that here too.
期望可能是矫枉过正。查看 Debian 或 Ubuntu 管理论坛之一——像 FAI 等人长期以来一直使用预置来解决 debconf 问题。你也应该可以在这里使用它。
Lastly, you could probably also use apt-get itself or other frontends.
最后,您可能还可以使用 apt-get 本身或其他前端。

