如何使用Perl更改mysql密码 [英] How to use Perl to change a mysql password

查看:80
本文介绍了如何使用Perl更改mysql密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Perl脚本更改一些mysql密码.更改数据库条目时,以下方法起作用,但是当我针对mysql用户更改对其进行修改时,它将它们重置为空白密码.最后,刷新特权"也很好,但是我还没有找到实现此目的的方法.

I need to change a few mysql passwords using a Perl script. The following works when changing database entries, but when I modified it for mysql user changes, it resets them to a blank password. It would also be nice to 'flush privileges' at the end of it, but I haven't found the method for that.

#!/usr/bin/perl

use DBI;
use strict;

my $newpass = "newpass";
my $driver = "mysql";
my $database = "mysql";
my $dsn = "DBI:$driver:database=$database";
my $dbh = DBI->connect($dsn, 'root', 'mysql' ) or die $DBI::errstr;
my $sth = $dbh->prepare("update user set password='$newpass' where User='admin'");
$sth->execute() or die $DBI::errstr;
$sth->finish();
$dbh->{AutoCommit} = 0;
$dbh->commit or die $DBI::errstr;

推荐答案

您缺少几个步骤. 使用PASSWORD()命令并使用'admin'而不是'root',并添加flush priv. 我在这里为您重写了脚本:

You're missing a couple of steps. Use the PASSWORD() command and used 'admin' and not 'root' and also add flush priv's. I rewrote the script for you, here:

#!/usr/bin/perl

use DBI;
use strict;

my $newpass = "newpass";
my $driver = "mysql";
my $database = "mysql";
my $dsn = "DBI:$driver:database=$database";
my $dbh = DBI->connect($dsn, 'root', 'mysql' ) or die $DBI::errstr;
$dbh->{AutoCommit} = 0;
my $sth = $dbh->prepare("update user set password=PASSWORD('$newpass') where User='root'");
$sth->execute() or die $DBI::errstr;
$dbh->do('FLUSH PRIVILEGES') or die $DBI::errstr;
$sth->finish();
$dbh->commit or die $DBI::errstr;

这篇关于如何使用Perl更改mysql密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