22 mysql bash installation ubuntu debian
我正在编写一个简单的bash脚本来在Ubuntu上安装MySQL.
#!/bin/bash
apt-get update
# Install MySQL5
aptitude -y install mysql-server mysql-client libmysqlclient15-dev
Run Code Online (Sandbox Code Playgroud)
但是MySQL提示输入密码和确认.如何传递root密码.我可以使用回音吗?
She*_*ars 14
这太容易了..
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
Run Code Online (Sandbox Code Playgroud)
如果你的shell不支持here-strings(zsh,ksh93和bash支持它们),请使用:
echo ... | sudo debconf-set-selections
Run Code Online (Sandbox Code Playgroud)
小智 12
感谢您对预期的提示.我通过搜索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
Run Code Online (Sandbox Code Playgroud)
这是我的新服务器安装脚本的摘录.您应该能够逐字复制,密码除外.
如果你还不是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."
Run Code Online (Sandbox Code Playgroud)
其他答案使用了-y,这使得apt-get总是回答问题.-q隐藏了一些进度指示器,因此您可以将输出发送到日志.您也可以使用-qq,它会自动为您提供-y.这是apt-get的手册页.
这<<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