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.

1
  • Just one quick note, debconf-utils is a package for Debian-based distributions only. That is the reason why the command is failing. Commented Nov 28, 2016 at 17:44

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

What about

echo  "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('secret');" | mysql -u root

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.