如何连接 Mysql 与 phpMyAdmin

How to connect Mysql with phpMyAdmin

我正在将 Linux 与 xampp 一起使用,我对 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.

反之亦然,如果我在 MySQL 服务已经 运行 时使用此命令 sudo opt/lampp/lampp start,我会在 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 mysqlshow 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 完全从我的系统中删除并重新安装,但对我没有任何作用,尽管它对其他人有用,所以我不是来这里浪费任何人的时间。

关于数据库丢失问题:你确定你只是运行MySQL?你不是有两个并排的数据库引擎 运行 吗?我不确定是否可以并排使用(例如)MariaDB 和 MySQL 运行,但如果是这样,这正是我预计会发生的问题。毕竟,他们可能使用了 80% 的相同路径和文件名。

我个人对手动安装 MySQL(或 MariaDB)、Apache 和 PHP 有更好的体验。然后使用 Docker 管理 PhpMyAdmin。然后您所要做的就是更改 PhpMyAdmin 的配置文件,它应该会在几分钟内顺利完成 运行。只是一个建议,我当然不知道你的设置和要求:-)

这是我想出解决办法后的问题的答案

解释:

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

要从终端使用 Mysql of xampp:

  • 使用此命令停止不属于 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 的密码,请在询问时输入密码,如果没有,请按回车键。

这是你应该得到的:

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 命令与 Mysql 之间发生冲突的原因phpMyAdmin,这就是它们彼此不同的原因。

提示: 你可以为这个命令起一个别名 sudo /opt/lampp/bin/mysql -u root -p 把它变成 mysql,只是为了简单使用。