Connect to SQL server from localhost - php

I have a newly set up LAMP system for testing a website I am developing. School has supplied a database that uses a phpmyadmin webinterface. If I host the website on the domain I have been assigned by school, everything works fine, but if I host the same files locally it get the following error:
SQLSTATE[HY000] [2002] Connection timed out
Having read up as much as I could, I suppose some setting restricts my access to an external database when hosting locally, but I am lost as to how to fix this.
I know of the my.cnf file but the skip-networking, and bind-address options were already commented out. Also, I checked that both httpd and mysqld are running.

Odds are very good that the school's database doesn't allow connections from remote IP addresses. Has your instructor specifically told you that you should be able to connect?
To fix it, you'd have to have the system administrator open it up for you.
When you say you edited my.cnf, on what machine did you make those changes? Your local LAMP system or on the school's database server (or convince them to give you VPN access).

Related

MySQL connection to external database using XAMPP

I've stored a web project (PHP, HTML and CSS). I'm working inside opt/lampp/htdocs XAMPP directory, so I can run the .php files using the web browser. One of those .php files tries to connect to an external server MySQL database, but, when run:
mysqli_connect($servername, $username, $userpassword);
it displays the following warning.
*mysqli_connect(): (HY000/2002): Connection refused*
I've tried to access the database with phpmyadmin and the corresponding credentials and it works fine.
So the question is: can I perform a msqli_connect to that external database using XAMPP or I should give up?
Thanks in advance!
Since I cannot comment because of my low points, I am answering here.
I had faced similar issues before, so sharing my experience. All of the comments have suggested valuable points. These are as follows:
Use IP address instead of server name (There may be some DNS cache issue, so it is safe to use IP address).
Make sure the IP address you are using is not being blocked by the server's firewall. If possible configure server's firewall to white-list your IP Address.
The default MySQL port is 3306. Make sure the firewall is configured for allowing outside connection to the port 3306.
Most importantly, make sure the MySQL server must be configured to accept connections externally. If all of the above settings are configured properly then you should grant external access and all privilege to your MySQL User.
GRANT ALL PRIVILEGES ON 'yourDatabase'.* TO 'yourUserName'#'%';
FLUSH PRIVILEGES;
Reference - Another SO Question.

Access denied for user 'username'#'localhost'

I am currently programming a launcher in Visual Studio that gets the version history from a MySQL server. I have run into an issue where I cannot connect to the database if I use a public login for the database. If I use the root login I can connect on my local machine but no other. This is what I am using with subins to protect my data:
"server='serverip';user='severusername';password='serverpassword';database'databasename'"
If I use the root password, it will connect on my local computer. If I use my godaddy hosting database, it will also work on any computer. The MySQL server I am connecting to is on my local computer and running WAMP. Does anyone know a way to get it to work? My user has full permissions. This is programmed in Visual Studio using Visual Basic.
It appears that you have a typo:
database'databasename'
Should that not be:
database='databasename'
Also, it could be that godaddy does not allow remote connections for MySQL so when you try to connect from your local WAMP installation to their remote MySQL server the connection is denied. This is more than likely the case as the bandwidth cost for this can become quite huge very quickly. You should however be able to run things in the shared hosting environment using localhost as the server/hostname.
I fixed it by adding 2 cases of my user. One with a host of localhost and another with a host of 127.0.0.1

Cannot connect to mysql database on remote host

I am not sure if the terms I am using are correct so ask for clarification if you need :).
Anyways, I am hosting through HostEasySolutions (Server A). It comes with a MySql database, using PHPMyAdmin as the frontend. On Server A, I added some PHP files to access the database, and it works fine.
If I copy the php files to my other server, through DreamHost (Server B), I cannot connect to the database. I get the error: Can't connect to MySQL server on 'combinedsystems.ca' (110)
In the cPanel on Server A, I added the IP for Server B into the Remote Database Access Hosts, I also just added the wildcard '%', just to see if I could get it working...but still it can't find the server.
I am not sure what is going on, as far as I can see there is no firewall.
The only thing that stands out to me, is that if I go to PHPmyAdmin for Server A, it says Server: Localhost via UNIX socket, where as for Server B's PHPMyAdmin, it says: Server: mysql.pdem.info via TCP/IP
Most (all?) hosting companies have port 3306 closed on the firewall.
What you need to do:
adding remote database access in the database
edit my.cnf to listen on all interfaces. Default mysql listens only to localhost
most of the time you are out of luck! Even if there is no firewall (would be very unsecure) you have no access to my.cnf

How to make Php application running on Xampp to connect to a remote mysql db?

I have a php application running on XAMPP but I want to connect to a mysql db running on a remote machine(which is also using XAMPP). The application works fine when I connect to the mysqldb instance on my machine. I have changed the configuration files to point to the remote db and have given the correct credentials as well. But I get access denied error.
"Access denied" means you're at least connecting to the remote MySQL instance. Most like you've got a mismatch on the credentials. You may have granted permissions to "user#somehost" on the remote MySQL, but if that machine can't resolve "somehost" via DNS or a Hosts file lookup, it'll have to fall back to checking for a "user#ip.add.re.ss" account.
First I would check the remote database owner / host to make sure they allow external access. Most hosts, especially shared hosting providers, only allow access to scripts run locally and do not allow external access like you are describing.

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