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)