我正在写一个简单的bash脚本来在Ubuntu上安装MySQL。
#!/bin/bash apt-get update # Install MySQL5 aptitude -y install mysql-server mysql-client libmysqlclient15-dev
但是MySQL提示input密码并确认。 我如何传递一个root密码。 有没有我可以使用的回声?
这很容易..
在没有密码提示的情况下在Ubuntu上安装mysql
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
如果你的shell here-strings
不支持here-strings
(zsh,ksh93和bash支持它们),使用:
echo ... | sudo debconf-set-selections
感谢您期待的提示。 我找不到任何搜索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
这是从我的新服务器的安装脚本摘录。 除了密码,您应该可以逐字复制。
如果你还不是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."
其他答案已经使用了-y这使得apt-get总是回答是的问题。 -q隐藏了一些进度指示器,以便您可以将输出发送到日志。 你也可以使用-qq,它会自动给你一个-y。 这是在apt-get的man页面中。
<<EOSQL
是一种bash heredoc语法,用于提高可读性。
我从这个家伙得到了这个解决方案的heredoc部分: http : //padwasabimasala.posterous.com/non-interactive-scripted-mysql-install-on-ubu
用heredoc记住的事情是在关闭字符串之前的空白符将它打断。 所以不要缩进那一行。 这是一个关于heredoc语法的页面: http : //tldp.org/LDP/abs/html/here-docs.html
最简单的方法是使用DEBIAN_FRONTEND环境变量和aptitude -q和-y标志:
sudo DEBIAN_FRONTEND=noninteractive aptitude install -q -y mysql-server
或者更一般地说,假设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
看看使用期望
它可以用来自动化大多数交互式会话,虽然我不会使用root密码
以超级用户身份启动时,此脚本成功:
#!/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
期待可能是矫枉过正。 看看Debian或Ubuntu管理员论坛之一 – 像FAI等人一直使用preseeding debconf问题。 你也应该可以在这里使用。
最后,你也可以使用apt-get本身或其他前端。