本文翻译自:Install MySQL on Ubuntu without a password prompt
How do I write a script to install MySQL server on Ubuntu? 如何编写脚本在Ubuntu上安装MySQL服务器?
sudo apt-get install mysql
will install, but it will also ask for a password to be entered in the console. sudo apt-get install mysql
将sudo apt-get install mysql
,但它也会要求在控制台中输入密码。
How do I do this in a non-interactive way? 我如何以非交互方式执行此操作? That is, write a script that can provide the password? 也就是说,编写一个可以提供密码的脚本?
#!/bin/bash
sudo apt-get install mysql # To install MySQL server
# How to write script for assigning password to MySQL root user
# End
#1楼
参考:https://stackoom.com/question/WTQz/在没有密码提示的情况下在Ubuntu上安装MySQL
#2楼
Another way to make it work: 使其工作的另一种方法:
echo "mysql-server-5.5 mysql-server/root_password password root" | debconf-set-selections
echo "mysql-server-5.5 mysql-server/root_password_again password root" | debconf-set-selections
apt-get -y install mysql-server-5.5
Note that this simply sets the password to "root". 请注意,这只是将密码设置为“root”。 I could not get it to set a blank password using simple quotes ''
, but this solution was sufficient for me. 我无法使用简单的引号''
设置空白密码,但这个解决方案对我来说已经足够了。
Based on a solution here . 基于这里的解决方案。
#3楼
Use: 使用:
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server
sudo mysql -h127.0.0.1 -P3306 -uroot -e"UPDATE mysql.user SET password = PASSWORD('yourpassword') WHERE user = 'root'"
#4楼
This should do the trick 这应该可以解决问题
export DEBIAN_FRONTEND=noninteractive
sudo -E apt-get -q -y install mysql-server
Of course, it leaves you with a blank root password - so you'll want to run something like 当然,它会留下一个空白的root密码 - 所以你想要运行类似的东西
mysqladmin -u root password mysecretpasswordgoeshere
Afterwards to add a password to the account. 然后为帐户添加密码。
#5楼
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password your_password'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password your_password'
sudo apt-get -y install mysql-server
For specific versions, such as mysql-server-5.6
, you'll need to specify the version in like this: 对于特定版本,例如mysql-server-5.6
,您需要像这样指定版本:
sudo debconf-set-selections <<< 'mysql-server-5.6 mysql-server/root_password password your_password'
sudo debconf-set-selections <<< 'mysql-server-5.6 mysql-server/root_password_again password your_password'
sudo apt-get -y install mysql-server-5.6
For mysql-community-server, the keys are slightly different: 对于mysql-community-server,密钥略有不同:
sudo debconf-set-selections <<< 'mysql-community-server mysql-community-server/root-pass password your_password'
sudo debconf-set-selections <<< 'mysql-community-server mysql-community-server/re-root-pass password your_password'
sudo apt-get -y install mysql-community-server
Replace your_password with the desired root password. 将your_password替换为所需的root密码。 (it seems your_password can also be left blank for a blank root password.) (对于空白的root密码,似乎your_password也可以留空。)
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