shell脚本读取mysql用户名和密码文件:
cat /etc/mysql/debian.cnf
password=$(awk '/password/{if(NR>=1 && NR<=5)print $3}' /etc/mysql/debian.cnf)
mysql登录语句
mysql -u debian-sys-maint -p$password
使用脚本登录并操作mysql命令
mysql -u debian-sys-maint -p$password <<EOF
ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxxx';
quit
EOF
操作的shell脚本:
#!/bin/bash
password='123456'
Newpassword='xxxxxx'
password=$(awk '/password/{if(NR>=1 && NR<=5)print $3}' /etc/mysql/debian.cnf)
mysql -u debian-sys-maint -p$password <<EOF
ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxxx';
quit
EOF
mysql -h 127.0.0.1 -u root -p$Newpassword <<EOF
........(其他操作)
quit
EOF