Website Connection to Local MySQL Server - php

I have recently configured a MySQL server on my Windows home computer. In addition, I have also created a website using 0fees.net-- a free hosting provider which comes with a vista panel that has various services including PHP support, FTP file-hosting, its own MySQL server etc.
For that website, I have created a "login" PHP script in order for people to login to my webpage. However, instead of reading from the MySQL database given to me on the cPanel on 0fees.net, I want the PHP logon script to read directly from the MySQL server that I have configured on my home computer. To do this, I have taken several steps:
1) Configured MySQL using the MySQL Server Instance Configuration Wizard
In that configuration, I have enabled TCP/IP Networking on port 3306 (and enabled a firewall exception for that port) and have enabled root access from remote machines.
2) On the MySQL command-line-client, I have granted remote access capabilities to other units by using:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' IDENTIFIED BY 'password';
3) I have forwarded port 3306 to my router (and have used various port checkers to confirm that it is open)
4) I have created the login script called "login.php" which lies in my site's .htdocs directory and that process an HTML login form on the homepage of my website. It attempts (and fails) to connect to my local MySQL server. The segment of interest of that php script is:
$host="my_external_ip:3306"; // External IP of my computer and port to listen on
$username="root"; // Username specified in the GRANT command-line-client command
$password="password"; // Password specified in the GRANT command-line-client command
$db_name="logon"; // MySQL database name
$tbl_name="accounts"; // Table name within the database
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die(mysql_error());
mysql_select_db("$db_name")or die("cannot select DB");
When attempting to connect to the MySQL sever, I get an error message that states:
Can't connect to MySQL server on 'my_external_ip' (4).
It should also be noted that I am successfully able to connect to my local MySQL server remotely from other machines which are on other internet networks (tested with Navicat) using this same external IP address on port 3306. Any ideas as to why my website cannot also connect?
Thanks in advance for any insights.

Since I can tell that your server is definitely running (and visible from the internets in general), there are a few possibilities:
You forgot to run FLUSH PRIVILEGES
Your username or password are wrong
Your free host provider is blocking the connection
My money is on #3, there are a lot of incentives to do that.

#Chris mentioned that your provider might be blocking outbound requests on port 3306. Here's a quick way to check if that's the case and get around it:
Configure your router to port forward from port 80 on the router to port 3306 on your box. This will allow you to connect to MySQL from outside your network on port 80 instead of port 3306.
Change your PHP code to connect to your IP on port 80 instead of 3306.
Your hosting provider might block outbound port 3306, but the almost certainly wouldn't block outbound port 80 requests (lots of people using CURL or web services and such).

Well I will let you in on a little trick I learned, all you got to do is go into wamp>mysql>my.ini and replace all 3306 with 3305 then save and then apply this to your firewall and port forward 3305. Then when you are connecting remotely, simply do
mysql_connect("ip adress:3305", "username", "password");
I've tested this and it works because the web hosting sites block incoming traffic from port 3306.

Related

MySQL database IP address from rackspace or google cloud?

