Skip to content
Advertisement

connecting to mysql via ipaddress with MySQL and mysqli

I have weird problem.

I don’t have a domain name for my remote mysql database, so on my development machine I usually just connect to a remote mysql using the ip address.

On my development machine the ip address connection works for both mysql and mysqli connections.

However, when I upload to my live server (CentOS) (also connecting to the same remote mysql database), the ip address connection only works for my old mysql functions but not my new mysqli functions.

I’m not sure how to correct this, since this isn’t a problem for me on my development machine.

Below is information for development machine and then for live server

Development machine:

**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

Live Server

**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

The error I keep getting for mysqli functions is: Failed to connect to MySQL: Unknown MySQL server host ‘[ipaddress]:3306’ (3)

Advertisement

Answer

The strange answer in my case was to separate the port number as a separate parameter mysqli object construction.

So instead of $mysqli = new mysqli(“ipaddress:3306”, “my_user”, “my_password”, “world”); I did $mysqli = new mysqli(“ipaddress”, “my_user”, “my_password”, “world”, (int)$mysql_port);

I don’t know why that did the trick, but it did. I hope that helps someone else.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement