简体   繁体   English

无法连接到 Apache 服务器上的 MySQL

[英]Unable to Connect to MySQL on Apache Server

I'm trying to setup a new local web server.我正在尝试设置一个新的本地 web 服务器。 Apache is already running and php is working. Apache 已经在运行,并且 php 正在运行。 Mysql is installed. Mysql 已安装。 I can run mysql through the MySQL workbench.我可以通过 MySQL 工作台运行 mysql。 However, I am not able to connect to the MySQL from the php document.但是,我无法从 php 文档连接到 MySQL。

First, the guides I looked at said to add the extension in php.ini.首先,我查看的指南说要在 php.ini 中添加扩展名。 I went to php.ini-development and uncommented the line:我去了 php.ini-development 并取消注释该行:

extension=mysqli

After restarting the server this didn't help.重新启动服务器后,这没有帮助。 I also tried variations like extension=php_mysqli, and php_mysqli.dll.我还尝试了 extension=php_mysqli 和 php_mysqli.dll 等变体。

I do in fact have the mysql dll on my computer.事实上,我的电脑上有 mysql dll。 It is in:它在:

C:\php\ext\php_mysqli.dll C:\php\ext\php_mysqli.dll

My computer says the dll has not been accessed or modified since I downloaded it, so the Apache hasn't touched it at all.我的电脑说 dll 自从我下载它后就没有被访问或修改过,所以 Apache 根本没有碰过它。

Here is the code:这是代码:

<?
$servername = "localhost";

$username = "root";

$password = "*************";

$conn = new mysqli($servername, $username, $password);

if ($conn->connect_error) {

  die("Connection failed: " . $conn->connect_error);

}

echo "Connected successfully";

?> 

I get this error:我收到此错误:

Fatal error: Uncaught Error: Class 'mysqli' not found in C:\Websites\x.php:14 Stack trace: #0 {main} thrown in C:\Websites\x.php on line 14 Fatal error: Uncaught Error: Class 'mysqli' not found in C:\Websites\x.php:14 Stack trace: #0 {main} thrown in C:\Websites\x.php on line 14

The MySQL is set to port 3306, and mysqli.default_port is set to 3306 in httpd.conf. httpd.conf中MySQL设置为3306端口,mysqli.default_port设置为3306。

This is Windows 10. The MySQL version is 8.0.2.这是 Windows 10。MySQL 版本是 8.0.2。 How can I make PHP use the mysql library/talk to MySQL?如何让 PHP 使用 mysql 库/与 MySQL 交谈?

EDIT: Not the same as How to solve "Fatal error: Class 'MySQLi' not found"?编辑:与如何解决“致命错误:Class 'MySQLi' not found”不同? . . I have tried the solutions there, but none of the answers worked so far.我已经尝试了那里的解决方案,但到目前为止没有一个答案有效。

First, be sure you're editing the "right" php.ini file.首先,确保您正在编辑“正确”的 php.ini 文件。 There can be several php.ini files on your system so execute this PHP code to see which php.ini is loaded in the context of your database connection script:您的系统上可能有多个 php.ini 文件,因此执行此 PHP 代码以查看在数据库连接脚本的上下文中加载了哪个 php.ini:

var_dump(php_ini_loaded_file());

Then, try to specify in this php.ini the absolute path to MySQLi DLL file like this:然后,尝试在此 php.ini 中指定 MySQLi DLL 文件的绝对路径,如下所示:

extension=C:\php\ext\php_mysqli.dll

Note: Don't forget to restart Apache server after the modification of the php.ini.注意:修改php.ini后不要忘记重启Apache服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM