为什么我会得到mysql_connect():失去与MySQL服务器的连接,我该如何解决? [英] Why am I getting mysql_connect(): Lost connection to MySQL server and how can I fix it?

查看:92
本文介绍了为什么我会得到mysql_connect():失去与MySQL服务器的连接,我该如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的php代码无法连接到我的Web服务器上的mysql数据库.我收到以下错误:

My php code fails to connect to the mysql database on my web server. I get the following error:

mysql_connect():在读取初始值时失去与MySQL服务器的连接 通信数据包",系统错误:第71行的文件名中为110.

mysql_connect(): Lost connection to MySQL server at 'reading initial communication packet', system error: 110 in filename at line 71.

从我的开发机运行时,指向我要从其运行服务器上的mysql数据库的相同代码可以正常工作(我将文件复制到服务器上,并尝试通过ssh在服务器上运行该服务器).我已经验证脚本中的mysql用户可以通过使用与我的php脚本中指定的用户名和密码相同的用户名和密码从该服务器上的ssh命令行运行mysql,从而从服务器连接到数据库.

The same code works fine when run from my development machine pointed at the mysql database on the server I'm trying to run this from (I copied the file up to the server and I'm trying to run it via ssh on that server). I have verified that the mysql user in the script can connect to the database from the server by running mysql from the ssh command line on that server using the same user name and password as specified in my php script.

这是我的代码:

function jsw_set_cnxn($env){
        global $env;
        $cnxn = mysql_connect($env['db_svr'], $env['db_usr'], $env['db_pwd']) 
               or die ('DB Connection Error: ' . mysql_error());
        mysql_select_db ($env['db'], $cnxn);
    return $cnxn;
}

推荐答案

当使用服务器IP地址并希望将localhost作为服务器地址时,我已经看到此错误.

I've seen this error caused when using the servers IP address when it expects localhost as the server address.

这是因为服务器是mysql期望的机器,但是主机输入错误.请尝试使用localhost.

This is because the server is the machine mysql expects but the wrong host coming in. Try using localhost.

这篇关于为什么我会得到mysql_connect():失去与MySQL服务器的连接,我该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