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. Android GridView设置行数

    普通的做法是设置一个高度,然后里面能显示出来几行就是几行,如果里面的内容高度变了,就需要重新调整高度来适配. 观察了一下它的onMeasure @Override protected void onM ...

  2. MySQL数据库、表的字符编码

    用MySQL命令行新建数据库和表时默认的字符编码是latin1,但是在实际开发过程中一般都是使用utf8格式的编码.操作如下: 1.修改数据库字符编码 mysql> alter database ...

  3. k 近邻算法(k-Nearest Neighbor,简称kNN)

    预约助教问题: 1.计算1-NN,k-nn和linear regression这三个算法训练和查询的时间复杂度和空间复杂度? 一. WHy 最简单最初级的分类器是将全部的训练数据所对应的类别都记录下来 ...

  4. Nginx 性能参数优化

    user www www; # ginx要开启的进程数 一般等于cpu的总核数,没必要开那么多,1个nginx内存消耗10兆左右 worker_processes 4; # 为每个进程分配cpu,上例 ...

  5. X11/Xlib.h:没有该文件或目录

    编译程序时出现的错误,在安装日志上发现一句:x11/xlib.h nosuch file or directory 在网上查阅了资料,原来是x11M没有装. 解决方案:先安装X11,命令为    su ...

  6. C#连接SQL Server测试

    string con, sql; con = "Server=192.168.31.26;Database=TestDB;user=kala;pwd=Password"; sql ...

  7. zt 设计模式六大原则(3):依赖倒置原则

    下面说法对不对? 父类将算法(逻辑)封装起来,子类实现细节:这个就叫DIP(依赖倒置:Dependency Inversion Principles),模板模式就是这个原则的实现.如果在父类中加一个t ...

  8. Hive入门操作

    Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供类SQL查询功能.本文描述了HIve的一些基本操作,如有错误之处还请指出. 常用语法 #显示相关信息 sh ...

  9. TB5上正常使用msfconsole

    在TB上使用系统自带的msfconsole,给出以下错误 [-] Failed to connect to the database: could not connect to server: Con ...

  10. 浅谈jquery之on()绑定事件和off()解除绑定事件

    off()函数用于移除元素上绑定的一个或多个事件的事件处理函数. off()函数主要用于解除由on()函数绑定的事件处理函数. 该函数属于jQuery对象(实例). 语法 jQuery 1.7 新增该 ...