mysql - 通过shell脚本为mysql设置第一次新密码

标签 mysql sh redhat

我正在尝试为本地系统编写一个 shell 脚本。操作系统是红帽。 我的脚本低于一个,除了从默认生成的密码设置新密码(从文件路径 /etc/.mysql_secret

获取)之外,一切正常
#!/usr/bin/sh

# MYSQL DEPLOYMENT

#variables
hostId="localhost"
newPasswrod="password"
newDatabase="mydb"
newUser="mymysql"

#install mysql with rpm files.
echo "installing mysql with rpm files."
rpm -ivh perl-Data-Dumper-2.145-3.el7.x86_64.rpm
rpm -ivh lib64numa1-2.0.12-1.mga7.x86_64.rpm
rpm -ivh libaio-0.3.109-13.el7.x86_64.rpm

tar xvf MySQL-5.6.46-1.el7.x86_64.rpm-bundle.tar
rpm -ivh MySQL-shared-5.6.46-1.el7.x86_64.rpm
rpm -ivh MySQL-shared-5.6.46-1.el7.x86_64.rpm
rpm -ivh MySQL-client-5.6.46-1.el7.x86_64.rpm

rpm -e --nodeps "mariadb-libs-5.5.64-1.el7.x86_64"

rpm -ivh MySQL-server-5.6.46-1.el7.x86_64.rpm

mysqlPasswordFile="/root/.mysql_secret"
echo "retrieve password from default location" 
#retrieve password from default location example with below parameter.
# The random password set for the root user at Mon Jan 13 11:00:59 2020 (local time): 4ug4P6drlfrN_vaE
mysqlPassword=` grep  '.*' $mysqlPasswordFile | cut -d : -f 4-`
mysqlPassword=`echo $mysqlPassword | sed 's/ *$//g'`
echo "default password is : $mysqlPassword"
# mysqlPassword=4ug4P6drlfrN_vaE 

# start mysql server
sudo service mysql start
echo "started mysql"

sleep 1s
# set new passwrd and create new user.
echo "set new passwrd and create new user"
if [ $mysqlPassword ] ; then
  mysql -u root -h "$hostId" -p"$mysqlPassword" -e "set password for root@'localhost'=password('$newPasswrod')"
  mysql -u root -h "$hostId" -p"$newPasswrod" -e "SHOW DATABASES"
  mysql -u root -h "$hostId" -p"$newPasswrod" -e "FLUSH PRIVILEGES;"
fi

当它要设置新密码时抛出以下错误

错误 1862 (HY000):您的密码已过期。要登录,您必须使用支持过期密码的客户端进行更改。

尝试通过以下方式直接设置新密码:-

#Default password 4ug4P6drlfrN_vaE
mysql -u root -p4ug4P6drlfrN_vaE 
set password for root@'localhost'=password('mysql');

我想通过shell脚本实现这个东西。

最佳答案

尝试使用这个--connect-expired-password

  "default_password_lifetime = 0" >> /etc/mysql/my.cnf
  mysql -u root -p<current dummy password> --connect-expired-password -e "SET PASSWORD = PASSWORD('new password');"

关于mysql - 通过shell脚本为mysql设置第一次新密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59842051/

相关文章:

在 Centos 上未使用 Shell cmd/脚本发送电子邮件

linux - antiword 在托管服务器上不起作用

python - AWS cli 描述 openshift 集群

linux - 脚本中的if-elif无法正常工作

mysql - 在 MySQL 中转置具有不同数据类型的数据库

php - 用户收到多封电子邮件通知

mysql - Spring 集成 : error while inseting records in database with jdbc outbound gateway

Bash shell 脚本在没有 Inotify 的情况下收到新文件时获取电子邮件

linux - 使用 ocf :pacemaker:ClusterMon and/or external-agent 监控起搏器集群

php - 无法弄清楚为什么 header 位置不适用于这种情况(php)