LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Fedora
User Name
Password
Fedora This forum is for the discussion of the Fedora Project.

Notices


Reply
  Search this Thread
Old 03-12-2014, 10:53 PM   #1
cyberdome
Member
 
Registered: Mar 2014
Distribution: Fedora 23 - MariaDB 10.1 -
Posts: 130
Blog Entries: 2

Rep: Reputation: 8
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.
 
Old 03-12-2014, 11:08 PM   #2
kirukan
Senior Member
 
Registered: Jun 2008
Location: Eelam
Distribution: Redhat, Solaris, Suse
Posts: 1,278

Rep: Reputation: 148Reputation: 148
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);
 
Old 03-12-2014, 11:17 PM   #3
cyberdome
Member
 
Registered: Mar 2014
Distribution: Fedora 23 - MariaDB 10.1 -
Posts: 130

Original Poster
Blog Entries: 2

Rep: Reputation: 8
Quote:
Originally Posted by kirukan View Post
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???
 
Old 03-13-2014, 03:38 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,240

Rep: Reputation: 1408Reputation: 1408Reputation: 1408Reputation: 1408Reputation: 1408Reputation: 1408Reputation: 1408Reputation: 1408Reputation: 1408Reputation: 1408
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.
 
Old 03-13-2014, 10:48 AM   #5
cyberdome
Member
 
Registered: Mar 2014
Distribution: Fedora 23 - MariaDB 10.1 -
Posts: 130

Original Poster
Blog Entries: 2

Rep: Reputation: 8
Quote:
Originally Posted by TenTenths View Post
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?
 
Old 03-13-2014, 10:56 AM   #6
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,240

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

[Edit:]Also you may find this handy:
Code:
select * from `mysql`.`db`;

Last edited by TenTenths; 03-13-2014 at 11:00 AM.
 
Old 03-13-2014, 01:11 PM   #7
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 03-13-2014, 02:15 PM   #8
cyberdome
Member
 
Registered: Mar 2014
Distribution: Fedora 23 - MariaDB 10.1 -
Posts: 130

Original Poster
Blog Entries: 2

Rep: Reputation: 8
Quote:
Originally Posted by TenTenths View Post
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.

Last edited by cyberdome; 03-13-2014 at 02:26 PM.
 
Old 03-13-2014, 02:21 PM   #9
cyberdome
Member
 
Registered: Mar 2014
Distribution: Fedora 23 - MariaDB 10.1 -
Posts: 130

Original Poster
Blog Entries: 2

Rep: Reputation: 8
Quote:
Originally Posted by jlinkels View Post
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.
 
Old 03-13-2014, 06:30 PM   #10
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 03-13-2014, 08:32 PM   #11
cyberdome
Member
 
Registered: Mar 2014
Distribution: Fedora 23 - MariaDB 10.1 -
Posts: 130

Original Poster
Blog Entries: 2

Rep: Reputation: 8
Quote:
Originally Posted by jlinkels View Post
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.

Last edited by cyberdome; 03-13-2014 at 08:34 PM.
 
Old 03-13-2014, 08:41 PM   #12
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,732
Blog Entries: 20

Rep: Reputation: 3765Reputation: 3765Reputation: 3765Reputation: 3765Reputation: 3765Reputation: 3765Reputation: 3765Reputation: 3765Reputation: 3765Reputation: 3765Reputation: 3765
Quote:
Originally Posted by cyberdome View Post
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");

Last edited by astrogeek; 03-13-2014 at 08:47 PM.
 
Old 03-13-2014, 08:50 PM   #13
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 9,505

Rep: Reputation: 2114Reputation: 2114Reputation: 2114Reputation: 2114Reputation: 2114Reputation: 2114Reputation: 2114Reputation: 2114Reputation: 2114Reputation: 2114Reputation: 2114
Have you created a user 'peter' and granted privileges in mysql? Doesn't look like it.
 
Old 03-13-2014, 10:18 PM   #14
cyberdome
Member
 
Registered: Mar 2014
Distribution: Fedora 23 - MariaDB 10.1 -
Posts: 130

Original Poster
Blog Entries: 2

Rep: Reputation: 8
Quote:
Originally Posted by astrogeek View Post
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" ?

Last edited by cyberdome; 03-13-2014 at 10:19 PM.
 
Old 03-13-2014, 10:22 PM   #15
cyberdome
Member
 
Registered: Mar 2014
Distribution: Fedora 23 - MariaDB 10.1 -
Posts: 130

Original Poster
Blog Entries: 2

Rep: Reputation: 8
Quote:
Originally Posted by yancek View Post
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Unable to connect PHP to MySQL? armsman Linux - Software 1 09-14-2011 09:13 PM
Fresh CentOS5, PHP, mySQL Install, but Cant Pull up Localhost in Web browser. hungry4knowhow Linux - Newbie 9 04-20-2010 12:32 AM
Failed to launch VMware Web Access: unable to find a graphical web browser. rob0t Linux - Server 0 03-18-2009 02:32 PM
LXer: Backing up your MySQL database using your web browser and a PHP script LXer Syndicated Linux News 0 04-21-2007 10:16 PM
how to connect via web browser SlipAway172 Mandriva 6 01-13-2005 06:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Fedora

All times are GMT -5. The time now is 08:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration