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-grants-tables
3.sh mysqld.sh
4.mysql -S /tmp/mysql3306.sock
5.flush privileges;
6.alter user [email protected] 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 1
Server version: 5.7.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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.
([email protected] mysql3306.sock)[(none)]03:23:51>alter user [email protected] identified by ‘innodb‘;
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
([email protected] mysql3306.sock)[(none)]03:24:18>flush privileges;
Query OK, 0 rows affected (0.00 sec)
([email protected] mysql3306.sock)[(none)]03:24:41>alter user [email protected] identified by ‘innodb‘;
Query OK, 0 rows affected (0.00 sec)
([email protected] mysql3306.sock)[(none)]03:24:53>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-grants-tables
3.sh mysqld.sh
4.mysql -S /tmp/mysql3306.sock
5.flush privileges;
6.set password for [email protected]=‘‘; --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 2
Server version: 5.7.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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.
([email protected] mysql3306.sock)[(none)]03:32:24>set password for [email protected]=‘mysql‘; -- or,set password for [email protected]=password(‘mysql‘)
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
([email protected] mysql3306.sock)[(none)]03:33:13>flush privileges;
Query OK, 0 rows affected (0.00 sec)
([email protected] mysql3306.sock)[(none)]03:33:25>set password for [email protected]=‘mysql‘;
Query OK, 0 rows affected (0.00 sec)
([email protected] mysql3306.sock)[(none)]03:33:32>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-grants-tables
3.sh mysqld.sh
4.mysql -S /tmp/mysql3306.sock
5.flush privileges; --this step is not indispensable
6.update authentication_string column of mysql.user table --must use password() function
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 3
Server version: 5.7.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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.
([email protected] mysql3306.sock)[(none)]03:42:32>update mysql.user set authentication_string=(‘oracle‘) where user=‘root‘ and host=‘localhost‘;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
([email protected] mysql3306.sock)[(none)]03:43:50>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.1.% | *872ECE72A7EBAC6A183C90D7043D5F359BD85A9E |
| zlm | 192.168.1.% | *B746A45EBB84DD9DDEF015B332281AEFD164E2A9 |
| zlm | 192.168.1.102 | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
| zlm | 192.168.1.1 | *2447D497B9A6A15F2776055CB2D1E9F86758182F |
| zlm | 192.168.1.103 | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
+---------------+---------------+-------------------------------------------+
8 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.
([email protected] mysql3306.sock)[(none)]03:44:00>update mysql.user set authentication_string=password(‘oracle‘) where user=‘root‘ and host=‘localhost‘;
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
([email protected] mysql3306.sock)[(none)]03:44:18>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.1.% | *872ECE72A7EBAC6A183C90D7043D5F359BD85A9E |
| zlm | 192.168.1.% | *B746A45EBB84DD9DDEF015B332281AEFD164E2A9 |
| zlm | 192.168.1.102 | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
| zlm | 192.168.1.1 | *2447D497B9A6A15F2776055CB2D1E9F86758182F |
| zlm | 192.168.1.103 | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
+---------------+---------------+-------------------------------------------+
8 rows in set (0.00 sec)
([email protected] mysql3306.sock)[(none)]03:44:25>exit
Bye
[[email protected] 03:45:03 ~]
#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:
[[email protected] 06:50:25 ~]
#pkill mysqld
[[email protected] 06:50:29 ~]
#ps -ef | grep mysqld
root 4719 3724 0 06:52 pts/0 00:00:00 grep --color=auto mysqld
[[email protected] 06:52:51 ~]
#pwd
/root
[[email protected] 06:56:50 ~]
#echo "alter user [email protected] identified by ‘password‘;" > change_password.sql
[[email protected] 06:57:54 ~]
#cat change_password.sql
alter user [email protected] identified by ‘password‘;
[[email protected] 06:58:04 ~]
#mysqld --defaults-file=/data/mysql/mysql3306/my.cnf --init-file=/root/change_password.sql &
[1] 4738
[[email protected] 06:59:30 ~]
#ps -efl|grep mysqld
0 R root 4770 3724 0 80 0 - 28160 - 06:59 pts/0 00:00:00 grep --color=auto mysqld
[1]+ Exit 1 mysqld --defaults-file=/data/mysql/mysql3306/my.cnf --init-file=/root/change_password.sql
[[email protected] 06:59:51 ~]
#mysql -p -S /tmp/mysql3306.sock
Enter password:
ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/tmp/mysql3306.sock‘ (2)
[[email protected] 07:00:28 ~]
it‘s obviously that the mysqld process has not been startd normally,let‘s check the "error.log" file to find what have happened.
the error.log shows below:
2018-05-31T05:15:13.520876Z 0 [Note] Server hostname (bind-address): ‘*‘; port: 3306
2018-05-31T05:15:13.520915Z 0 [Note] IPv6 is available.
2018-05-31T05:15:13.520920Z 0 [Note] - ‘::‘ resolves to ‘::‘;
2018-05-31T05:15:13.520934Z 0 [Note] Server socket created on IP: ‘::‘.
2018-05-31T05:15:13.544976Z 0 [Note] Event Scheduler: Loaded 0 events
2018-05-31T05:15:13.545087Z 0 [Note] Execution of init_file ‘/root/change_password.sql‘ started.
2018-05-31T05:15:13.545108Z 0 [ERROR] mysqld: File ‘/root/change_password.sql‘ not found (Errcode: 13 - Permission denied)
2018-05-31T05:15:13.545111Z 0 [ERROR] Aborting
2018-05-31T05:15:13.545226Z 0 [Note] Giving 0 client threads a chance to die gracefully
2018-05-31T05:15:13.545233Z 0 [Note] Shutting down slave threads
2018-05-31T05:15:13.545237Z 0 [Note] Forcefully disconnecting 0 remaining clients
2018-05-31T05:15:13.545239Z 0 [Note] Event Scheduler: Purging the queue. 0 events
2018-05-31T05:15:13.545301Z 0 [Note] Binlog end
2018-05-31T05:15:13.547647Z 0 [Note] Shutting down plugin ‘ngram‘
2018-05-31T05:15:13.547666Z 0 [Note] Shutting down plugin ‘BLACKHOLE‘
2018-05-31T05:15:13.547669Z 0 [Note] Shutting down plugin ‘partition‘
2018-05-31T05:15:13.547671Z 0 [Note] Shutting down plugin ‘ARCHIVE‘
2018-05-31T05:15:13.547673Z 0 [Note] Shutting down plugin ‘MyISAM‘
2018-05-31T05:15:13.547678Z 0 [Note] Shutting down plugin ‘CSV‘
2018-05-31T05:15:13.547681Z 0 [Note] Shutting down plugin ‘INNODB_SYS_VIRTUAL‘
2018-05-31T05:15:13.547683Z 0 [Note] Shutting down plugin ‘INNODB_SYS_DATAFILES‘
2018-05-31T05:15:13.547685Z 0 [Note] Shutting down plugin ‘INNODB_SYS_TABLESPACES‘
2018-05-31T05:15:13.547686Z 0 [Note] Shutting down plugin ‘INNODB_SYS_FOREIGN_COLS‘
2018-05-31T05:15:13.547687Z 0 [Note] Shutting down plugin ‘INNODB_SYS_FOREIGN‘
2018-05-31T05:15:13.547689Z 0 [Note] Shutting down plugin ‘INNODB_SYS_FIELDS‘
2018-05-31T05:15:13.547690Z 0 [Note] Shutting down plugin ‘INNODB_SYS_COLUMNS‘
2018-05-31T05:15:13.547692Z 0 [Note] Shutting down plugin ‘INNODB_SYS_INDEXES‘
2018-05-31T05:15:13.547693Z 0 [Note] Shutting down plugin ‘INNODB_SYS_TABLESTATS‘
2018-05-31T05:15:13.547694Z 0 [Note] Shutting down plugin ‘INNODB_SYS_TABLES‘
2018-05-31T05:15:13.547696Z 0 [Note] Shutting down plugin ‘INNODB_FT_INDEX_TABLE‘
2018-05-31T05:15:13.547703Z 0 [Note] Shutting down plugin ‘INNODB_FT_INDEX_CACHE‘
2018-05-31T05:15:13.547705Z 0 [Note] Shutting down plugin ‘INNODB_FT_CONFIG‘
2018-05-31T05:15:13.547706Z 0 [Note] Shutting down plugin ‘INNODB_FT_BEING_DELETED‘
2018-05-31T05:15:13.547707Z 0 [Note] Shutting down plugin ‘INNODB_FT_DELETED‘
2018-05-31T05:15:13.547709Z 0 [Note] Shutting down plugin ‘INNODB_FT_DEFAULT_STOPWORD‘
2018-05-31T05:15:13.547710Z 0 [Note] Shutting down plugin ‘INNODB_METRICS‘
2018-05-31T05:15:13.547711Z 0 [Note] Shutting down plugin ‘INNODB_TEMP_TABLE_INFO‘
2018-05-31T05:15:13.547713Z 0 [Note] Shutting down plugin ‘INNODB_BUFFER_POOL_STATS‘
2018-05-31T05:15:13.547714Z 0 [Note] Shutting down plugin ‘INNODB_BUFFER_PAGE_LRU‘
2018-05-31T05:15:13.547716Z 0 [Note] Shutting down plugin ‘INNODB_BUFFER_PAGE‘
2018-05-31T05:15:13.547717Z 0 [Note] Shutting down plugin ‘INNODB_CMP_PER_INDEX_RESET‘
2018-05-31T05:15:13.547718Z 0 [Note] Shutting down plugin ‘INNODB_CMP_PER_INDEX‘
2018-05-31T05:15:13.547720Z 0 [Note] Shutting down plugin ‘INNODB_CMPMEM_RESET‘
2018-05-31T05:15:13.547721Z 0 [Note] Shutting down plugin ‘INNODB_CMPMEM‘
2018-05-31T05:15:13.547722Z 0 [Note] Shutting down plugin ‘INNODB_CMP_RESET‘
2018-05-31T05:15:13.547724Z 0 [Note] Shutting down plugin ‘INNODB_CMP‘
2018-05-31T05:15:13.547725Z 0 [Note] Shutting down plugin ‘INNODB_LOCK_WAITS‘
2018-05-31T05:15:13.547727Z 0 [Note] Shutting down plugin ‘INNODB_LOCKS‘
2018-05-31T05:15:13.547728Z 0 [Note] Shutting down plugin ‘INNODB_TRX‘
2018-05-31T05:15:13.547729Z 0 [Note] Shutting down plugin ‘InnoDB‘
2018-05-31T05:15:13.547781Z 0 [Note] InnoDB: FTS optimize thread exiting.
2018-05-31T05:15:13.547899Z 0 [Note] InnoDB: Starting shutdown...
2018-05-31T05:15:13.553631Z 0 [Note] InnoDB: Buffer pool(s) load completed at 180531 7:15:13
2018-05-31T05:15:13.553667Z 0 [Note] InnoDB: Dumping buffer pool(s) to /data/mysql/mysql3306/data/ib_buffer_pool
2018-05-31T05:15:13.553809Z 0 [Note] InnoDB: Buffer pool(s) dump completed at 180531 7:15:13
2018-05-31T05:15:15.366016Z 0 [Note] InnoDB: Shutdown completed; log sequence number 1036828567
2018-05-31T05:15:15.366078Z 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
2018-05-31T05:15:15.366085Z 0 [Note] Shutting down plugin ‘MEMORY‘
2018-05-31T05:15:15.366090Z 0 [Note] Shutting down plugin ‘MRG_MYISAM‘
2018-05-31T05:15:15.366093Z 0 [Note] Shutting down plugin ‘PERFORMANCE_SCHEMA‘
2018-05-31T05:15:15.366111Z 0 [Note] Shutting down plugin ‘sha256_password‘
2018-05-31T05:15:15.366113Z 0 [Note] Shutting down plugin ‘mysql_native_password‘
2018-05-31T05:15:15.366263Z 0 [Note] Shutting down plugin ‘binlog‘
2018-05-31T05:15:15.370287Z 0 [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:
[[email protected] 07:41:36 ~]
#ls -l
total 685212
-rw-------. 1 root root 1431 Jul 16 2015 anaconda-ks.cfg
-rw-r--r-- 1 root root 52 May 31 06:57 change_password.sql
-rwxr-xr-x 1 root root 641798603 Apr 28 14:02 mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
-rwxr--r-- 1 root root 54 Apr 28 14:14 mysqld.sh
-rw-r--r-- 1 root root 451 May 30 05:18 mysqld.strace
drwxr-xr-x 14 mysql mysql 4096 May 2 07:57 zabbix-3.0.16
-rwxr-xr-x 1 root root 59801600 May 2 07:55 zabbix-3.0.16.tar
first of all,i use command "chown mysql.mysql change_password.sql" to give the right owner 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" direcotry which owned by mysql user:
[[email protected] 07:41:37 ~]
#mv change_password.sql /home/mysql
[[email protected] 07:42:11 ~]
#cd /home/mysql
[[email protected] 07:42:14 /home/mysql]
#ls -l
total 4
-rw-r--r-- 1 mysql mysql 52 May 31 06:57 change_password.sql
let‘s start the mysqld process again,well,it‘s running now:
[[email protected] 07:42:33 ~]
#mysqld --defaults-file=/data/mysql/mysql3306/my.cnf --init-file=/home/mysql/change_password.sql &
[1] 5181
[[email protected] 07:42:45 ~]
#ps aux|grep mysqld
mysql 5181 3.2 17.5 1069676 179052 pts/0 Sl 07:42 0:00 mysqld --defaults-file=/data/mysql/mysql3306/my.cnf --init-file=/home/mysql/change_password.sql
root 5215 0.0 0.0 112640 960 pts/0 R+ 07:42 0:00 grep --color=auto mysqld
[[email protected] 07:42:52 ~]
#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 4
Server version: 5.7.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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.
([email protected] mysql3306.sock)[(none)]07:43:38>
Summary:
1.when changing the password of root,shutdown the mysqld process once is necessary.
2.method 1~3 based on the parameter "--skip-grant-tables",the only difference is using different gramma.
3.method 1~2 need to use "flush privileges;" before excecution the spercific changing command.
4.method 4 is more convenient,so i rather recommend to use this way to achive your purpose.
5.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.
6.once you‘ve used the new password logining into the mysql server by "-p" parameter,that is,the privilege table in mysql database has been updated.then you cannot login without "-p" parameter(or with "-p" but type a wrong password) to login next time,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 5
Server version: 5.7.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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.
([email protected] mysql3306.sock)[(none)]08:52:25>exit
Bye
[[email protected] 08:53:26 ~]
#mysql -p -S /tmp/mysql3306.sock
Enter password:
<here put the wrong password>
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
[[email protected] 08:53:32 ~]
#mysql -S /tmp/mysql3306.sock
--not using "-p" parameter
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)
#[[email protected] 09:04:55 ~]
原文地址:https://www.cnblogs.com/aaron8219/p/9117048.html