1. 程式人生 > 其它 >shell--指令碼使用初始化使用者名稱和密碼登入mysql(8.0)並操作MySQL命令修改使用者密碼

shell--指令碼使用初始化使用者名稱和密碼登入mysql(8.0)並操作MySQL命令修改使用者密碼

技術標籤:shell

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