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

标签 mysql bash ubuntu apt

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

sudo apt-get install mysql 将安装,但它还会要求在控制台中输入密码。

我如何以非交互方式执行此操作?也就是写一个可以提供密码的脚本?

#!/bin/bash
sudo apt-get install mysql  # To install MySQL server

# How to write script for assigning password to MySQL root user
# End

最佳答案

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

对于特定版本,例如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

对于 mysql-community-server, key 略有不同:

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

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

如果您的 shell 不支持 here-strings(zshksh93bash 支持它们) ,使用:

echo ... | sudo debconf-set-selections 

关于mysql - 在没有密码提示的情况下在 Ubuntu 上安装 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7739645/

相关文章:

java - 连接到 127.0.0.1 :5432 refused. 检查主机名和端口是否正确以及邮局管理员是否接受 TCP/IP 连接

linux - android studio avd 中的模拟器在 Pop 中非常慢! _os

mysql - 在以下情况下,一列与多列的 FULLTEXT 索引?

Bash:生成在父脚本退出时退出的子进程

bash - 如何通过Bash中的curl从谷歌驱动器下载大文件?

BASH MySQL 查询以逗号分隔的文件

php - for 循环,$i 等于同一循环中的两个不同值

mysql - 分配每个月Mysql中的所有行

c# - "Duplicate entry for key primary"在一台机器上而不是另一台机器上,具有相同的数据?

ubuntu - 在 Beaglebone Black 上读取 CPU 温度