如何设置mysql root密码:


1、首次安装 root初始密码为空,注意就是没有密码,直接敲回车即可进入
D:\wamp\bin\mysql\mysql5.6.17\bin> mysql -uroot -p
Enter password: [直接回车]

Welcome to the MySQL monitor. Commands end with ; or \g. 

Your MySQL connection id is 7 

Server version: 5.6.17 MySQL Community Server (GPL) 



Copyright (c) 2000, 2014, 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>




2、修改root密码的几种方法:
1) 用SET PASSWORD命令
  首先登录MySQL。
  格式:mysql> set password for 用户名@localhost = password('新密码');

  例如:


mysql>  
set password for root@localhost = password('root'); 

Query OK, 0 rows affected (0.00 sec) 



mysql>  
quit 

Bye 



D:\wamp\bin\mysql\mysql5.6.17\bin> 
mysql -uroot -proot 

Warning: Using a password on the command line interface can be insecure. 

Welcome to the MySQL monitor. Commands end with ; or \g. 

Your MySQL connection id is 8 

Server version: 5.6.17 MySQL Community Server (GPL) 



Copyright (c) 2000, 2014, 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>


2)用mysqladmin


  格式:mysqladmin -u用户名 -p旧密码 password 新密码


  例如:


D:\wamp\bin\mysql\mysql5.6.17\bin> 
mysqladmin -uroot -proot password root123 

Warning: Using a password on the command line interface can be insecure. 



D:\wamp\bin\mysql\mysql5.6.17\bin> 
mysql -uroot -proot123 

Warning: Using a password on the command line interface can be insecure. 

Welcome to the MySQL monitor. Commands end with ; or \g. 

Your MySQL connection id is 10 

Server version: 5.6.17 MySQL Community Server (GPL) 



Copyright (c) 2000, 2014, 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>



3)用UPDATE直接编辑user表


  首先登录MySQL。


mysql> use mysql;
   mysql> update user set password=password('root') where user='root' and host='localhost';
   mysql> flush privileges;



--注意一定要flush privileges;


mysql 新设置用户或更改密码后需用flush privileges刷新MySQL的系统权限相关表,否则会出现拒绝访问,还有一种方法,就是重新启动mysql服务器,来使新设置生效。



4)

在忘记root密码的时候,可以这样。

  以windows为例:

  1. 关闭正在运行的MySQL服务。

  2. 打开DOS窗口,转到mysql\bin目录。

  3. 输入mysqld --skip-grant-tables 回车。--skip-grant-tables 的意思是启动MySQL服务的时候跳过权限表认证。

  4. 再开一个DOS窗口(因为刚才那个DOS窗口已经不能动了),转到mysql\bin目录。

  5. 输入mysql回车,如果成功,将出现MySQL提示符 >。

  6. 连接权限数据库: use mysql; 。

  6. 改密码:update user set password=password('root123') where user='root';(别忘了最后加分号) 。

  7. 刷新权限(必须步骤):flush privileges; 

  8. 退出quit当前cmd,并且关闭2中的DOS窗口

  9. 使用用户名root,新密码root123在4中DOS窗口登录。