---
title: "Directadmin修改Mysql Root密码脚本"
id: "341"
type: "post"
slug: "directadmin%e4%bf%ae%e6%94%b9mysql-root%e5%af%86%e7%a0%81%e8%84%9a%e6%9c%ac"
published_at: "2020-11-05T07:03:20+00:00"
modified_at: "2020-11-05T08:50:58+00:00"
url: "https://www.hitsword.org/2020/341.html"
markdown_url: "https://www.hitsword.org/2020/341.md"
excerpt: "[bash] #!/bin/sh random_pass() { PASS_LEN=`perl -le 'print int(rand(6))+9'` START_LEN=`perl -le 'print int(rand(8))+1'` END_LEN=$(expr ${PASS_LEN} - ${START_LEN}) SPECIAL_CHAR=`perl -le 'print map { (qw{@ ^ _ -})[rand 6] } 1'`; NUMERIC_CHAR=`perl -le 'print int(rand(10))'`; PASS_START=`perl -le \"print map+(A..Z,a..z,0..9)[rand 62],0..$START_LEN\"`; PASS_END=`perl -le \"print..."
taxonomy_category:
  - "Shell脚本"
taxonomy_post_tag:
  - "DirectAdmin"
---

# Directadmin修改Mysql Root密码脚本

```
#!/bin/sh
random_pass() {
        PASS_LEN=`perl -le 'print int(rand(6))+9'`
        START_LEN=`perl -le 'print int(rand(8))+1'`
        END_LEN=$(expr ${PASS_LEN} - ${START_LEN})
        SPECIAL_CHAR=`perl -le 'print map { (qw{@ ^ _ -})[rand 6] } 1'`;
        NUMERIC_CHAR=`perl -le 'print int(rand(10))'`;
        PASS_START=`perl -le "print map+(A..Z,a..z,0..9)[rand 62],0..$START_LEN"`;
        PASS_END=`perl -le "print map+(A..Z,a..z,0..9)[rand 62],0..$END_LEN"`;
        PASS=${PASS_START}${SPECIAL_CHAR}${NUMERIC_CHAR}${PASS_END}
        echo $PASS
}
#backup
cp /usr/local/directadmin/scripts/setup.txt /usr/local/directadmin/scripts/setup.txt.bak
cp /usr/local/directadmin/conf/my.cnf /usr/local/directadmin/conf/my.cnf.bak
cp /usr/local/directadmin/conf/mysql.conf /usr/local/directadmin/conf/mysql.conf.bak
#ResetPWD
source /usr/local/directadmin/conf/mysql.conf
MYSQLOLD=$passwd
MYSQLNEW=`random_pass`
echo "OLDPASS:"$MYSQLOLD
echo "NEWPASS:"$MYSQLNEW
chmod 644 /usr/local/directadmin/conf/mysql.conf
perl -pi -e "s/^mysql=.*$/mysql=$MYSQLNEW/" /usr/local/directadmin/scripts/setup.txt
perl -pi -e "s/^password=.*$/password=$MYSQLNEW/" /usr/local/directadmin/conf/my.cnf
perl -pi -e "s/^passwd=.*$/passwd=$MYSQLNEW/" /usr/local/directadmin/conf/mysql.conf
mysqladmin -uroot -p$MYSQLOLD password $MYSQLNEW
mysqladmin -uda_admin -p$MYSQLOLD password $MYSQLNEW
service directadmin restart
```
