如何编写脚本在 Ubuntu 上安装 MySQL 服务器?
sudo apt-get install 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,您需要像这样指定版本:
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,键值略有不同:
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 ( zsh 、 ksh93 和 bash 支持它们),请使用:
echo ... | sudo debconf-set-selections