【问题标题】:MySQL random unable to connect messagesMySQL 随机无法连接消息
【发布时间】:2013-05-08 12:38:54
【问题描述】:

我有一个 Apache 服务器,它连接到 MySQL 数据库服务器。我收到很多错误消息:

PHP 警告:mysqli::mysqli(): (HY000/2003): Can't connect to MySQL server on '10.0.0.13'

但大多数时候,连接没有问题,我可以telnet 10.0.0.13 3306 没有问题。如何找出问题所在?

源码是php脚本,大部分时间运行没有任何问题。这是我的连接方式:

error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors','On');
date_default_timezone_set ('Europe/Copenhagen');
Header('Content-type: text/xml');
$conn = new mysqli('10.0.0.13','webuser', 'xxxxxx', "np_indexes");

有什么建议吗?

此连接是否正确关闭?

$sql = "call spGetOrderDepthBySymbol('$stock', 0)";
    $result = $conn->query($sql);
    while($row = $result->fetch_assoc()) { ?>
        <row>
            <type static="true"></type>
            <shares flash="true"><?= $row['shares']<>"" ? number_format($row['shares'], 0, ',', '.') : '-' ?></shares>
            <price flash="true"><?= $row['price']<>"" ? number_format($row['price'], 2, ',', '.') : '-' ?></price>
        </row>
    <? }
    ?>
    <spacer static="true"></spacer>
    <? 
    $conn->next_result();
    $sql = "call spGetOrderDepthBySymbol('$stock', 1)";
    $result = $conn->query($sql);
    while($row = $result->fetch_assoc()) { ?>
        <row>
            <type static="true"></type>
            <shares flash="true"><?= $row['shares']<>"" ? number_format($row['shares'], 0, ',', '.') : '-' ?></shares>
            <price flash="true"><?= $row['price']<>"" ? number_format($row['price'], 2, ',', '.') : '-' ?></price>
        </row>
    <? } 
    mysql_close($result);
    ?>

【问题讨论】:

  • 你的mysql运行了吗?也许重启 mysqld 服务?
  • 可能存在连接限制。
  • 您记得在完成连接后关闭连接吗?即使(尤其是)您的程序提前出错?
  • MySQL 正在运行,99% 的时间都没有问题。重启不能解决这个问题,我试过了。我在哪里可以检查是否有连接限制?
  • 这可能是因为连接没有正确关闭。我关闭这个正确吗?在原始帖子中添加了一个脚本。

标签: php mysql apache


【解决方案1】:

也许这就是你的情况。 see here

解决方案说:login as root and do setsebool -P httpd_can_network_connect=1

【讨论】:

  • 我认为如果服务器拒绝来自网络的连接,错误将是持久的,而不是随机的。
【解决方案2】:

如果您的 MySQL 服务器在 Windows 上运行 - 请阅读本节:http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html#can-not-connect-to-server-on-windows

【讨论】:

  • 两个服务器都在运行 linux (Debian)
猜你喜欢
  • 1970-01-01
  • 2016-09-27
  • 1970-01-01
  • 1970-01-01
  • 2020-09-02
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
相关资源
最近更新 更多