使用mysql和mysqli通过ip地址连接到mysql

时间:2014-02-22 18:20:02

标签: php mysql mysqli

我有奇怪的问题。

我没有远程mysql数据库的域名,所以在我的开发机器上我通常只使用ip地址连接到远程mysql。

在我的开发机器上,ip地址连接适用于mysql和mysqli连接。

然而,当我上传到我的实时服务器(CentOS)(也连接到相同的远程mysql数据库)时,ip地址连接仅适用于我的旧mysql函数,但不适用于我的新mysqli函数。

我不确定如何纠正这个问题,因为这对我的开发机器来说不是问题。

下面是开发机器的信息,然后是实时服务器的信息

开发机器:

**MySQL**
MySQL Support   enabled
Active Persistent Links 0
Active Links    0
Client API version  mysqlnd 5.0.8-dev - 20102224 - $Id: 65fe78e70ce53d27a6cd578597722950e490b0d0 $

Directive   Local Value Master Value
mysql.allow_local_infile    On  On
mysql.allow_persistent  On  On
mysql.connect_timeout   60  60
mysql.default_host  no value    no value
mysql.default_password  no value    no value
mysql.default_port  no value    no value
mysql.default_socket    no value    no value
mysql.default_user  no value    no value
mysql.max_links Unlimited   Unlimited
mysql.max_persistent    Unlimited   Unlimited
mysql.trace_mode    Off Off

**mysqli**

MysqlI Support  enabled
Client API library version  mysqlnd 5.0.8-dev - 20102224 - $Id: 65fe78e70ce53d27a6cd578597722950e490b0d0 $
Active Persistent Links 0
Inactive Persistent Links   0
Active Links    0

Directive   Local Value Master Value
mysqli.allow_local_infile   On  On
mysqli.allow_persistent On  On
mysqli.default_host no value    no value
mysqli.default_port 3306    3306
mysqli.default_pw   no value    no value
mysqli.default_socket   no value    no value
mysqli.default_user no value    no value
mysqli.max_links    Unlimited   Unlimited
mysqli.max_persistent   Unlimited   Unlimited
mysqli.reconnect    Off Off

直播服务器

**mysql**
MySQL Support   enabled
Active Persistent Links 0
Active Links    0
Client API version  5.0.22
MYSQL_MODULE_TYPE   external
MYSQL_SOCKET    /var/lib/mysql/mysql.sock
MYSQL_INCLUDE   -I/usr/include/mysql
MYSQL_LIBS  -L/usr/lib/mysql -lmysqlclient

Directive   Local Value Master Value
mysql.allow_local_infile    On  On
mysql.allow_persistent  On  On
mysql.connect_timeout   60  60
mysql.default_host  no value    no value
mysql.default_password  no value    no value
mysql.default_port  no value    no value
mysql.default_socket    /var/lib/mysql/mysql.sock   /var/lib/mysql/mysql.sock
mysql.default_user  no value    no value
mysql.max_links Unlimited   Unlimited
mysql.max_persistent    Unlimited   Unlimited
mysql.trace_mode    Off Off

**mysqli**

MysqlI Support  enabled
Client API library version  5.0.22
Active Persistent Links 0
Inactive Persistent Links   0
Active Links    0
Client API header version   5.0.95
MYSQLI_SOCKET   /var/lib/mysql/mysql.sock

Directive   Local Value Master Value
mysqli.allow_local_infile   On  On
mysqli.allow_persistent On  On
mysqli.default_host no value    no value
mysqli.default_port 3306    3306
mysqli.default_pw   no value    no value
mysqli.default_socket   no value    no value
mysqli.default_user no value    no value
mysqli.max_links    Unlimited   Unlimited
mysqli.max_persistent   Unlimited   Unlimited
mysqli.reconnect    Off Off

我一直在为mysqli函数获取的错误是: 无法连接到MySQL:未知的MySQL服务器主机'[ipaddress]:3306'(3)

1 个答案:

答案 0 :(得分:0)

在我的案例中,奇怪的答案是将端口号作为单独的参数mysqli对象构造分开。

所以而不是    $ mysqli = new mysqli(“ipaddress:3306”,“my_user”,“my_password”,“world”); 我做到了    $ mysqli = new mysqli(“ipaddress”,“my_user”,“my_password”,“world”,(int)$ mysql_port);

我不知道为什么会这样做,但确实如此。我希望能帮到别人。