Remote mysql connection issue in PHP - php

I am creating a remote connection using this PHP script from my website to another website of mine. But it is showing errors which says hostname is not allowed to connect to this MySQL serve
Please let me know what I am missing and also tell me if there any other way to access remote MySQL data using a PHP script.
$connect=mysql_connect("hostname","user","password")or die(mysql_error().'Our database is currently down for updates, please check back later.');

You need to explicitly allow the mysql user to connect from the host. For example CREATE USER 'jeffrey'#'localhost' IDENTIFIED BY 'mypass'; will allow the user jeffrey to connect only from localhost.
You can do that via sql commands or phpmyadmin, check the users tab.

You need to grant the mysql user access to your database from the host where the script is running. So if your mysql server is on host server, and the script runs on machine dev, your grant statement would look something like this:
GRANT ALL PRIVILEGES ON database.* TO 'user'#'dev' IDENTIFIED BY 'password';

Does your MySQL database allow remote access to connect? If not, change the configs.
UPD
Tutorial for changing mysql remote access configs

Related

Prevent database from external access

I have created a home Apache server with MySQL and PHP. I have just created a website that I want to be publicly accessible, majorly for my experimenting. However I just realized that my MySQL server that is running on port 3306, usually my PHP connects to the database on localhost with a username and no password. Can't any other remote PHP script connect to my database and siphon off all the stored data? Does it not put my website in jeopardy? How do I stop remote connections to my database server? I only want the applications on my server(or those that I approve of), to access my database. Forgive me if the wording is bad, I couldn't find any helpful articles on Google.
You can restrict your MySQL server to listen only for localhost connections with bind-address = 127.0.0.1 configuration option as it was suggested in the comments.
If you want to make changes only for this particular user you can check the grants with the following query:
SHOW GRANTS FOR CURRENT_USER;
If it is not restricted only to localhost you can restrict it:
GRANT ALL PRIVILEGES ON *.* TO <username> #'localhost' IDENTIFIED BY '<password>';
GRANT ALL PRIVILEGES ON *.* TO <username> #'127.0.0.1' IDENTIFIED BY '<password>';
In general, I would recommend adding a password because you can make some other configuration changes in future or run other apps using the same MySQL server. It is better to be safe than sorry.

cant connect opencart to mySQL database

I'm trying to set up opencart for my website, but when I fill in everything for the MySQL database I get this error:
No connection could be made because the target machine actively refused it.
I've tried putting it on localhost, I've tried on 127.0.0.1. when I try on 192.168.1.126 (the database server) it says it can't find the database
the server hosting the PHP site is on 192.168.1.125
I'm very new to SQL, I really don't understand it fully. I'm creating a new database in SQL workbench. and filling in the info I need. How to I fix this error?
My guess is that your DB user is only allowed to access the DB server from 127.0.0.1. You might want to grant additional privileges to it and in your SQL shell run something like
GRANT ALL PRIVILEGES ON *.* To 'your-db-user'#'192.168.1.125'

How to setup Windows Server 2008 for MySQL remote database connectivity?

I am very new to this server setup and database connectivity. I googled a lot but couldn't find the solution for it.I am developing an android application which needs to post data to remote database. Which is done.
Now i have to setup MySQL Database in the new Windows Server 2008. I have installed and managed to get access the root user(Which is fine). Now i want to access this database from my local computer[Which is iMac]. I already set the privileges by following this link. When i tried to run from my web application it shows the following error.
Error: unable to connect to database. Host 'xx.xx.xxx.x' is not allowed to connect to this MySQL server
Here is my questions
1.) How to get access to the remote MySQL database from any computer?
2.) How to run the web application in the server?Like Web Hosting i.e Do i need to setup ftp account and put the stuff over there?(For Eg: testhost.com/connecttodatabase.php by executing this it will execute whatever code in testhost server. But i have no idea how to do that for my server).
Any help is much appreciated.
Alternately, You can also try...
use the_database_name;
GRANT ALL PRIVILEGES ON
the_database_name.*
TO
'the_user_in_php_code'#'%'
IDENTIFIED BY
'password_of_the_user_in_php_code';
FLUSH PRIVILEGES;
source:
http://forums.devshed.com/mysql-help-4/host-is-not-allowed-to-connect-to-this-mysql-server-366908.html

How to connect to remote mysql database with php?

I have a website and a database on a host. But I want the administrator panel on my laptop? Is it possible connect remotely? How can I do this? Thank you very much!
If I've understood correctly your question, you're looking for a GUI front-end for a remote MySQL database. There's many MySQL front-ends out there, but two I would recommend are the official MySQL Workbench if you're running Windows or Linux, and Sequel Pro if you're running Mac (MySQL Workbench can also run on Mac, but I personally prefer Sequel Pro). Both are free.
The first thing you should try is simply connecting to the remote MySQL server by the command line.
$ mysql -u your_user -h remote.host.name -p
Depending on the output will determine what you need to do next.
Error 1
ERROR 2003 (HY000): Can't connect to MySQL server on 'remote.host.name' (113)
This means that the port is not even open for an external machine to connect to it, so you will need to add whatever port MySQL is running on to your firewall to accept incoming connections.
Error 2
ERROR 1045 (28000): Access denied for user 'your_user'#'your.host.name' (using password: YES)
Assuming that your login credentials are correct, this means that you need to grant permissions from within MySQL. Connecting locally from the remote server, grant permissions like this:
GRANT ALL ON your_database.* TO your_user#'your.host.name' IDENTIFIED BY 'your_password';
Obviously substitute all of the relevant things to what they should be.
When you can connect by command line, connecting by PHP is as simple as using the hostname, username and password information that you used in the mysql command above.
You can use a piece of software called Chive to do this, however it's possible that your server doesn't accept connections remotely.
You can either install Chive locally on your machine and connect to the remote computer, or install Chive remotely and connect to it via HTTP (though I strongly recommend HTTPS).
http://www.chive-project.com/
Wow, just like you connect to the site, you should only change the authentication data if necessary, and external IP mysql.

Connecting to a remote database from a localhost computer using mysql and PHP

So here is the situation.
I have a database on a remote server that gives out quizzes and scores for individual students...
I also have a local database which contains the students names and their respective groups.
What I want to do is to display the list of students per group then show the results of their scores from the remote database.. Is this possible??
Im currently running the script from my local computer using XAMMP like so
mysql_connect("REMOTE SERVER ADDRESS","USERNAME", "PASSWORD") or die("Could not connect to MySQL server!");
But all I get is Could not connect to MySql Server..
GRANT ALL ON database.* TO user#ipaddress IDENTIFIED BY 'password';
You should force a reload of the grant tables using:
FLUSH PRIVILEGES;
https://documentation.cpanel.net/display/ALD/Remote+MySQL# I found useful this one myselt. When you haven't define access permissions to remote db using it's cpanel, then you cannot connect with it via the localhost from your PC.
You will find something like 'Add Access Host' in your hosting server cpanel and there you can add which IPs are you going to use to access from outside. There I used as "%.%.%.%" and that means I allowed every v4 IPs to access my database. Instead of "%.%.%.%" you can put your PC's IP(for example 51.254.230.178). As PC's IP gets changed frequently, then I used this because I cannot change it manually all the time by checking my IP every time I connected to the Internet.

Categories