修改mysql用户密码的方法总结_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 1668 | 回复: 0   主题: 修改mysql用户密码的方法总结        下一篇 
youduoduo
注册用户
等级:新兵
经验:78
发帖:78
精华:0
注册:2011-11-26
状态:离线
发送短消息息给youduoduo 加好友    发送短消息息给youduoduo 发消息
发表于: IP:您无权察看 2015-6-30 11:52:15 | [全部帖] [楼主帖] 楼主

1.修改root密码
方法1:使用mysqladmin命令
--适用于记得root旧密码,修改root密码
语法:
mysqladmin -u用户名 -p旧密码 password 新密码
例如:

# mysqladmin -u root -proot password mysql
--注意:如当旧密码输入错误时会报如下错误
# mysqladmin -u root -proot1 password mysql
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost'(using password: YES)'
--注意:当使用mysqladmin修改普通用户时
[root@gc ~]# [root@gc ~]# mysqladmin -uhive -phive1 password hive
mysqladmin: Can't turn off logging; error: 'Access denied; you need (at least one of) the SUPER privilege(s) for this operation'


方法2:直接更新user表password字段
--适用于忘记root密码,而对root密码进行重置,也适用于修改普通用户的密码
Step 1: 修改MySQL的登录设置

# vi /etc/my.cnf
--windows系统是my.ini文件
--在[mysqld]的段中加上一句:skip-grant-tables,如没有[mysqld]字段,可手动添加上
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-name-resolve
skip-grant-tables


Step 2: 重新启动mysql

[root@gc ~]# service mysql restart


Shutting down MySQL..[确定]
Starting MySQL...[确定]

Step 3: 登录并修改MySQL的root密码

--此时直接用mysql即可无需密码即可进入数据库了
[root@gc ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.24 MySQL Community Server (GPL)
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> update user set password=password('new_password') where user='root';
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5  Changed: 5  Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
--注意:如果没做step1,直接用mysql登录时会报如下错误
[root@gc ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)


Step 4: 将MySQL的登录设置修改回来
再删除/etc/my.cnf文件中的skip-grant-tables

Step 5: 重新启动mysql

[root@gc ~]# service mysql restart


Shutting down MySQL..[确定]
Starting MySQL...[确定]

其它修改密码的方法:
方法3:用MYSQL的grant语句
,例如

[root@gc ~]# mysql -uroot -proot
mysql> GRANT ALL ON *.* TO 'root'@'localhost' IDENTIFIED BY 'root1' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL ON *.* TO 'hive'@'localhost' IDENTIFIED BY 'hive1' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye


注意:不用password函数,也不用flush privileges命令。

方法4:通过set命令设置
语法:SET PASSWORD FOR '用户名'@'主机' = PASSWORD('密码')

[root@gc ~]# mysql -uroot -proot
mysql>set password for 'root'@'localhost'=password('root1');
--允许root通过远程登录
mysql>set password for 'root'@'%'=password('root1');


注意:要使用password函数,但不用flush privileges命令。

方法5:使用insert和replace语句,增加用户和修改密码

--增加用户
[root@gc ~]# mysql -uroot -proot
mysql> insert into mysql.user(host,user,password)
-> values('localhost','test2',password('test2'));
Query OK, 1 row affected, 3 warnings (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
--修改密码
mysql> replace into mysql.user (Host,User,Password)
-> values('localhost','test2',password('test'));
Query OK, 2 rows affected, 3 warnings (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)


--转自 北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论