[SOLVED] unable to connect o MySQL using PHP via web browser???
FedoraThis forum is for the discussion of the Fedora Project.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
MySQL grants right to users at localhost. For localhost it uses the socket connection. It is most likely that that permission is granted.
But I see you use the domain name to connect. Most likely the domain name is resolved to the IP of your box, say 123.123.123.123. Now this IP is not granted access to the database. Because it is an IP and not localhost.
So try to grant permissions to this user to connect from localhost ('*'@localhost) and to connect from all hosts ('*'@'*').
You can test this behaviour on the Bash command line:
Now I am getting a different error after executing these commands above?
Code:
Failed to connect to MySQL: Access denied for user 'user'@'localhost' (using password: YES)
user is the name of the account I use to log into my fedora desktop. When I change the username for ROOT , I just get a blank white page.
when I change the IP address from 127.0.0.1 to 192.168.1.3 on my php script, I get a different error message. I am confused as to what I am doing wrong?
Code:
error message :
Failed to connect to MySQL: Host 'fedoraserver.home' is not allowed to connect to this MariaDB server
MySQL grants right to users at localhost. For localhost it uses the socket connection. It is most likely that that permission is granted.
But I see you use the domain name to connect. Most likely the domain name is resolved to the IP of your box, say 123.123.123.123. Now this IP is not granted access to the database. Because it is an IP and not localhost.
So try to grant permissions to this user to connect from localhost ('*'@localhost) and to connect from all hosts ('*'@'*').
You can test this behaviour on the Bash command line:
Code:
mysql -h example.com -u peter -pabc123 mydb
I assume it fails now, whereas
Code:
mysql -u peter -pabc123 mydb
succeeds.
jlinkels
Sorry, for the confusion, my apologies. I am actually using 127.0.0.1 or 192.168.1.3 ( LAN IP ). I just posted that as a example.
mine looks something like this.
Code:
<?php
// Create connection
$con=mysqli_connect("127.0.0.1","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
but even still it does not work on my browser. I am opening my browser on my own server not on any other computer.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
Open a terminal window and issue both these commands:
Code:
mysql -h 127.0.0.1 -u peter -pabc123 mydb
and
Code:
mysql -u peter -pabc123 mydb
Post the result here.
I assume at least of those commands allows you access to the database.
If so, while you are connected to the database, issue the command:
Code:
show grants;
(note the ';')
And post the output here.
CTRL-C exits you from the database.
Open a terminal window and issue both these commands:
Code:
mysql -h 127.0.0.1 -u peter -pabc123 mydb
and
Code:
mysql -u peter -pabc123 mydb
Post the result here.
I assume at least of those commands allows you access to the database.
If so, while you are connected to the database, issue the command:
Code:
show grants;
(note the ';')
And post the output here.
CTRL-C exits you from the database.
jlinkels
Code:
mysql -h 127.0.0.1 -u peter -pabc123 mydb
This command did not work.
Code:
mysql -u peter -pabc123 mydb
This command did not work either
I was able to get into the MySQL database using :
Code:
mysql -u 127.0.0.1 root -pabc123 mydb
and
mysql -u root -pabc123 mydb
both commands with ROOT worked. basically, my username (one that I use to login initially to login into desktop) does not have access to MySQL database.
All I am trying to do test my php connection to my database via WEB browser. that is why I used that simple $mysql connect script. so that when I create a table or database, I could view using a browser. this is my end goal.
output from the SHOW GRANTS; command.
Code:
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*0C96AF962992C0897E3FA11FC5BB750ADBC39DA0' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*0C96AF962992C0897E3FA11FC5BB750ADBC39DA0' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
hope this is not all confusing.
Your user "peter" does not have any permission to access the database.
Try this... (as your root user)
Code:
GRANT ALL ON mydb.* TO peter@localhost identified by "abc123";
Then connect with...
Code:
mysql -u peter -pabc123 mydb
OR from PHP
$con=mysqli_connect("localhost","peter","abc123","mydb");
Your user "peter" does not have any permission to access the database.
Try this... (as your root user)
Code:
GRANT ALL ON mydb.* TO peter@localhost identified by "abc123";
Then connect with...
Code:
mysql -u peter -pabc123 mydb
OR from PHP
$con=mysqli_connect("localhost","peter","abc123","mydb");
so before I could not connect using my user account to connect to my database. Thanks to your help. I was able to run the command:
Code:
mysql -u user -pabc123 mydb
It worked. My end goal is to use PHP to query results from database / tables to show results on my WEB browser. So I run a small PHP script to at least connect to my MySQL database.
example code:
Code:
<?php
$hostname = "192.168.1.15";
$username = "peter";
$password = "abc123";
// Create connection to the database
$dbhandle = mysql_connect($hostname, $username, $password);
or die ("unable to connect to MySQL");
echo "connected to MySQL";
?>
When I run it on my WEB browser: 127.0.0.1/file.php
All I get is a BLANK White Page?
At least you helped me get the USER to connect to my DATABASE. thanks,
Should I not get the message "connected to MySQL" ?
Have you created a user 'peter' and granted privileges in mysql? Doesn't look like it.
as per previous poster, I granted privileges to my USER and now I am able to connect via terminal.
But my browser still shows BLANK white page when I try to run a sample PHP mysql connect script.
Code:
<?php
$hostname = "127.0.0.1";
$username = "user";
$password = "abc123";
// Create connection to the database
$dbhandle = mysql_connect($hostname, $username, $password);
or die ("unable to connect to MySQL");
echo "connected to MySQL";
?>
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.