Maison  >  Article  >  base de données  >  Comment automatiser l'installation de MySQL sur Ubuntu sans invite de mot de passe ?

Comment automatiser l'installation de MySQL sur Ubuntu sans invite de mot de passe ?

Mary-Kate Olsen
Mary-Kate Olsenoriginal
2024-11-15 01:22:02773parcourir

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

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

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

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 

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

Verification

To check if the configuration was set correctly, run the following command:

sudo debconf-get-selections | grep ^mysql

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

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn