如何连接Mysql和phpMyAdmin



我使用Linux与xampp,我有MySQL的问题。

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

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:

艾哈迈德
  • information_schema
  • mysql
  • performance_schema
  • phpmyadmin
  • 产品
  • 测试

它们是完全不同的,当我在其中一个上创建数据库时,它不会在另一个上出现或存在,它们彼此之间没有任何关系。

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

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

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

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

这是我找到解决方案后的答案

解释:

这是真的,他们是不同的Mysql数据库,这是很简单的使用它从phpMyAdmin,但mysql命令不会链接到Mysql的xampp,因为他们是不同的数据库服务器。

从终端使用xampp的Mysql:

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

    sudo service mysql stop

  • 开始xampp:

    sudo /opt/lampp/lampp start

现在你可以打开phpMyAdmin并从那里与你的SQL表交互

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

    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命令和phpMyAdmin的Mysql冲突的原因,这就是为什么它们彼此不同的原因。

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