mysql5.7修改密碼
為了提高安全性,mysql5.7中 user 表的 password 被 authentication_string 字段所取代,下面簡紹幾種修改root密碼的方法(其他用戶也大同小異)。
推薦順序:
法一:
mysql> alter user 'root'@'localhost' identified by 'cy7m0ypu8CpLFperzI45';
法二:
mysql> set password for 'root'@'localhost'=password('cy7m0ypu8CpLFperzI45');
法三:
mysql> update mysql.user set authentication_string=password('cy7m0ypu8CpLFperzI45') where user='root' and Host = 'localhost';
-- 記得最后要刷新權限 mysql> flush privileges;
具體示例:
[mysql@db134 application]$ /data/mysql/application/mysql/bin/mysql -uroot -p -h localhost --socket=/data/mysql/percona_server_new/run/mysql.sock Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.17-13-log Copyright (c) 2009-2016 Percona LLC and/or its affiliates Copyright (c) 2000, 2016, 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> set global validate_password_policy=LOW; Query OK, 0 rows affected (0.00 sec) mysql> set password for 'root'@'localhost'=password('cy7m0ypu8CpLFperzI45'); Query OK, 0 rows affected, 1 warning (0.08 sec) mysql> alter user 'root'@'localhost' identified by 'cy7m0ypu8CpLFperzI45'; Query OK, 0 rows affected (0.01 sec) mysql> update mysql.user set authentication_string=password('cy7m0ypu8CpLFperzI45') where user='root' and Host = 'localhost'; Query OK, 0 rows affected, 1 warning (0.05 sec) Rows matched: 1 Changed: 0 Warnings: 1 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) -- 驗證登錄 [mysql@db134 application]$ /data/mysql/application/mysql/bin/mysql -uroot -p -h localhost --socket=/data/mysql/percona_server_new/run/mysql.sock Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.17-13-log Percona Server (GPL), Release 13, Revision fd33d43 Copyright (c) 2009-2016 Percona LLC and/or its affiliates Copyright (c) 2000, 2016, 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> exit Bye [mysql@db134 application]$
修改其他用戶示例:
mysql> create database db_test; Query OK, 1 row affected (0.35 sec) mysql> create user 'test_u'@'%' identified by 'kkSWhEpMVZ1xfaebJsRS'; Query OK, 0 rows affected (0.06 sec) mysql> grant select,insert,delete,update on db_test.* to 'test_u'@'%' ; Query OK, 0 rows affected (0.00 sec) -- 測試新建的賬號 [root@db134 ~]# mysql -h 192.168.142.134 -utest_u -p'kkSWhEpMVZ1xfaebJsRS' mysql: [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 7 Server version: 5.7.17-13-log Percona Server (GPL), Release 13, Revision fd33d43 Copyright (c) 2009-2016 Percona LLC and/or its affiliates Copyright (c) 2000, 2016, 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db_test | +--------------------+ 2 rows in set (0.00 sec) -- 假設該用戶忘記密碼 -- 用管理員賬號登錄操作 mysql> select user,host,authentication_string from mysql.user; +-----------+-----------+-------------------------------------------+ | user | host | authentication_string | +-----------+-----------+-------------------------------------------+ | root | localhost | *9EF2DB3EB868F2BF2AF1F8DD00185B23390E20A3 | | mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | test_u | % | *240FADB0A594DB97220A0F354CF891C0EF9F00D4 | +-----------+-----------+-------------------------------------------+ 3 rows in set (0.00 sec) mysql> alter user 'test_u'@'%' identified by 'kkSWhEpMVZ1xfaebJsHK'; Query OK, 0 rows affected (0.01 sec) -- 用新密碼驗證登錄 [root@db134 ~]# mysql -h 192.168.142.134 -utest_u -p'kkSWhEpMVZ1xfaebJsHK' mysql: [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.7.17-13-log Percona Server (GPL), Release 13, Revision fd33d43 Copyright (c) 2009-2016 Percona LLC and/or its affiliates Copyright (c) 2000, 2016, 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> exit Bye [root@db134 ~]# -- 其他兩種方法均可 mysql> set password for 'test_u'@'%'=password('kkSWhEpMVZ1xfaebJsXY'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> update mysql.user set authentication_string=password('kkSWhEpMVZ1xfaebJsHG') where user='test_u' and Host = '%'; -- 這個操作,必須執行flush privileges;才能生效 Query OK, 1 row affected, 1 warning (0.08 sec) Rows matched: 1 Changed: 1 Warnings: 1 mysql> mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)