Ok here’s the deal. I setup MySQL database on Rackspace and I’m trying to connect to it. I am using the tutorial google maps to create store locator using MySQL. The line of the code in the tutorial asks for the host name and I’m giving the IP address of my server on Rackspace. I’m using what I think is the correct one but it’s not working. Any ideas?
Here's the google tutorial code:
// Opens a connection to a mySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
die("Not connected : " . mysql_error());
};
My code:
$connection=mysqli_connect ('THE IP ADDRESS OF MY RACKSPACE SERVER', 'MY USERNAME', 'MY PASSWORD');
Here's a link to the tutorial from google: https://developers.google.com/maps/solutions/store-locator/clothing-store-locator
I know I have the server IP correct and my username/password correct since I'm able to login via SSH from terminal with those credentials.
When I use mysql> \s to show the status it says Connection: Localhost via UNIX socket - does this mean it's local host? I need it hosted on IP to connect right?
I expect that your MySQL server is by default by your Linux distribution's packaging configured to listen either locally, or on the socket file only. You can update your DB config to listen on the public IP address of your server however obviously this can come with some security implications.
To do this edit the /etc/mysql/mysqld.conf file (this may be in a slightly different location depending on distribution being used) and the following line as such...
From
#bind-address = 127.0.0.1
To
bind-address = 0.0.0.0
Now restart your MySQL service using the systemctl or service command.
service mysql restart
Your MySQL server is now listening on ALL the host's IP addresses. If you want to limit it to just one you should enter that IP instead of 0.0.0.0. You should now be able to connect to your MySQL server remotely, however, you must have already configured your database user to be able to login from the webserver. If you haven't configured the user yet do something like this.
mysql
CREATE USER '<username>'#'<webserver ip address here>' INDENTIFIED BY '<password>';
GRANT ALL PRIVILEGES ON <database>.* TO '<username>'#'<webserver ip address>';
FLUSH PRIVILEGES;
You should now be able to login as this user and view / modify / insert etc... data to the database specified from the server IP address specified. You can test this from the web server using the MySQL client like this...
mysql -u <username> -h <db server ip> -p

PHP - Can't connect to a remote MySQL database

I have a MySQL database, hosted by me on a Windows server, and I would like to query it from a remote webserver. When I try to connect to the MySQL database using PHP (I know, I should be using mysqli):
$connection = #mysql_connect("203.0.113.0:3306", "username", "password");
With or without specifying the port, after a long time of loading, I get this error with an errorno of 2003:
Can't connect to MySQL server on '203.0.113.0' (4)
Here is what I get when I run netstat -n in command prompt on the server that is hosting the MySQL server: http://pastebin.com/pbRJNqCZ. It filled up the screen so I couldn't copy everything, but I know that everything else was (I saw a couple ports with a value of 3306, which is the MySQL port):
TCP 127.0.0.1:port 127.0.0.1:port ESTABLISHED
When I run netstat -a | find "LISTENING" I get: http://pastebin.com/Lqj2BrQK
Here's what I know so far:
It isn't an error with the MySQL server not being up, because I can connect to it locally.
It isn't an error with the webserver not being able to connect to MySQL databases, because I can connect to other databases
It isn't an authentication error (The username and password are correct, and the remote server has permission)
It isn't a port forwarding error, because the port 3306 is fowarded on both TCP & UDP.
It isn't an error with being able to connect to the machine the server is hosted on, because I can ping it fine.
The server isn't only listening on the localhost interface. skip-networking and bind-address are commented out in my my.cnf file.
How could I edit my connection code, or edit my MySQL server to fix this error?
Summarizing our discussion/chat:
Bind the network address 0.0.0.0 in my.cnf: bind-address = 0.0.0.0 and ensure skip-networking is commented out or not present.
Check netstat -a | find "LISTENING"
According to your pastebin there is a service listening on 3306. Test if the port is reachable on the NIC address from the server itself. This way an external firewall does not take effect. A simple test is to try a telnet connection to that port. More detailed information can be catched by the tool nmap. Nmap has displayed the mysql port as filtered. This adverts to a problem with a local packet filter, in this case the Windows firewall.
Ensure the Windows firewall is configured to allow public access to TCP port 3306 from all or a specific machine. Setup a rule in public profile or, if both servers are controled by the same domain controller, in domain profile. When the access from local machine is successful try the same from the remote web server.
If you fail to properly configure remote access to MySql port, consider to establish a SSH tunnel between the two machines. Once established you can access to MySql as if it were on the local machine. The port is then forwarded via the tunnel and on the database server side you can access the service on localhost loopback IP.

Can't connect to MySQL server on 'ipaddress' (110)

