开发者

Install MySQL on Ubuntu without a password prompt

开发者 https://www.devze.com 2023-04-12 17:26 出处:网络
How do I write a script to install MySQL server on Ubuntu? sudo apt-get install mysql will install, but it will also ask for a password to be entered in the console.

How do I write a script to install MySQL server on Ubuntu?

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 s开发者_StackOverflow社区cript 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

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

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

Replace your_password with the desired root password. (it seems your_password can also be left blank for a blank root password.)

If you writing a script then your shell may be not bash but dash/ash or some basic unix shell. These shells, unlike zsh, ksh93 and bash, doesn't support here-strings <<<. So you should use:

echo ... | sudo debconf-set-selections 

Or a more readable print of multiline strings:

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

You can check with 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


This should do the trick

export DEBIAN_FRONTEND=noninteractive
sudo -E apt-get -q -y install mysql-server

Of course, it leaves you with a blank root password - so you'll want to run something like

mysqladmin -u root password mysecretpasswordgoeshere

Afterwards to add a password to the account.


Another way to make it work:

echo "mysql-server-5.5 mysql-server/root_password password root" | debconf-set-selections
echo "mysql-server-5.5 mysql-server/root_password_again password root" | debconf-set-selections
apt-get -y install mysql-server-5.5

Note that this simply sets the password to "root". I could not get it to set a blank password using simple quotes '', but this solution was sufficient for me.

Based on a solution here.


Use:

sudo DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server

sudo mysql -h127.0.0.1 -P3306 -uroot -e"UPDATE mysql.user SET password = PASSWORD('yourpassword') WHERE user = 'root'"
0

精彩评论

暂无评论...
验证码 换一张
取 消