mysql8 安装后无法登录的问题

使用 apt 安装mysql 先search一下

sudo apt search mysql

得到结果

找到了这个 发现是8 那就装吧

sudo apt-get install mysql-server

安装后发现无法登录

$ mysql -uroot -p
Enter password: 
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

此时可以查看 文件

$ sudo cat /etc/mysql/debian.cnf 
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = koJJxVpPDRINkIE6
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = koJJxVpPDRINkIE6
socket   = /var/run/mysqld/mysqld.sock

然后使用这个用户密码登录

mysql -u debian-sys-maint -pkoJJxVpPDRINkIE6

发现可以登录进来  然后选择 mysql 数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.08 sec)

mysql> use mysql;

改密码 刷新权限

mysql> alter user 'root'@'localhost' identified with mysql_native_password by '新密码';
Query OK, 0 rows affected (0.10 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

然后退出使用新密码重新登录看到可以登录了

$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 15
Server version: 8.0.23-0ubuntu0.20.04.1 (Ubuntu)

Copyright (c) 2000, 2021, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> exit
Bye
原文地址:https://www.cnblogs.com/lizhaoyao/p/14662968.html