So I have a PHP file hosted on Namecheap server.
$db=mysql_connect ("ipaddress", "user", "pass") or die ('I cannot connect to the database because: ' . mysql_error());
and it gives this error:
I cannot connect to the database because: Can't connect to MySQL server on 'ipaddress' (110)
I CAN connect to this DB using mysql workbench outside of the network just fine.
I have experienced this issue. What I did was use the internet address instead of your public IP/DNS.
Since, I'm using Linux I do ifconfig and you will see inet addr: xxx.xxx.xxx.xxx and use that IP as your host instead of the public IP/DNS. On Windows, Simply use your local IP address.
That's it!
If you are using MySQL for your database solution (which seems odd due to the usage of IIS on a Windows Server operating system)
Try running (As Root):
GRANT ALL ON Database.* TO Username#'IPAddress' IDENTIFIED BY 'PASSWORD';
Where the second is the permissions that you are granting on, this is a place holder for all
This will allow a connection from the IP you specify
also A problem is with connecting to your MySQL engine from inside your network, you will naturally connect from an internal IPV4 Address (192.168.0.x for example) this does not require portforwarding. BUT if you are using:
mysql_connect('WANIP', 'User', 'password'); you will have to forward port 3306 to your server. http://www.portforward.com for assistance.
Edit:
http://dev.mysql.com/doc/refman/5.0/en/can-not-connect-to-server.html
The manual for this subject, this may provide some assistance
If you are using Microsoft SQL Server check this link out:
http://blogs.msdn.com/b/walzenbach/archive/2010/04/14/how-to-enable-remote-connections-in-sql-server-2008.aspx
I have faced this issue when installing opencart on my server. When i was using Windows server it requires IP address of website/domain. Now when shifted to linux hosting, linux hosting accpted as localhost And done. I could install opencart

mysql connection from a different domain

I have website example.com, it contains a MySQL server. I have another example.org, both are different domains. I want to access the MySQL server on example.com from example.org. How would it be possible?
You will need to set the remote example.com when you call the database connection initialization function. For example:
mysqli_connect("example.com", 'username', 'password', 'database name');
But you will need to check whether example.com's MySQL server is set to accept connections from other hosts (see the bind-address directive in your my.cnf or my.ini), and that the username you connect with is set to be able to connect from external domains.
Consider the following Users page of phpMyAdmin:
It is clear that only user test has access from outside domains (% in the Host field). Not shown on the image, but user test has full privilege on the database called test. The other users are bound to the local domain, even though the server is set to accept connections from the outside, when authenticating, users are thrown a denial.
You can connect to remote MySQL server. Just have to make sure that the remote server is binding to public address.
Here's how: http://www.howtogeek.com/howto/mysql/switch-mysql-to-listen-on-tcp/
You can connect to any MySQL server wherever it is, as long as it's setup for remote connection.
Depending on your operating system and webserver of choice, the settings will be different, but a good place to start is by (if using linux) looking here:
/etc/mysql/my.cnf
In this file you will find:
bind-address: 127.0.0.1
You will change this to the IP address of the server and then restart the mysql daemon.
..and now you can connect remotely ;)

unable to connect to remote mysql server from client website

website A: hosted on some free web host.
website B: my server
I want to connect from website B-(client) to mysql server of website A-(my server).
I've granted remote permission on mysql user with %.
But connection doesn't work. I've tried to run php script (mysql_connect) on different free web hosts.
000webhost.com : Can't connect to MySQL server on 'My SERVER IP' (4)
biz.nf : Lost connection to MySQL server during query
same script different mysql errors.
this php script run smoothly on my online SERVER, and also on localhost setup (connecting remotely to SERVER).
Is something else I've to configure on my SERVER?
I know that free web hosts doesn't allow remote mysql connection but in my situation I'm trying to connect with my own mysql server.
Does "remote mysql access" means both in and out connetion?
I've search a lot, something to do with ip-binding or port? I dont know where is actual problem?
Kindly help me out.
Tell mysql that it should listen on a fixed IP address, not 127.0.0.1 or localhost.
open file /etc/mysql/my.cnf (or /etc/my.cnf):
before
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1
after
# INSERT YOUR IP Address here
bind-address = 192.168.45.1
#bind-address=127.0.0.1
Then restart your mysql server. HTH
Does your remote website's firewall allow external connections to the MySQL Port 3306?
Find this out to make sure you aren't spinning your tires

Categories