【问题标题】:failed to connect to MySQL at localhost: 3306 with user root无法在 localhost 连接到 MySQL:3306 使用 root 用户
【发布时间】:2019-01-05 09:07:57
【问题描述】:

我已经安装了 mysql server 和 mysql workbench。我需要使用工作台界面连接到 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> 

【问题讨论】:

    标签: mysql database ubuntu database-connection mysql-workbench


    【解决方案1】:

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

    您的选择是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      相关资源
      最近更新 更多