Connect to MySQLi through a specific IP - php

Due to a network outage, our architecture changed for a week or two. Now I am required to have 2 IPs in the network, one with 192.168.1.* and the second with 192.168.2.* .
The web development server where the mysql databases are located is accesible only from the 192.168.2.* IP.
Because of that, mySQLi connection takes exactly 20 seconds which is unacceptable :D
This is what I've tried so far:
$link = mysqli_init();
if (!mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 3)) {
die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
}
mysqli_real_connect($link, $_env[$ENVIRONMENT]['host'], $_env[$ENVIRONMENT]['user'], $_env[$ENVIRONMENT]['pass'], $_env[$ENVIRONMENT]['db'], 3306) or die("Could not connect: ".mysqli_error($link));
Setting a connect timeout of 3 seconds does not resolve the problem. How should I fix this?
(I use Windows)

Related

SQL Debug Message: 2002) No connection could be made because the target machine actively refused it

I'm getting the almost infamous
(HY000/2002): No connection could be made because the target machine actively refused it
Connection failed: No connection could be made because the target machine actively refused it.
error messages in trying to get PHP to talk to MySQL.
Configure
PHP is 7.3.7 (NTS MSCV15 (Visual c++ 2017) x64)
MySQL is 8.0.17 on a localhost (127.0.0.1)
IIS Windows 10 (10.0.18362.1)
All of this is running locally on laptop at 127.0.0.1 (though the corporate network is a 10.0.?.?)
IT support has spent the best part of 2 hours looking at all network and firewall issues (to the point disabled all firewall and enabled basically all the ports) and nothing seems to get through
MySQL is running, the username and passwords are perfectly fine, the hosts file has a correct DNS entry and the code is correct.
<?php
$servername = "rackforms";
$username = "rackforms";
$password = "????????";
$port = "3312";
// Create connection
$conn = new mysqli($servername, $username, $password, $port);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
MySQL port has been deliberately moved to 3312. Use other MySQL instances on 3306. Really it shouldn't make difference.
I've looked at XAMPP and WAMP and other *AMP entries without much luck and spent way too much time on SO.
Any suggestions on what else to do?
Thanks
RESOLVED!!
Need to change the mysql.default_port in php.ini to the same port I am using for MySQL database. Not sure if there is a way to override this port using a connection string as (at least on Windows) seems to be ignored based on comments in php.ini

mysql connecting to remote host fails

In my script on de webserver I'm trying connect to mysql through the (good old) mysql_connect() to an ip (db server) in the same network.
MySQL keeps throwing me the error:
Access denied for user ''#'localhost' to database 'dbname'
This seems like the db is being searched on the localhost (webserver) instead the IP I've entered (db server).
I've checked the my.cnf and can't find a bind-address or whatever.
When I connect through mysqli_connect(), the connection can be made, so there shouldn't be a firewall issue I guess.
Reason I'm still using mysql_connect is because i'm transferring a big website to a new server, and there is no time to change the function through all the script.
Anybody familiar with this problem and got any suggestion? Thanks in advance!
Update: piece of code
$link = mysql_connect("12.34.56.78", "username", "password");
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('dbname', $link);
if (!$db_selected) {
die ('Can\'t use dbname : ' . mysql_error());
}
Make sure you have "SQL safe mode" disabled in your php.ini file.
From the documentation of mysql_connect on the server parameter:
server
The MySQL server. It can also include a port number. e.g. "hostname:port" or a path to a local socket e.g. ":/path/to/socket" for the localhost.
If the PHP directive mysql.default_host is undefined (default), then the default value is 'localhost:3306'. In SQL safe mode, this parameter is ignored and value 'localhost:3306' is always used.
Similar filtering applies to the username and password parameters:
The username paramter defaults to the name of the user that owns the server process and password defaults to an empty string.
You can read more on SQL safe mode from here:
http://us1.php.net/manual/en/ini.core.php#ini.sql.safe-mode
Looks like the php is searching on the localhost but is using the username "". I think you have a problem with your user/pass combination and are also putting the ip address in the wrong place. Could you paste your code?

Clueless on how to use fsockopen()

