简体   繁体   English

警告:mysqli_connect():MySQL 服务器已经消失

[英]Warning: mysqli_connect(): MySQL server has gone away

I wrote a simple PHP code to connect to the MySQL server as below我写了一个简单的 PHP 代码来连接到 MySQL 服务器,如下所示

<?php

$username = "root";
$password = "Kepwd";
$hostname = "localhost:81";

//connection to the database
$dbhandle = mysqli_connect($hostname, $username, $password)
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

but this generates the following errors.但这会产生以下错误。 I found some topics regarding this issue in google and Stack Overflow.我在 google 和 Stack Overflow 上找到了一些关于这个问题的主题。 but those don't help me.但那些对我没有帮助。

( ! ) Warning: mysqli_connect(): MySQL server has gone away in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
#   Time    Memory  Function    Location
1   0.0014  240936  {main}( )   ..\test.php:0
2   0.0014  241528  mysqli_connect ( )  ..\test.php:8

( ! ) Warning: mysqli_connect(): Error while reading greeting packet. PID=10612 in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
#   Time    Memory  Function    Location
1   0.0014  240936  {main}( )   ..\test.php:0
2   0.0014  241528  mysqli_connect ( )  ..\test.php:8

( ! ) Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in C:\wamp\www\SSDConsultingNew\inc\test.php on line 8
Call Stack
#   Time    Memory  Function    Location
1   0.0014  240936  {main}( )   ..\test.php:0
2   0.0014  241528  mysqli_connect ( )  ..\test.php:8
Unable to connect to MySQL

The error is here: 错误在这里:

$hostname = "localhost:81";

You are not connecting to MySQL, but to Apache server. 您不是在连接MySQL,而是在Apache服务器上。 If you didn't change MySQL port just use 如果您没有更改MySQL端口,请使用

$hostname = "localhost";

you forget to specify the database name after entering database name try again. 您忘记输入数据库名称后再指定数据库名称,然后重试。 The syntax should be like this 语法应该像这样

<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

I found this useful for my case, stuck into same/similar problem.我发现这对我的情况很有用,陷入了相同/类似的问题。

<?php 
$host = "localdomain.local";
$port = 3306;

$mydatabase['default']['default'] = array(
  'database' => "db_name",
  'username' => "db_user",
  'password' => "",
  'host' => $host,
  'driver' => "mysql",
  'port' => $port,
  'prefix' => "",
);

if($mydatabase) {
    echo "true";
}

I hope this can be helpful for somebody...我希望这对某人有帮助......

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

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