mysqli connect via IP address but not using host name - php

I do test run with mysqli on remote host, Working fine with IP address but its not working with fully qualified domain name.
DNS is correctly mapped,I can browse the page using browser and ping also success
$mysqli = new mysqli("www.testdomain.com", "uname", "passwd", "database");
Any suggestions ?
mysql is running on Centos 6

First, unless you have a particular reason why you have to add www. before testdomain.com(for example, testdomain.com and www.testdomain.com are pointing to different address), please try again without www. in your host name.
Try nslookup testdomain.com on the machine that hosts your php application(or write a php script and call gethostbyname), see if php can find the correct IP address from given host name.
If the IP address you got from last step is different from the one you tried, try to troubleshoot DNS resolution problem, or edit /etc/hosts file on the machine that hosts your php application, and point testdomain.com to correct IP address. Some servers may have multiple IP addresses bind to them, but MySQL server only listens to one of them, check /etc/mysql/my.cnf on the machine hosts your MySQL server and look for bind 1.2.3.4(or other host name/IP address). In case you don't already know, MySQL server only accepts connections from localhost by default.
If your MySQL server if configured to listen to a particular host name/IP address, try bind to 0.0.0.0 so your MySQL server allows connections from all sources. Also there could be a firewall blocking connections from other sources. Here's how to check firewall configuration on CentOS.
If your php script can resolve testdomain.com, try using mysql command-line client to connect to your database from the machine that hosts your php applicaiton. mysql -h testdomain.com -u uname -p. The command-line client may give you more information on why the connection failed.
I'm not sure if you're using hosted VPS services, such as Amazon EC2, or you have your own server machine, but Amazon EC2 does provide out-of-box firewall, it's called "Security Group". Login to your EC2 console, and click "Security Group" on left-hand-side panel, then you can edit your firewall settings on right-hand-side panel.

Related

How i execute a script use exec function in php? In a external server and hosting

I try running a php script on a external server and hosting. I selected byethost because it's a free service. All my system run excellent, except this script. I search information and i not found anything.
exec('mysqldump -u useronserver -ppasswordonserver clinicasbv >' . RESPALDOS_DB_L . date('Y_m_d_g_i_s', time() - (21600 - 3600)) . '_clinicasbv_backup.sql');
This code working perfect on my virtual host in my machine, but not work in byethost.
So, i need a root privileges or anything for running this script in a
external server? I need this script working on a external server, it's all
It is very likely that the hosting provider has blocked your ability to run the "exec()" function. It is technically a security risk to allow it. You should find another way to get the MySQL dump, i.e. connecting remotely using a MySQL system or using the PHP MySQLi functions.
To do this, the connecting host will preferably need a static IP address otherwise you'll need to re-do this guide each time their dynamic IP address from their ISP changes.
You can allow access from cPanel for MySQL itself. We'll also need to setup an allow rule through the firewall which is covered here.
Login to your cPanel.
Go to Databases ยป Remote MySQL.
Enter the IP address of the computer that will be making the remote connection.
Click the Add Host button.
Now we need to allow the person's IP to be able to connect to port 3306 which is closed by default. The best way to do this is with a firewall rule specific to their IP and port 3306 and how to do this for CSF is detailed here.

How to connect to remote DB server with IP address and mysqli_connect [duplicate]

I am trying to connect mysql database using php. But it is running fine on local machine but not when hosted online and error appeared as
mysqli_connect(): (HY000/2002): Connection timed out
<?php
$link = mysqli_connect('sql6.freesqldatabase.com','user_name','password','database_name');
if(mysqli_connect_error())
die("couldn't connect to database");
?>
Your MySQL server is probably not configured to accept requests from the host you're trying to access it from.
Check the configuration for the server.
Since you say the local machine can access it check where you permitted this and do the same for the ip of the online host.
In my case, AWS firewall was blocking my web server.
Adding the web servers IP address to the firewall exceptions fixed
this for me.
There might firewall in the host which is blocking the request to MySQL server. In my case adding the firewall rule helped me out.
Ubuntu
To allow one device to access the MySQL server,
sudo ufw allow from remote_IP_address to any port 3306
To allow any device to connect to MySQL server,
sudo ufw allow 3306
It worked!
If your website and MySQL database are hosted on separate servers, make sure that you've mentioned correct MySQL hostname (resolvable) in connection string.
In case you've specified the correct username, most probably your MySQL server isn't allowing remote MySQL connections from web server where you site resides.
Contact your web hosting provider for the exact MySQL hostname and to allow your web server IP address for remote MySQL connections.
I think you have to white list your hosting server's ip address to your DB server.
So your DB server will accept requests from your hosting server.
Specify the port (3306?) to the connection string as well.

curl: Ran on Different Servers Returned Different IP Addresses

