Home > Database > Mysql Tutorial > How to Automate MySQL Installation on Ubuntu Without Password Prompts?

How to Automate MySQL Installation on Ubuntu Without Password Prompts?

Mary-Kate Olsen
Release: 2024-11-15 01:22:02
Original
858 people have browsed it

How to Automate MySQL Installation on Ubuntu Without Password Prompts?

Automating MySQL Installation on Ubuntu without Password Prompts

Installing MySQL server on Ubuntu using the command sudo apt-get install mysql requires a password to be entered manually. To streamline this process and eliminate the need for user interaction, a script can be created to provide the password automatically.

Script Syntax

The following script will install MySQL server and assign a custom password to the root user:

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

Explanation

  • sudo debconf-set-selections is used to set debconf configuration values.
  • The <<< operator redirects the following string to stdin.
  • mysql-server/root_password and mysql-server/root_password_again are the configuration keys.
  • your_password is the desired root password.

Specific Version Support

For specific MySQL versions, such as 5.6, specify the version in the configuration keys:

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 Server, the configuration 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
Copy after login

Alternative Shell Syntax

If your script is using a non-bash shell, use the following syntax to pass multiline strings:

echo ... | sudo debconf-set-selections 
Copy after login

Or, using heredocs (requires bash, zsh, or ksh93 shell):

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 if the configuration was set correctly, run the following command:

sudo debconf-get-selections | grep ^mysql
Copy after login

This should output the values set for the MySQL configuration keys.

The above is the detailed content of How to Automate MySQL Installation on Ubuntu Without Password Prompts?. 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