LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 01-21-2013, 11:24 AM   #1
sbachta08
LQ Newbie
 
Registered: Mar 2012
Posts: 16

Rep: Reputation: Disabled
PHP scripts not connecting to mySQL DB on linux server 11.10


I recently installed a Linux LAMP server on an old desktop computer of mine following these instructions. I also installed phpmyadmin.

http://net.tutsplus.com/tutorials/ph...rver-for-free/


I uploaded a php page to the server to test the connection with the mySQL DS but I keep getting can't connect from the script. This is what I have for the php script:

$host = "localhost";
$username = "root";
$password = "my root password";

mysql_connect($host, $username, $password) or die("Can't connect");
echo "Connected to MySQL<br/>";
 
Old 01-21-2013, 12:52 PM   #2
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,350

Rep: Reputation: 127Reputation: 127
You need to get more info than just the "can't connect" from your own script, search the logs for apache, php & mysql.
One useful way is to open a few terminals and use "tail -f" on logs, like "tail -f /var/log/apache2/error.log" in one terminal, "tail -f /var/log/mysql.err" on second and so on (this on my Debian Lenny) while trying to connect.
You will then catch log messages in real-time.

A few other things to check:
Is mysql server running?
Can you log in to mysql in terminal with that password, can you access the database?

---
Edit:
I'm not very familiar with php, just wonder if you can have a blank before the value when assigning a variable?

Last edited by pingu; 01-21-2013 at 12:56 PM. Reason: Thinking...
 
Old 01-21-2013, 04:46 PM   #3
larvel
LQ Newbie
 
Registered: Jun 2012
Posts: 29

Rep: Reputation: Disabled
Do you have php-mysql innstalled?
 
Old 01-21-2013, 05:04 PM   #4
kox444
Member
 
Registered: Aug 2012
Posts: 128

Rep: Reputation: Disabled
Run something like this
$host = "localhost";
$username = "root";
$password = "my root password";
mysql_connect($host, $username, $password) or die(mysql_error());

And tell us what error you got.
 
Old 01-21-2013, 07:07 PM   #5
sbachta08
LQ Newbie
 
Registered: Mar 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Ok the error I am getting now says Access denied for user 'root'@'localhost' (using password: YES). I also get that error in the apache log files and when trying to login to mysql through the terminal.

Last edited by sbachta08; 01-21-2013 at 07:11 PM.
 
Old 01-21-2013, 07:27 PM   #6
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: Slackware, Ubuntu, PCLinux,
Posts: 10,539

Rep: Reputation: 2496Reputation: 2496Reputation: 2496Reputation: 2496Reputation: 2496Reputation: 2496Reputation: 2496Reputation: 2496Reputation: 2496Reputation: 2496Reputation: 2496
Did you create a root user password for mysql? Check the link below here at LQ. If you use the Search function on the top right of the page, you should be able to see numerous other posts here as this is a common problem.

http://wwww.linuxquestions.org/quest...rd-yes-900690/
 
1 members found this post helpful.
Old 01-22-2013, 07:32 PM   #7
sbachta08
LQ Newbie
 
Registered: Mar 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Hey yancek thanks for the input, I checked out that thread and ran some of the querys and there are passwords listed for mysql root users. But I am still getting the Access denied for user 'root'@'localhost' (using password: YES) error when I am trying to connect to mysql through a php page.
 
Old 01-22-2013, 08:55 PM   #8
cr-atlanta
LQ Newbie
 
Registered: Jan 2013
Location: Atlanta, GA, USA
Distribution: Gentoo, OSX
Posts: 18

Rep: Reputation: Disabled
Are you running the PHP on the same server as the MySQL database? I presume so since you used 'localhost'. Can you post the output of 'select user, host, password from mysql.user;'?

I use mysql_connect with a fourth parameter, the database to connect to - give that a shot too.
 
Old 01-22-2013, 08:56 PM   #9
cr-atlanta
LQ Newbie
 
Registered: Jan 2013
Location: Atlanta, GA, USA
Distribution: Gentoo, OSX
Posts: 18

Rep: Reputation: Disabled
Are you running the PHP on the same server as the MySQL database? I presume so since you used 'localhost'. Can you post the output of 'select user, host, password from mysql.user;'?

I use mysql_connect with a fourth parameter, the database to connect to - give that a shot too.

Last edited by cr-atlanta; 01-22-2013 at 08:59 PM.
 
Old 01-22-2013, 08:56 PM   #10
cr-atlanta
LQ Newbie
 
Registered: Jan 2013
Location: Atlanta, GA, USA
Distribution: Gentoo, OSX
Posts: 18

Rep: Reputation: Disabled
Are you running the PHP on the same server as the MySQL database? I presume so since you used 'localhost'. Can you post the output of 'select user, host, password from mysql.user;'?

I use mysql_connect with a fourth parameter, the database to connect to - give that a shot too. <--- Oops, nevermind this, I was thinking of mysqli

But it sounds like you're having connectivity issues locally - have you set the root password?

Last edited by cr-atlanta; 01-22-2013 at 09:01 PM.
 
Old 01-23-2013, 09:07 PM   #11
sbachta08
LQ Newbie
 
Registered: Mar 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
This is what I get from selecting from mysql.user:

+------------------+------------+-------------------------------------------+
| USER | HOST | PASSWORD |
+------------------+------------+-------------------------------------------+
| root | localhost | *Encrypted password |
| root | web-server | *Encrypted password |
| root | 127.0.0.1 | *Encrypted password |
| debian-sys-maint | localhost | *Encrypted password |
| phpmyadmin | localhost | *Encrypted password |
| root | % | *Encrypted password |
+------------------+------------+-------------------------------------------+

I able to access the database through the terminal and through phpmyadmin but not the PHP scripts.
 
Old 02-01-2013, 06:14 AM   #12
acelino
LQ Newbie
 
Registered: Jan 2013
Distribution: Ubuntu
Posts: 10

Rep: Reputation: Disabled
try resetting mysql root password:

Quote:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'some_pass' WITH GRANT OPTION;
where 'some_pass' is the password you want to use

If your accessing outside the box change the following

Quote:
'root'@'localhost' to root@%
 
  


Reply



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
connecting php to remote mysql server habibno05 Programming 16 07-31-2018 06:12 AM
Decoding Base64 encoded PHP scripts on Linux Server charu Linux - Newbie 2 01-18-2011 11:57 AM
connecting PHP to MySQL in a LAMP server rickyglucas_143 Linux - Newbie 2 02-06-2006 04:02 AM
Running PHP Scripts in SuSe Linux 9.3 w/o a server. elliotfuller Linux - Software 3 06-18-2005 06:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 09:02 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