首页 > 解决方案 > 如何连接 Mysql 和 phpMyAdmin

问题描述

我正在使用带有 xampp 的 Linux,我遇到了 MySQL 的问题。

当我使用这个命令sudo /opt/lampp/lampp start时,它会正确启动一切,当我sudo service mysql start在 xampp 的 MySQL 已经运行时使用时,该命令会给我这个错误:

Job for mysql.service failed because the control process exited with error code.
See "systemctl status mysql.service" and "journalctl -xe" for details.

反之亦然,如果我sudo opt/lampp/lampp start在 MySQL 服务已经运行时使用此命令,我会在 PHPMyAdmin 页面上收到此错误:

MySQL said:

Cannot connect: invalid settings.

mysqli::real_connect(): (HY000/2002): No such file or directory

Connection for controluser as defined in your configuration failed.

phpMyAdmin tried to connect to the MySQL server, and the server rejected the connection. You should check the host, username and password in your configuration and make sure that they correspond to the information given by the administrator of the MySQL server.

除此之外:当我尝试命令sudo mysqland时show databases;,我得到的数据库与 PHPMyAdmin 上的不同,所以这些是 MySQL 命令中的数据库:

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           |
+--------------------+
| Products           |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

MySQL> 

但这些是我在 PHPMyAdmin 中的数据库:

它们完全不同,当我在其中一个上创建数据库时,它不会出现或不存在于另一个上,它们彼此无关。

这对我来说似乎很困惑,我不知道该怎么办,我已经尝试了所有方法,但我坚持了 4 天,不幸的是,我不是那种不了解情况就继续前进的人这个问题,尤其是在我学习的时候,所以如果有人对此有解决方案,那将不胜感激。

注意:我已经搜索了很多解决方案,在 youtube 上观看了几个小时,并阅读并遵循了很多不起作用的解决方案,我还从我的系统中完全删除了 apache、Mysql 和 xampp 并重新安装了它,并且没有什么对我有用,尽管它对其他人有用,所以我不是来这里浪费任何人的时间的。

标签: mysqllinuxapachephpmyadminxampp

解决方案


这是找出解决方案后对我的问题的答案

解释:

确实它们是不同的Mysql数据库,并且从phpMyAdmin使用它非常简单,但是mysql命令不会与xampp的Mysql链接,因为它们是不同的数据库服务器。

要从终端使用 xampp 的 Mysql:

  • 使用以下命令停止不属于 xampp 堆栈的 Mysql 服务:

    sudo service mysql stop

  • 启动 xampp:

    sudo /opt/lampp/lampp start

现在您可以打开 phpMyAdmin 并从那里与您的 SQL 表进行交互。

  • 要从终端打开 xampp 的 Mysql (MariaDB),请使用以下命令:

    sudo /opt/lampp/bin/mysql -u root -p

如果您有 phpMyAdmin root 的密码,请在询问时输入密码,如果没有,请按 Enter。

这是你应该得到的:

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.4.21-MariaDB Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
  • 进入 Mysql 控制台后,使用以下命令:

    show databases;

在这里,您拥有与 phpMyAdmin 相同的数据库。

这是 xampps 的 Mysql,但另一个不是,不幸的是,这就是 Mysql 命令和 phpMyAdmin 的 Mysql 之间发生冲突的原因,这就是它们彼此不同的原因。

提示:您可以将此命令的别名sudo /opt/lampp/bin/mysql -u root -p变成mysql,只是为了使用简单。


推荐阅读