在没有密码提示的情况下在 Ubuntu 上安装 MySQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7739645/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:19:01  来源:igfitidea点击:

Install MySQL on Ubuntu without a password prompt

mysqlbashubuntuapt

提问by Venkat

How do I write a script to install MySQL server on Ubuntu?

如何编写脚本在 Ubuntu 上安装 MySQL 服务器?

sudo apt-get install mysqlwill install, but it will also ask for a password to be entered in the console.

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

回答by Dimitre Radoulov

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. (it seems your_password can also be left blank for a blank root password.)

将 your_password 替换为所需的 root 密码。(似乎 your_password 也可以为空白 root 密码留空。)

If your shell doesn't support here-strings(zsh, ksh93and bashsupport them), use:

如果您的 shell 不支持here-stringszshksh93bash支持它们),请使用:

echo ... | sudo debconf-set-selections 

回答by Mez

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.

之后为帐户添加密码。

回答by chrisfargen

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". I could not get it to set a blank password using simple quotes '', but this solution was sufficient for me.

请注意,这只是将密码设置为“root”。我无法使用简单的引号设置空白密码'',但这个解决方案对我来说已经足够了。

Based on a solution here.

基于这里的解决方案。

回答by BALAJI POTHULA

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'"