Forum Moderators: open

Message Too Old, No Replies

Setting Port and Socket params for mysqli real connect()

or, how to find the current port and socket for MySQL

         

csdude55

8:44 pm on Dec 13, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As I'm moving things to a new VPS, I'm setting up my PHP scripts on the new VPS to look at the old VPS for MySQL. This will let me transition slowly, and when everything's ready to go then I can move MySQL over last.

In doing this, I discovered that mysqli_connect() doesn't have a "flag" option (like MYSQLI_CLIENT_COMPRESS); if I want that then I need to use mysqli_real_connect().

The docs show that I need to use these params:

Host
Username
Password
Database name
Port
Socket
Flags

[php.net...]

I want to use a flag, but I don't know the port or socket. How do I tell it to "use the default"?

Failing that, how do I determine the actual port and socket being used so that I can plug them in manually?

Here's the code I'm using:

$dbh = mysqli_init();
if (!$dbh) { die('mysqli_init failed'); }

// I don't know if I really need these, but it's in the docs so I thought I'd test it
if (!mysqli_options($dbh, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) { die('Setting MYSQLI_INIT_COMMAND failed'); }
if (!mysqli_options($dbh, MYSQLI_OPT_CONNECT_TIMEOUT, 5)) { die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed'); }

// I'm just plugging in a whitespace and a comma until I figure out how to do this properly
if (!mysqli_real_connect($dbh, '123.45.67.89', '[USERNAME]', '[PASSWORD]', '[DATABASE_NAME]', , , MYSQLI_CLIENT_COMPRESS)) {
// do error stuff
}

robzilla

9:22 pm on Dec 13, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need to know the port or PHP won't be able to connect to MySQL on the remote server. The default port for MySQL is 3306, which you could open up in the firewall, ideally only to the new server's IP address. You may also need to configure bind-address in my.cnf.

In PHP, I think you can probably just use false as the value for the socket, since it won't be used anyway (you've specified an IP address for the host so a TCP connection will be used).

What's the latency between these servers?

csdude55

1:25 am on Dec 14, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What's the latency between these servers?

I'm not sure, I haven't actually done it yet... is there a way to test latency beforehand?

I'm hoping that this will be very temporary, though; maybe a week, at most. I was really hoping to have everything on the new VPS by Dec 15, then I could change the nameservers and have about 10 days for DNS to propagate everywhere before being charged for the old VPS for another month. But... things happen, and now I don't think I'm gonna make that deadline.

robzilla

8:53 am on Dec 14, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just ping one server's IP address from the other. Even 10ms is going to be quite noticeable, especially if you query your database a lot. You'll have some downtime anyway as you switch to the new database, so you might as well move everything at once. DNS usually propagates much more quickly than that. You can help speed it along by temporarily lowering the TTL for your DNS records.

If you need to actually change the nameservers, not just the records, I'd do that as soon as possible, and just point the records at the old server. (One of many reasons why I don't host my own nameservers.)

csdude55

7:33 am on Dec 15, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I gotcha... it's 38ms, so :-(

The issue I have is that my old VPS uses MySQL 5.5, and the new one is MariaDB 10.3. I know there are some differences, so I'm expecting at least a few kinks to have to fix! And the database is 11G... so copying them over for a test, then fixing it, then copying again is probably going to result in at least a few hours of downtime for a few days in a row. I was hoping not to do that, but maybe I don't have a choice...

csdude55

7:36 am on Dec 15, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If the database is local, is there any real advantage to using mysqli_real_connect()?

robzilla

7:53 am on Dec 15, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'd just copy the database to the new server, test it there thoroughly without changing DNS (use your local hosts file to connect to the site on the new server), then copy it again when everything looks good and disable the site on the old server.

If the database is local, is there any real advantage to using mysqli_real_connect()?

For you, probably not, no. mysqli_real_connect() lets you set a few more options like timeouts, charset and buffer sizes. Most of the time, mysqli_connect() will be sufficient.