修改用户密码

 

[root@SR3 mysqldata]# /opt/mysql/bin/mysql -hlocalhost -uroot -pinfohow
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.16-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use mysql;
Database changed

 

mysql> set password for 'dms'@'10.86.87.155'=password('infohow');
Query OK, 0 rows affected (0.03 sec)

mysql> FLUSH PRIVILEGES
    -> ;
Query OK, 0 rows affected (0.01 sec)

 

 

[root@SR3 mysqldata]# /opt/mysql/bin/mysql -h10.86.87.155 -udms  -pinfohow
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.5.16-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit

 

在 PostgreSQL 中修改用户密码可以通过 SQL 命令或使用 `psql` 工具来完成。以下是几种常用的方法: ### 使用 SQL 命令修改用户密码 可以使用 `ALTER USER` 语句来更改用户密码。基本语法如下: ```sql ALTER USER username WITH PASSWORD 'new_password'; ``` 其中,`username` 是要修改密码的数据库用户名,`'new_password'` 是新的密码。例如,将用户 `postgres` 的密码更改为 `my_new_password`: ```sql ALTER USER postgres WITH PASSWORD 'my_new_password'; ``` 此方法适用于直接通过数据库客户端执行命令的情况 [^1]。 ### 在 `psql` 工具中修改密码 如果已经登录到 `psql` 命令行工具,可以直接执行该命令来修改密码。首先切换到 `postgres` 用户并进入 `psql` 环境: ```bash sudo su postgres -c psql template1 ``` 然后执行以下 SQL 命令来更改密码: ```sql ALTER USER postgres WITH PASSWORD 'new_password'; ``` 完成后输入 `\q` 退出 `psql`: ```sql \q ``` 这种方法常用于系统管理员在本地环境中操作 PostgreSQL 数据库 [^2]。 ### 修改其他用户密码 除了 `postgres` 用户外,也可以为其他自定义用户修改密码。只需替换 `ALTER USER` 语句中的用户名即可。例如,修改用户 `john_doe` 的密码: ```sql ALTER USER john_doe WITH PASSWORD 'new_secure_password'; ``` 确保新密码符合安全性要求,并定期更新以防止未经授权的访问。 ### 使用 `pgcrypto` 扩展(可选) PostgreSQL 提供了 `pgcrypto` 模块,允许对密码进行加密存储。启用该模块后,可以使用 `crypt()` 函数来设置加密密码: ```sql CREATE EXTENSION IF NOT EXISTS pgcrypto; UPDATE pg_authid SET rolpassword = crypt('new_password', gen_salt('md5')) WHERE rolname = 'username'; ``` 这种方式提供了更高的安全性,适合对数据库安全有严格要求的场景。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值