Non-Interactive Installation of MySQL on Ubuntu
The standard method of installing MySQL server on Ubuntu using sudo apt-get install mysql prompts for a password in the console, which can be inconvenient when writing scripts for automated installations.
Using debconf-set-selections
To avoid the password prompt during installation, you can use the debconf-set-selections command to preconfigure the password for the MySQL root user. This requires the following steps:
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
In this script, replace your_password with the desired root password. Note: it is also possible to leave your_password blank to set the root password to empty.
Specific MySQL Versions
If you need to install a specific version of MySQL, such as MySQL 5.6, specify the version in the debconf-set-selections command as follows:
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
For MySQL community editions, the debconf-set-selections keys differ slightly:
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
Here-Strings in Non-Bash Shells
Some shells, such as dash or ash, do not support here-strings. In these cases, use the following:
echo ... | sudo debconf-set-selections
Or, write multiline strings using:
cat <<< EOF | sudo debconf-set-selections mysql-server mysql-server/root_password password your_password mysql-server mysql-server/root_password_again password your_password EOF
Verification
To check the configured root password, use the debconf-get-selections command:
$ sudo debconf-get-selections | grep ^mysql mysql-server mysql-server/root_password_again password your_password mysql-server mysql-server/root_password password your_password
The above is the detailed content of How can I install MySQL on Ubuntu without a password prompt?. For more information, please follow other related articles on the PHP Chinese website!