I am very new to php, and i am trying to connect to ftp and post a form, which has a few text fields, and 3 image upload, the images will be uploaded to the server. I am using godaddy, and they dont allow ftp_connect, only fsocketopen(),and only available on ports 80 (http) and 443(https). Can i have some advice on how to approach this(fsockopen)?
I researched and below is what i got, i assume the first part is server, second part is the port so i assume is 80(as godaddy said only that 2 ports are available), but what are the last 3? The $error_number,$error_string, and the last part?
Thanks for your time. Sorry that if the question is a newbie question. I researched for a while, i still can't fix it.
fsockopen('abc.com', '80', $error_number, $error_string, 30)
<?php
$ftp_user_name='name';
$ftp_user_pass='pass';
$connection = 'server';
$errno='';
$connect= fsockopen("abc.info", 80, $errno, $errstr, 30) or die ("Cannot connect to host");
$login = ftp_login($connect, $ftp_user_name, $ftp_user_pass);
if (!$connect)
{die ("FTP connection has encountered an error!");}
//exit;
if (!$login)
{die ("But failed at login Attempted to connect to $connection for user $ftp_user_name....");}
?>
At the risk of sounding conceited, RTM please.
From the PHP Docs:
errno If provided, holds the system level error number that
occurred in the system-level connect() call. If the value returned in
errno is 0 and the function returned FALSE, it is an indication that
the error occurred before the connect() call. This is most likely due
to a problem initializing the socket.
errstr The error message as a string.
timeout The connection timeout, in seconds.

PHP, MySQL, Lost connection to MySQL server during query, TurnKey Ubuntu/Linux,

Hi to all php and mysql experts,
I try to find some help or 'tutorial' or similar question/answer for my problem but it was unsuccessful.
I have install TurnKey Linux, php, mysql, successfuly width ip address: 172.##.##.## and I can connect to this server from another comp.
I was edit my my.cnf file, I was open port 3306 and I can connect via mysql -u root -p -h 172.##.##.## from another comp. I was add user:root that can connect from any host or IP address. Also I can make successful connections width mysqladmin or mysql workbench.
I can, also, make database and some tables on this server like db_test, tab_test etc. and if I use next script on this server its work very good:
<?php
$db_hostname="localhost";
$db_username="root";
$db_password="pxxxxx";
$db_connect=mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_connect) {
die('Could not connect: ' . mysql_error());
}
mysql_close($db_connect);
?>
But if I use next script it does not work and report to me error: Could not connect: Lost connection to MySQL server during query;
<?php
$db_hostname="172.##.##.##";
$db_username="root";
$db_password="pxxxxx";
$db_connect=mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_connect) {
die('Could not connect: ' . mysql_error());
}
mysql_close($db_connect);
?>
Also this script it does not work if I use another php server witch can see or ping this server.
Can somebody help me to resolve this problem! Please. I am desperate.
I have the same error, please go to xampp\phpMyAdmin\libraries\config.default.php
Modify the following code
existing one - $cfg['ExecTimeLimit'] = 300;
modify to - $cfg['ExecTimeLimit'] = 600;
This is getting a bit long for a comment....
It doesn't appear to be a connection error - that's a different error message.
Changing the configuration of PHPMyAdmin is not going to make any difference to other scripts.
It might help if you publish details of the mysql config from PHP. Also is your mysqld started with skip-name-resolve? How long does your example script take to fail?
One thing to watch for is that when the mysql client sees 'localhost' in the conection string, it tries to connect via a filesystem socket - not a network socket - do you get the same error when you supply the host as 172.##.##.## ?

MySQL Connection String Using PHP

I am trying to connect to the database that is hosted on my server through my local machine.
my server has cPanel 11, and it is a typical shared server, powered by CentOS, PHP and MySQL installed.
to be precise i am holding the reseller account in the same server. i want to access the database between different accounts or domains.
in MySQL connection String i tried defining the domain name, and it isn't working. here is what i tried.
<?php
$link = mysql_connect('mydomain.com', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
this is giving the following error
Warning: mysql_connect() [function.mysql-connect]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://bhatkalnews.com:3306) in C:\wamp\www\test\conn.php on line 4
Warning: mysql_connect() [function.mysql-connect]: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\wamp\www\test\conn.php on line 4
Fatal error: Maximum execution time of 60 seconds exceeded in C:\wamp\www\test\conn.php on line 4
what do i have to define in connection string to make it work?
Firewall ?
try
telnet mydomain.com 3306
on the command line.
If that doesn't work, the connection is blocked by a firewall, most likely
Did you try adding the port explicitly:
mysql_connect('mydomain.com:3306', 'mysql_user', 'mysql_password');
or the port youre running on if not the default 3306. This of course assumes your server allows remote connections.
Also make sure the user in quest has access from the IP address youre connecting from. This isnt youre issue right now, but it may be the next question you have after you get the server to respond :-)
Please refer,
http://forums.mysql.com/read.php?52,294772
You need to add your IP address/range on Access Host list in Remote MySQL in cPanel.

Categories