MySQL修改密码和加密

来源:互联网 发布:colorsnapper for mac 编辑:程序博客网 时间:2024/04/30 02:17

1,给密码加密

范例:用户名是hw;密码是root,不想别人在数据库中看到密码root的明文:

insert into hw values(null,'hw',PASSWORD('root'));

那么别人在数据库中select看到的结果就是:

——-|—————————|———————-

id name password2 1 hw *A0B30B8D9F3C3595594C253D96748149629A9407

其中 *A0B30B8D9F3C3595594C253D96748149629A9407是经过PASSWORD函数加密的。

那么在Web中如何验证加密后的密码呢?
使用的SQL语句是:
“select count(*)from hw where hw.name =? and hw.password2=PASSWORD(?)”
具体的验证代码如下:

public boolean getByName(String name,String password){        BigInteger  pass=(BigInteger )this.sessionFactory.getCurrentSession().        createSQLQuery("select count(*)from hw where hw.name =? and hw.password2=PASSWORD(?)").setParameter(0, name).setParameter(1, password).uniqueResult();        if(0==pass.intValue())return false;        return true;    }

2,修改MySQL密码

有两种方式修改MySQL密码

方式一

mysql>  update mysql.user set password=PASSWORD('root') where user='root';mysql> flush privileges;

方式二
运行cmd进入命令行窗口,然后输入:

mysqladmin -uroot -proot password 1234 (把密码从root改为1234)mysqladmin -uroot -phw password root (把密码从hw改为root)

3,创建新用户

mysql> grant all  on *.* to dbuser@localhost identified by1234with grant option;

@ 后面跟ip,表示允许登录的ip,%表示允许任何ip访问
这里写图片描述

0 0
原创粉丝点击