Home > Database > Mysql Tutorial > How can I install MySQL on Ubuntu without a password prompt?

How can I install MySQL on Ubuntu without a password prompt?

Linda Hamilton
Release: 2024-11-15 22:27:02
Original
663 people have browsed it

How can I install MySQL on Ubuntu without a password prompt?

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
Copy after login

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
Copy after login

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
Copy after login

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 
Copy after login

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
Copy after login

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
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template