I've been struggling to figure out why my page which uses curl library in PHP worked on my dev machine but failed on my remote server. One suspicion is that it's related to whether the curl has an SSL support, as indicated in one of the comments here:
If your curl installation is not compiled with SSL support you will beat your head against a wall when trying to figure out why curl_exec() is failing to fail or do anything else ...
So, I ran this script to find out whether SSL is supported on both my dev machine and remote server. Both my dev machine and remote server produced the following (looks like both support SSL):
CURL_VERSION_IPV6 matches CURL_VERSION_KERBEROS4 does not match CURL_VERSION_SSL matches CURL_VERSION_LIBZ matches
However, when I ran curl from the command line on my remote server (the one where my script failed), here's what I got (i.e. curl hang):
curl -v http://<my-domain>/blog/press-release/
* About to connect() to <my-domain> port 80
* Trying 66.201.45.235...
The same curl command line worked fine on my dev machine although it returned a different IP address:
curl -v http://<my-domain>/blog/press-release/
* About to connect() to <my-domain> port 80 (#0)
* Trying 10.3.1.207... connected
Please... please help? Any other way to figure out the differences between the installed curl on my dev machine and remote server? Why did they return different addresses?
A domain name (like my-domain.com) is resolved to an IP address through DNS.
When a program tries to access a network device by name, the operating system contacts it's configured DNS server and asks it for the IP address that corresponds to the supplied name. The DNS server will then either respond with an IP address (or set of IP addresses - it is possible to configure a name to resolve to more than one IP address) or an error message that says, in essence "I couldn't find that name". The operating system will also have a hosts file, which is a list of domain-to-IP mappings that are statically configured. If there is a match for the requested domain in the host file, the DNS server will not be contacted and the configured IP will be used instead.
A corporate LAN will often have it's own internal DNS server which controls an internal domain, and if it gets a request for a member of that domain, it will return the LAN IP address of that host, instead of the WAN IP address.
My guess for your problem is that one of two things is happening:
The hosts file on your dev server has a configured entry for the relevant domain, which is internal to you network. I say this because the address you get on your dev machine resolves to an IP address that lies in a range normally reserved for LANs
The machine you are trying to contact is on your own domain, hosted inside the network where your dev machine resides, and your LAN has a DNS server which controls your domain, used by you dev machine for DNS lookups.
Either way, the actual problem that you need to resolve is that the host you are trying to contact does not accept connections from the WAN on the 66.201.45.235 address, which is what the internet sees it's IP address as. This could be because of server configuration, firewalls, NAT or any one of a number of other things.
This is not likely to be anything SSL related - cURL is trying to connect to port 80, which is used for un-encrypted HTTP connections, and does not require SSL.
You should contact your network administrator to resolve the issue.

Host my wordpress site locally using the server database

I have set up my wordpress site on my local machine and I would like it to talk to the live mysql database on the server. I accessed the wp-config.php file on my machine and changed the hostname to use the ip address instead of localhost, but it will not work.
What do I need to do?
It could be that your remote database is configured to accept connections only from localhost for security reasons. Most web providers set it up that way. In that case, you have no chance of making this work.
Anyway, even if you would get it to work, you will encounter two problems: It will be awfully slow, and the HTML served by the remote database will contain references to server URLs (as opposed to local ones).
If you need more detailed information, you will need to post any error messages you get from mySQL.
Alternatively, you can try running the mysql command-line utility to connect.
mysql -u username -h server -p wordpress_database
Make sure that works first before attempting to get Wordpress to connect.
My guess is you won't be able to connect due to firewall issues. MySql uses port 3306 by default, so if the server's firewall doesn't allow connections through that port, you'll either have to change the port mysql is using (probably a bad idea if other apps expect to see MySql on that port) or get that port opened on the server (make sure you only open it for local IPs, so someone else can't get to your MySql instance!)

mysql_connect() works fine on localhost but not when accessing seperate ip

I'm working with my University's Systems Administrators to get a LAMP stack setup for me. I'll need to access this server from several websites that I'm working on and I'm having some issues.
So to keep thing's clear:
LAMP Server URL = https://mysqlserver.edu
School URL = https://schoolsite.edu
When I run mysql_connect() on the LAMP Server it connects fine:
mysql_connect('localhost', 'user', 'password'); Works great!
However, when I run mysql_connect() on the School URL I can't connect:
mysql_connect('mysqlserver.edu', 'user', 'password');
Warning: mysql_connect(): Unknown MySQL server host 'mysqlserver.edu' (1) in /home/content/x/x/x/xxx/html/testconnect.php on line 3
Unknown MySQL server host 'mysqlserver.edu' (1)
What do I need to ask the System Administrator to do in order to give my PHP scripts on external sites access to the MySQL server?
Does the SSL complicate issues?
I appreciate any insight you might be able to provide.
Unknown MySQL server host probably means that your machine cannot resolve to hostname mysqlserver.edu, have you tried connecting via IP address?
After you make sure you are resolving the name, you have to overcome three further barriers:
firewalls as others have shown,
MySQL server configuration which has to allow remote connections and
a user able to connect remotely has to be setup.
MySQL user depends on the computer it is access from. 'root#localhost' is not the same root as 'root#192.168.0.156'. So you have to add the user from the computer you are going to access from. So let say the computer where php script is served is '192.168.156' you need to add a user 'user#192.168.0.156' to the MySQL server and don't forget to set the privilege for that user to access to the database needed.
Hope this helps
It's rare to make a MySQL connection over a public WAN. It's common to take a LAN hop or two but you usually can't cross a firewall for security reasons. That is, incoming TCP connections to port 3306 are being blocked by the firewall or gateway onto the local LAN where mysqlserver.edu is.
The canonical test for this is:
$ telnet mysqlserver.edu 3306
I can predict that this will not work. ("Work" means that it reports "Connected to..." and outputs some gibberish.) You have several choices:
run the database locally, or at least nearby. I mean, it's not a LAMP stack unless it has MySQL. :-) This is what approximately everyone does.
use a tunnel / VPN solution of some kind to get through the firewall and to port 3306
open up 3306 on the firewall. You probably will not get cooperation on this.
Is "skip-name-resolve" option enabled in your my.cnf?

Categories