首页 > 解决方案 > 无法在 localhost 连接到 MySQL:3306 使用 root 用户

问题描述

我已经安装了 mysql 服务器和 mysql 工作台。我需要使用工作台界面连接到 mysql。我以前在 Windows 中很容易做到这一点,因为服务器安装也是在图形用户界面中。

但现在我在 Ubuntu 18 机器上工作。当我输入root密码后尝试测试连接时,出现此错误:

我执行此命令以确保密码正确:

mysql> UPDATE mysql.user set authentication_string = PASSWORD('mypass') where user = 'root' and host = 'localhost';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> exit()

然后当我尝试这个并输入密码时,我再次收到错误:

x@x-VirtualBox:~$ mysql -u root -p -h > localhost -P 3306
Enter password: 
ERROR 2005 (HY000): Unknown MySQL server host '-P' (2)

谁能告诉我怎么了?如何建立数据库连接?

编辑: 该服务肯定正在运行。这是测试:

x@x-VirtualBox:~$ service mysql status
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: en
   Active: active (running) since Sat 2018-07-28 20:54:14 WIB; 6min ago
  Process: 12790 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/m
  Process: 12779 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exi
 Main PID: 12792 (mysqld)
    Tasks: 28 (limit: 4915)
   CGroup: /system.slice/mysql.service
           └─12792 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pi

Jul 28 20:54:13 e-VirtualBox systemd[1]: Starting MySQL Community Server...
Jul 28 20:54:14 e-VirtualBox systemd[1]: Started MySQL Community Server.

在此处输入图像描述

编辑2: 使用命令行,我输入root密码并且它可以工作。但是工作台不接受。

sudo mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.22-0ubuntu18.04.1 (Ubuntu)

Copyright (c) 2000, 2018, 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> 

标签: mysqldatabaseubuntudatabase-connectionmysql-workbench

解决方案


您的mysql.user表有几个根用户的条目,并且根据您的连接方式使用不同的条目(例如,通过本地 unix 域套接字或通过 tcp 端口)。您似乎更改了其中一个条目的密码,但没有更改其他条目的密码。

您的选择是:

  • mysql.user更改或中所有 root 用户条目的密码
  • 将工作台中的“连接方法”(如屏幕截图所示)从“标准”更改为“本地套接字”,以便它使用与通过命令行登录时相同的密码

推荐阅读