LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Fedora (https://www.linuxquestions.org/questions/fedora-35/)
-   -   unable to connect o MySQL using PHP via web browser??? (https://www.linuxquestions.org/questions/fedora-35/unable-to-connect-o-mysql-using-php-via-web-browser-4175497994/)

cyberdome 03-12-2014 10:53 PM

unable to connect o MySQL using PHP via web browser???
 
Hello to all,

I am just trying to established and TEST a simple to my MySQL test database using PHP via my firefox browser.


I am able to connect using SSH and via terminal window on Fedora 20.

I have created small php connection file so that I can test the connection.

Code:

<?php
// Create connection
$con=mysqli_connect("example.com","peter","abc123","my_db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>


when I type in my 127.0.0.1/test.php file in my FIREFOX browser.

I get a error message " Failed to connect to MySQL: Permission denied "


Just trying to understand where I went wrong?

any help is greatly appreciated.

kirukan 03-12-2014 11:08 PM

Is this your sample code?
you must replace respective Host_IP address, username, password and databasename
Quote:

$con=mysqli_connect("example.com","peter","abc123","my_db");
Sample:-
Quote:

mysqli_connect(host,username,password,dbname);

cyberdome 03-12-2014 11:17 PM

Quote:

Originally Posted by kirukan (Post 5133648)
Is this your sample code?
you must replace respective Host_IP address, username, password and databasename


Sample:-


Yes, I did that. I put in the IP address of the server, username of the mysql user, user password, and the database name. it is still giving me error?

Does this have something to do with the mysql.socket file???

TenTenths 03-13-2014 03:38 AM

Is your MySQL server on the same box as your Apache server?

Check that the username you are using has permissions to access the database from the host that Apache is running on.

cyberdome 03-13-2014 10:48 AM

Quote:

Originally Posted by TenTenths (Post 5133714)
Is your MySQL server on the same box as your Apache server?

Check that the username you are using has permissions to access the database from the host that Apache is running on.


Yes, MySQL server is on the same box as my Apache server.


how do I check the permissions to access the database? what command I have to use?

TenTenths 03-13-2014 10:56 AM

http://dev.mysql.com/doc/refman/5.5/en/show-grants.html

[Edit:]Also you may find this handy:
Code:

select * from `mysql`.`db`;

jlinkels 03-13-2014 01:11 PM

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

cyberdome 03-13-2014 02:15 PM

Quote:

Originally Posted by TenTenths (Post 5133955)
http://dev.mysql.com/doc/refman/5.5/en/show-grants.html

[Edit:]Also you may find this handy:
Code:

select * from `mysql`.`db`;

I do not know where to put this commnad in?

I did enforce the SELinux policy on my server.



Code:

[root@localhost]#  setsebool -P httpd_can_network_connect 1


[root@localhost]#  setsebool -P httpd_can_network_connect_db 1



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



Quote:

<?php
// Create connection
$con=mysqli_connect("192.168.1.3","username","password","my_db");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();

}
?>
I am lost and I do not know how to proceed. Please, any help would be appreciated.

cyberdome 03-13-2014 02:21 PM

Quote:

Originally Posted by jlinkels (Post 5134003)
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.

jlinkels 03-13-2014 06:30 PM

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

cyberdome 03-13-2014 08:32 PM

Quote:

Originally Posted by jlinkels (Post 5134119)
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                                                                          |


hope this is not all confusing. :)

astrogeek 03-13-2014 08:41 PM

Quote:

Originally Posted by cyberdome (Post 5134171)
Code:

| 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");


yancek 03-13-2014 08:50 PM

Have you created a user 'peter' and granted privileges in mysql? Doesn't look like it.

cyberdome 03-13-2014 10:18 PM

Quote:

Originally Posted by astrogeek (Post 5134177)
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" ?

cyberdome 03-13-2014 10:22 PM

Quote:

Originally Posted by yancek (Post 5134182)
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";
?>

All I get is a white page?


All times are GMT -5. The time now is 11:13 AM.