脚本通过CentOS安装没有密码提示的mysql-server

时间:2015-08-04 09:12:36

标签: mysql linux bash shell centos

我的操作系统是CentOS 6.6,我想知道如何通过shell脚本自动安装mysql-server。

我发现有一个话题谈到同一个问题,但它在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'

错误消息是

bash: debconf-set-selections: command not found

这是我的基本脚本

#!/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 

是否有任何方法可以自动填写root密码并回答&#39; y&#39;对大多数安全问题。

或者它有另一种方法来实现同样的目的。也就是说,一个自动安装myaql-server而没有密码提示的脚本。

非常感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

mysql_secure_installation是一个shell脚本。 所以你可以在脚本或任何其他用户交互部分编辑set_root_password()函数。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

此代码在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