MySQL 案例实战--修改 MySQL 数据库的登录密码
原创
©著作权归作者所有:来自51CTO博客作者褪色的腿毛的原创作品,请联系作者获取转载授权,否则将追究法律责任
修改 MySQL 数据库的登录密码
- 前言
- 一、登录数据库
- 二、修改密码
- 1、命令行修改
- 2、alter user 方式修改
- 3、set password for 方式修改
- 4、更新用户表 mysql.user 方式修改
前言
本环境是基于 Centos 7.8 系统构建MySQL-5.7.14
具体构建,请参考 MySQL-5.7.14 环境构建
一、登录数据库
MySQL基础操作:
1、启动/关闭:systemctl start/stop mysqld.service
2、登录与退出
1)mysql命令行
mysql -u用户 -p密码 -h IP地址 -D数据库名 -P端口
注意:-p密码 不能有空格,密码有特殊字符,需要用单引号括起来。
2) 退出
\q
quit
exit
登录据库
#获取初始密码
[root@mysql-server ~]# awk '/temporary password/ {print $NF}' /var/log/mysqld.log
O/Q-owypP9xl
#登录数据库
[root@mysql-server ~]# mysql -uroot -pO/Q-owypP9xl -h localhost -D test -P 3306
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1049 (42000): Unknown database 'test'
[root@mysql-server ~]# mysql -uroot -pO/Q-owypP9xl -h localhost -D mysql -P 3306
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 4
Server version: 5.7.14
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>
mysql> alter user root@localhost identified by 'ABCabc123!';
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.14 |
+-----------+
1 row in set (0.00 sec)
mysql>
二、修改密码
1、命令行修改
空密码修改
[root@mysql-server ~]# mysqladmin -uroot password '123abcABC!'
非空密码修改
[root@mysql-server ~]# mysqladmin -uroot -p'ABCabc123!' password '123abcABC!'
2、alter user 方式修改
mysql> alter user root@localhost identified by 'abcABC123!';
Query OK, 0 rows affected (0.00 sec)
3、set password for 方式修改
mysql> set password for 'root'@'localhost' = 'abc123ABC!';
Query OK, 0 rows affected (0.00 sec)
4、更新用户表 mysql.user 方式修改
mysql> update mysql.user set authentication_string=password('123abcABC!!')
-> where user='root' and host='localhost';
Query OK, 1 row affected, 1 warning (0.10 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)三、重置密码
#跳过权限表
[root@mysql-server ~]# mysqld --user=mysql --skip-grant-tables
#登录数据库,修改密码
[root@mysql-server ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.14 MySQL Community Server (GPL)
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>
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> alter user root@localhost identified by 'ABCabc123!';
Query OK, 0 rows affected (0.00 sec)
mysql>
#停止数据库
[root@mysql-server ~]# killall mysqld
#启动服务
[root@mysql-server ~]# systemctl start mysqld
#登录成功
[root@mysql-server ~]# mysql -u root -p'ABCabc123!'
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 2
Server version: 5.7.14 MySQL Community Server (GPL)
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>