問題描述
如何編寫腳本在 Ubuntu 上安裝 MySQL 服務器?
How do I write a script to install MySQL server on Ubuntu?
sudo apt-get install mysql
會安裝,但也會要求在控制臺輸入密碼.
sudo apt-get install mysql
will install, but it will also ask for a password to be entered in the console.
如何以非交互式方式執行此操作?也就是寫個腳本可以提供密碼?
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
推薦答案
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
,您需要像這樣指定版本:
For specific versions, such as mysql-server-5.6
, you'll need to specify the version in like this:
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 略有不同:
For mysql-community-server, the keys are slightly different:
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 密碼留空.)
Replace your_password with the desired root password. (it seems your_password can also be left blank for a blank root password.)
如果您的 shell 不支持 here-strings(zsh、ksh93 和 bash 支持它們),使用:
If your shell doesn't support here-strings (zsh, ksh93 and bash support them), use:
echo ... | sudo debconf-set-selections
這篇關于在沒有密碼提示的情況下在 Ubuntu 上安裝 MySQL的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!