为什么修改密码
MySQL8版本安装的时候,不再要求用户设置密码。安装成功后,第一次登录不需要密码,但是必须设置一个密码,否则退出后没有密码无法登录数据库了。
第一次免密码登录
安装成功后,第一次登录无需密码,即无需指定
“`-p“`参数即可登录
ubuntu@VM-0-3-ubuntu:~$ mysql -h localhost -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.30-0ubuntu0.20.04.2 (Ubuntu)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> use mysql;
- 修改密码
> 修改root密码,并刷新权限
>
mysql> alter user'root'@'localhost' identified by 'hrzone+cn';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
- 登录验证
> 退出数据库,再次登录验证密码是否设置成功
> 这次登录,需要使用密码,因此要使用
“`-p“`参数
ubuntu@VM-0-3-ubuntu:~$ mysql -h localhost -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.30-0ubuntu0.20.04.2 (Ubuntu)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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.