5

My OS is CentOS 6.6 and I want to know how to install mysql-server automatically via shell script.

I have found that there is a topic talked about the same issue but it failed on CentOS 6: install mysql on ubuntu without password prompt

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'

The error message is

bash: debconf-set-selections: command not found

Here is my basic script

#!/bin/sh
# Make sure only root can run our script
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

#password setting
root_password='abc123'

#install mysql-server
yum install mysql mysql-server 

#start service
service mysqld start

#MySQL Initial Setup
mysql_secure_installation

#This command needs root password and answers(y/n) for secure questions 

Does there have any methods to automatically fill the root password and answer 'y' to most of the secure question.

Or it have alternative way to achieve the same thing. That is, a script to install myaql-server automatically and without password prompt.

very appreciated for your help.

Community
  • 1
  • 1
austinc
  • 285
  • 2
  • 5
  • 15
  • Just one quick note, debconf-utils is a package for Debian-based distributions only. That is the reason why the command is failing. – Luis Bosquez Nov 28 '16 at 17:44

3 Answers3

3

mysql_secure_installation is a shell script. so you can edit set_root_password() function in the script or any other user interactive sections.

1

What about

echo  "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('secret');" | mysql -u root
Andreas
  • 2,211
  • 1
  • 18
  • 36
1

This code works for me on vagrantfile:

password_match=`awk '/A temporary password is generated for/ {a=$0} END{ print a }' /var/log/mysqld.log | awk '{print $(NF)}'`
mysql -u root -p$password_match --connect-expired-password -e "SET password FOR root@localhost=password('$password_match');"
mysql -u root -p$password_match -e "SHOW VARIABLES LIKE 'validate_password%';"
mysql -u root -p$password_match -e "SET GLOBAL validate_password_length=4;"
mysql -u root -p$password_match -e "SET GLOBAL validate_password_policy=LOW;"
mysql -u root -p$password_match -e "SHOW VARIABLES LIKE 'validate_password%';"
mysql -u root -p$password_match -e "SET password for root@localhost=password('root');"
mysql -u root -proot -e "flush privileges;"

echo "character-set-server=utf8" >> /etc/my.cnf
echo "default_password_lifetime=0" >> /etc/my.cnf
echo "validate_password_policy=LOW" >> /etc/my.cnf
echo "validate_password_length=4" >> /etc/my.cnf

systemctl restart mysqld
Gregory Iyama
  • 139
  • 1
  • 3