sometimes we will forget our password of root in MySQL DB server.so,there're several methods below to solve these kind of issues.
 
I. ALTER USER ...
  1. pkill mysqld
  2. vim my.cnf -> add skip-grant-tables
  3. sh mysqld.sh
  4. mysql -S /tmp/mysql3306.sock
  5. flush privileges;
  6. alter user root@localhost identified by '';
  7. login again using new password
  8. exit & modify my.cnf to the original state
 eg 1:
 
 #mysql -S /tmp/mysql3306.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL)
Copyright (c) , , 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. (root@localhost mysql3306.sock)[(none)]::>alter user root@localhost identified by 'innodb';
ERROR (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement (root@localhost mysql3306.sock)[(none)]::>flush privileges;
Query OK, rows affected (0.00 sec) (root@localhost mysql3306.sock)[(none)]::>alter user root@localhost identified by 'innodb';
Query OK, rows affected (0.00 sec) (root@localhost mysql3306.sock)[(none)]::>quit;
Bye #mysql -p -S /tmp/mysql3306.sock
Enter password: <here the new Password is "innodb">
II. SET PASSWORD ...
  1. pkill mysqld
  2. vim my.cnf -> add skip-grant-tables
  3. sh mysqld.sh
  4. mysql -S /tmp/mysql3306.sock
  5. flush privileges;
  6. set password for root@localhost=''; --also can use password() function here
  7. login again using new password
  8. exit & modify my.cnf to the original state
 eg 2:
 

 #mysql -S /tmp/mysql3306.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL)
Copyright (c) , , 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. (root@localhost mysql3306.sock)[(none)]::>set password for root@localhost='mysql'; -- or,set password for root@localhost=password('mysql')
ERROR (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement (root@localhost mysql3306.sock)[(none)]::>flush privileges;
Query OK, rows affected (0.00 sec) (root@localhost mysql3306.sock)[(none)]::>set password for root@localhost='mysql';
Query OK, rows affected (0.00 sec) (root@localhost mysql3306.sock)[(none)]::>exit
Bye #mysql -p -S /tmp/mysql3306.sock
Enter password: <here the new Password is "mysql">
III. UPDATE MYSQL.USER SET ...
  1. pkill mysqld
  2. vim my.cnf -> add skip-grant-tables
  3. sh mysqld.sh
  4. mysql -S /tmp/mysql3306.sock
  5. flush privileges; --this step is not indispensable
  6. update mysql.user set authentication_string=password('') where ... ; --must use password() function,don't forget where clause to specify condition
  7. login again using new password
  8. exit & modify my.cnf to the original state
 eg 3:

 #mysql -S /tmp/mysql3306.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL)
Copyright (c) , , 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. (root@localhost mysql3306.sock)[(none)]::>update mysql.user set authentication_string=('oracle') where user='root' and host='localhost';
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: (root@localhost mysql3306.sock)[(none)]::>select user,host,authentication_string from mysql.user;
+---------------+---------------+-------------------------------------------+
| user | host | authentication_string |
+---------------+---------------+-------------------------------------------+
| root | localhost | oracle |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| repl | 192.168..% | *872ECE72A7EBAC6A183C90D7043D5F359BD85A9E |
| zlm | 192.168..% | *B746A45EBB84DD9DDEF015B332281AEFD164E2A9 |
| zlm | 192.168.1.102 | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
| zlm | 192.168.1.1 | *2447D497B9A6A15F2776055CB2D1E9F86758182F |
| zlm | 192.168.1.103 | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
+---------------+---------------+-------------------------------------------+
rows in set (0.00 sec)

    be careful,if you don't using the password() function to get your password,then you'll get a wrong result,and you cannot use the password "oracle" to login the mysql server.

 
 (root@localhost mysql3306.sock)[(none)]::>update mysql.user set authentication_string=password('oracle') where user='root' and host='localhost';
Query OK, row affected, warning (0.00 sec)
Rows matched: Changed: Warnings:
(root@localhost mysql3306.sock)[(none)]::>select user,host,authentication_string from mysql.user;
+---------------+---------------+-------------------------------------------+
| user | host | authentication_string |
+---------------+---------------+-------------------------------------------+
| root | localhost | *2447D497B9A6A15F2776055CB2D1E9F86758182F |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| repl | 192.168..% | *872ECE72A7EBAC6A183C90D7043D5F359BD85A9E |
| zlm | 192.168..% | *B746A45EBB84DD9DDEF015B332281AEFD164E2A9 |
| zlm | 192.168.1.102 | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
| zlm | 192.168.1.1 | *2447D497B9A6A15F2776055CB2D1E9F86758182F |
| zlm | 192.168.1.103 | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
+---------------+---------------+-------------------------------------------+
rows in set (0.00 sec)
(root@localhost mysql3306.sock)[(none)]::>exit
Bye
[root@zlm3 :: ~]
#mysql -p -S /tmp/mysql3306.sock
Enter password: <here the new Password is "oracle">
 
IV. USING --INIT-FILE WITHOUT --SKIP-GRANT-TABLES(Recommended)
  1. pkill mysqld
  2. add "alter user ..." into file change_pass.sql
  3. start mysqld with --init-file=<yourpath>/change_pass.sql
eg 4:

 [root@zlm3 :: ~]
#pkill mysqld [root@zlm3 :: ~]
#ps -ef | grep mysqld
root : pts/ :: grep --color=auto mysqld [root@zlm3 :: ~]
#pwd
/root [root@zlm3 :: ~]
#echo "alter user root@localhost identified by 'password';" > change_password.sql [root@zlm3 :: ~]
#cat change_password.sql
alter user root@localhost identified by 'password'; [root@zlm3 :: ~]
#mysqld --defaults-file=/data/mysql/mysql3306/my.cnf --init-file=/root/change_password.sql &
[] [root@zlm3 :: ~]
#ps -efl|grep mysqld
R root - - : pts/ :: grep --color=auto mysqld
[]+ Exit mysqld --defaults-file=/data/mysql/mysql3306/my.cnf --init-file=/root/change_password.sql [root@zlm3 :: ~]
#mysql -p -S /tmp/mysql3306.sock
Enter password:
ERROR (HY000): Can't connect to local MySQL server through socket '/tmp/mysql3306.sock' (2)
[root@zlm3 :: ~]
    it's obviously that the mysqld process has not been startd normally,let's check the "error.log" file to find what have happened.error.log shows below:
 
 --31T05::.520876Z  [Note] Server hostname (bind-address): '*'; port:
--31T05::.520915Z [Note] IPv6 is available.
--31T05::.520920Z [Note] - '::' resolves to '::';
--31T05::.520934Z [Note] Server socket created on IP: '::'.
--31T05::.544976Z [Note] Event Scheduler: Loaded events
--31T05::.545087Z [Note] Execution of init_file '/root/change_password.sql' started.
--31T05::.545108Z [ERROR] mysqld: File '/root/change_password.sql' not found (Errcode: - Permission denied)
--31T05::.545111Z [ERROR] Aborting
--31T05::.545226Z [Note] Giving client threads a chance to die gracefully
--31T05::.545233Z [Note] Shutting down slave threads
--31T05::.545237Z [Note] Forcefully disconnecting remaining clients
--31T05::.545239Z [Note] Event Scheduler: Purging the queue. events
--31T05::.545301Z [Note] Binlog end
--31T05::.547647Z [Note] Shutting down plugin 'ngram'
--31T05::.547666Z [Note] Shutting down plugin 'BLACKHOLE'
--31T05::.547669Z [Note] Shutting down plugin 'partition'
--31T05::.547671Z [Note] Shutting down plugin 'ARCHIVE'
--31T05::.547673Z [Note] Shutting down plugin 'MyISAM'
--31T05::.547678Z [Note] Shutting down plugin 'CSV'
--31T05::.547681Z [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL'
--31T05::.547683Z [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
--31T05::.547685Z [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
--31T05::.547686Z [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
--31T05::.547687Z [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
--31T05::.547689Z [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
--31T05::.547690Z [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
--31T05::.547692Z [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
--31T05::.547693Z [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
--31T05::.547694Z [Note] Shutting down plugin 'INNODB_SYS_TABLES'
--31T05::.547696Z [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
--31T05::.547703Z [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
--31T05::.547705Z [Note] Shutting down plugin 'INNODB_FT_CONFIG'
--31T05::.547706Z [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
--31T05::.547707Z [Note] Shutting down plugin 'INNODB_FT_DELETED'
--31T05::.547709Z [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
--31T05::.547710Z [Note] Shutting down plugin 'INNODB_METRICS'
--31T05::.547711Z [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO'
--31T05::.547713Z [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
--31T05::.547714Z [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
--31T05::.547716Z [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
--31T05::.547717Z [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
--31T05::.547718Z [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
--31T05::.547720Z [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
--31T05::.547721Z [Note] Shutting down plugin 'INNODB_CMPMEM'
--31T05::.547722Z [Note] Shutting down plugin 'INNODB_CMP_RESET'
--31T05::.547724Z [Note] Shutting down plugin 'INNODB_CMP'
--31T05::.547725Z [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
--31T05::.547727Z [Note] Shutting down plugin 'INNODB_LOCKS'
--31T05::.547728Z [Note] Shutting down plugin 'INNODB_TRX'
--31T05::.547729Z [Note] Shutting down plugin 'InnoDB'
--31T05::.547781Z [Note] InnoDB: FTS optimize thread exiting.
--31T05::.547899Z [Note] InnoDB: Starting shutdown...
--31T05::.553631Z [Note] InnoDB: Buffer pool(s) load completed at ::
--31T05::.553667Z [Note] InnoDB: Dumping buffer pool(s) to /data/mysql/mysql3306/data/ib_buffer_pool
--31T05::.553809Z [Note] InnoDB: Buffer pool(s) dump completed at ::
--31T05::.366016Z [Note] InnoDB: Shutdown completed; log sequence number
--31T05::.366078Z [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
--31T05::.366085Z [Note] Shutting down plugin 'MEMORY'
--31T05::.366090Z [Note] Shutting down plugin 'MRG_MYISAM'
--31T05::.366093Z [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
--31T05::.366111Z [Note] Shutting down plugin 'sha256_password'
--31T05::.366113Z [Note] Shutting down plugin 'mysql_native_password'
--31T05::.366263Z [Note] Shutting down plugin 'binlog'
--31T05::.370287Z [Note] mysqld: Shutdown complete
    okay,now we know about the reason why the mysqld process down,it was the privilege issue of OS code 13.let's check the privilege of "change_password.sql" then:
 
 [root@zlm3 :: ~]
#ls -l
total
-rw-------. root root Jul anaconda-ks.cfg
-rw-r--r-- root root May : change_password.sql
-rwxr-xr-x root root Apr : mysql-5.7.-linux-glibc2.-x86_64.tar.gz
-rwxr--r-- root root Apr : mysqld.sh
-rw-r--r-- root root May : mysqld.strace
drwxr-xr-x mysql mysql May : zabbix-3.0.
-rwxr-xr-x root root May : zabbix-3.0..tar
    first of all,i use command "chown mysql.mysql change_password.sql" to give the right ownership to the sql file,but it still don't work. why?'cause the father directory "/root" is not belong to the mysql user.then,i moved the file to the "/home/mysql" directory which owned by mysql user:
 
 [root@zlm3 :: ~]
#mv change_password.sql /home/mysql [root@zlm3 :: ~]
#cd /home/mysql [root@zlm3 :: /home/mysql]
#ls -l
total
-rw-r--r-- mysql mysql May : change_password.sql
    let's start the mysqld process again,well,it's running now:
 
 [root@zlm3 :: ~]
#mysqld --defaults-file=/data/mysql/mysql3306/my.cnf --init-file=/home/mysql/change_password.sql &
[] [root@zlm3 :: ~]
#ps aux|grep mysqld
mysql 3.2 17.5 pts/ Sl : : mysqld --defaults-file=/data/mysql/mysql3306/my.cnf --init-file=/home/mysql/change_password.sql
root 0.0 0.0 pts/ R+ : : grep --color=auto mysqld [root@zlm3 :: ~]
#mysql -p -S /tmp/mysql3306.sock
Enter password: <here the new Password is "password">
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL)
Copyright (c) , , 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.
(root@localhost mysql3306.sock)[(none)]::>
Summary:
  • when changing the password of root,shutdown the mysqld process once is necessary.
  • method 1~3 based on the parameter "--skip-grant-tables",the only difference is using different gramma.
  • method 1~2 need to use "flush privileges;" before excecution the spercific changing command.
  • method 4 is more convenient,so i rather recommend to use this way to achive your purpose.
  • putting the parameter "init-file=<your sql file path>" under the "[mysqld],[mysqld_safe],[server]" group is also a workaround,but i don't recommend that.
  • once you've executed "flush privileges;" ,it means the privilege table has been updated,then you must use the specific password you've changed just now with "-p" parameter to login the MySQL server,even if your parameter "skip-grant-tables" is still in my.cnf,only if you restart the mysqld process.
 
for example:
 
 #mysql -p -S /tmp/mysql3306.sock
Enter password: <here put the right password>
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-log MySQL Community Server (GPL)
Copyright (c) , , 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. (root@localhost mysql3306.sock)[(none)]::>exit
Bye [root@zlm3 :: ~]
#mysql -p -S /tmp/mysql3306.sock
Enter password: <here put the wrong password>
ERROR (): Access denied for user 'root'@'localhost' (using password: YES) [root@zlm3 :: ~]
#mysql -S /tmp/mysql3306.sock --not using "-p" parameter
ERROR (): Access denied for user 'root'@'localhost' (using password: NO) #[root@zlm3 :: ~]

MySQL 5.7修改root密码的4种方法的更多相关文章

  1. mysql忘记密码如何重置密码,以及修改root密码的三种方法

    1.先将MySQL停止. 命令:systemctl  stop mysqld       #停掉MySQL 命令:systemctl status mysqld         #查看状态 2.然后跳 ...

  2. MySQL修改root密码的几种方法

    方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:my ...

  3. MySQL——修改root密码的4种方法(以windows为例)

    方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:my ...

  4. MySQL修改root密码的3种方法

    方法1: 用SET PASSWORD命令 mysql -u rootmysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass' ...

  5. [MySQL]修改root密码的4种方法(以windows为例)

    方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:my ...

  6. MySQL 8.*版本 修改root密码

    MySQL .*版本 修改root密码 查看版本:select version() from dual; 1.6. 登录mysql: 登录mysql:(因为之前没设置密码,所以密码为空,不用输入密码, ...

  7. Mysql数据库中设置root密码的命令及方法

    我们都知道通常PHP连接 Mysql都是通过root用户名和密码连接,默认情况下在Mysql安装时root初始密码为空,在安装使用PHP开源系统时,都需要填写连接Mysql数据库的用户名和密码,此时当 ...

  8. 几种破解MySQL root密码的几种方法:

    几种破解MySQL root密码的几种方法: 方法一 使用phpmyadmin,这是最简单的了,修改mysql库的user表,不过别忘了使用PASSWord函数. 方法二 使用mysqladmin,这 ...

  9. windows MYSQL 安装及修改root密码

    官网下载zip包,我下载的是64位的: 下载地址:https://dev.mysql.com/downloads/mysql/ 下载zip的包: 下载后解压:(解压在哪个盘都可以的) 我放在了这里 E ...

随机推荐

  1. Linux符设备驱动编程

    加入内核源码树外 ① 建立两个文件scull.c,scull.h,以及Makefile文件 Makefile文件 ② 用make进行编译,生成scull.ko驱动程序模块 ③ 把scull.ko模块加 ...

  2. lua中使用table实现类和继承

    --因为只有当读写不存在的域时,才会触发__index和__newindex classA = {className = "classA",name="classAIns ...

  3. 程序员装B指南(转载)

    转自:http://www.oschina.net/question/615783_115390 一.准备工作 "工欲善其事必先利其器." 1.电脑不一定要配置高,但是双屏是必须的 ...

  4. 基于bootstrap的单选(radio)或者多选(checkbox)的选择框组

    完成的效果如下图所示: html代码如下: <!-- 两行组 --> <div class="box"> <ul class="list-g ...

  5. [使用经验]cocostudio UI编辑器的裁剪

    日志-2015/03/16 描述:在程序使用UI编辑器导出文件的时候,该panel中大于panel的部分都没有显示出来,例如人物,一些特效等 原因:UI编辑器panel都勾上了裁剪 解决:在编辑器中把 ...

  6. Toad for MySQL 7.3 Freeware异常 2017-01-09 15:14 115人阅读 评论(0) 收藏

    打开Toad出现如下异常信息: 解决办法: 重装.NET Framework4.0

  7. 设计模式:代理(Proxy)模式

    设计模式:代理(Proxy)模式 一.前言    代理模式或许我们都听说过,至少知道代理(Proxy)这个东西的,否则看这篇博客也没任何意义的.什么叫做代理,代理是代替服务器去接受请求者的请求的中间人 ...

  8. [工具教程] HBuilder调试夜神安卓模拟器方法(该方法真实有效)

    HBuilder调试夜神安卓模拟器方法 现在开发手机app的IDE很多,今天我就以我个人开发使用的HBuider开发工具讲一下手机app开发调试.HBuider支持真机调试,这个比较简单,只要安装好手 ...

  9. 全链路实践Spring Cloud 微服务架构

    Spring Cloud 微服务架构全链路实践Spring Cloud 微服务架构全链路实践 阅读目录: 网关请求流程 Eureka 服务治理 Config 配置中心 Hystrix 监控 服务调用链 ...

  10. JAVA串口开发帮助类分享-及写在马年末

    摘要: 在系统集成开发过程中,存在着各式的传输途径,其中串口经常因其安全性高获得了数据安全传输的重用,通过串口传输可以从硬件上保证数据传输的单向性,这是其它介质所不具备的物理条件.下面我就串口java ...