PDA

View Full Version : PHP MySQL Connect to a Database Problem


Kadrus
November 1st, 2007, 09:55 AM
Euuh..I am having a problem connecting to a database using PHP

The script

<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}

mysql_close($con);
?>


I am getting this error..

Could not connect: Lost connection to MySQL server at 'reading initial communication packet', system error: 111

If anyone can help me or anything..really appreciated :)

dataw0lf
November 1st, 2007, 10:32 AM
Hmmm. This is just a shot in the dark, but replace this line:

$con = mysql_connect("localhost","username","password");

with this one:

$con = mysql_connect("127.0.0.1","username","password");
If that doesn't work, give me the output of the "sudo ifconfig -a" command.

Kadrus
November 1st, 2007, 10:39 AM
Hmmm. This is just a shot in the dark, but replace this line:

;

with this one:

$con = mysql_connect("127.0.0.1","username","password");
If that doesn't work, give me the output of the "sudo ifconfig -a" command.
When I wrote "
$con = mysql_connect("127.0.0.1","username","password");
it gave me

Could not connect: Access denied for user .....

Kadrus
November 1st, 2007, 11:12 AM
When I wrote "
$con = mysql_connect("127.0.0.1","username","password");
it gave me

Could not connect: Access denied for user .....

So any solution?

dataw0lf
November 1st, 2007, 11:44 AM
So any solution?
Wait. Are you actually using "username" and "password" in those fields? They have to be an actual username and it's actual password (in the MySQL system, not the Linux system). I think MySQL comes packaged with username "root" and password "", but it's been a while since I used it.

Kadrus
November 1st, 2007, 11:51 AM
Wait. Are you actually using "username" and "password" in those fields? They have to be an actual username and it's actual password (in the MySQL system, not the Linux system). I think MySQL comes packaged with username "root" and password "", but it's been a while since I used it.

No I am not writing "username and password"

dataw0lf
November 1st, 2007, 11:53 AM
So does the user and password you specified exist in MySQL? My magic 8 ball says "Not likely".

Kadrus
November 1st, 2007, 11:56 AM
So does the user and password you specified exist in MySQL? My magic 8 ball says "Not likely".
Yes

dataw0lf
November 1st, 2007, 11:59 AM
Do you get any errors when you do:
mysql -u root mysql
from bash?

Are you able to perform a "SELECT * FROM user;" if it does work? (and if it works, look for the username and password you specified).

Kadrus
November 1st, 2007, 12:05 PM
Do you get any errors when you do:
mysql -u root mysql
from bash?

Are you able to perform a "SELECT * FROM user;" if it does work? (and if it works, look for the username and password you specified).

I get Access denied!

dataw0lf
November 1st, 2007, 12:13 PM
Alright, perform these steps:

1) sudo bash

2) /etc/init.d/mysql stop

3) Run in bash: echo "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('SomeNewPassword');" >> ~/user.sql

Where SomeNewPassword is the new password you want for the MySQL root account

4) Run: mysqld_safe --init-file=~/user.sql &

5) Kill the mysqld_safe process (ps aux | grep mysqld_safe, get the process id, then kill -9 it)

6) Run: /etc/init.d/mysql start

7) try mysql -u root -p

8) Enter the password you supplied in step 3 when you are prompted for a password

It should connect then.

Kadrus
November 1st, 2007, 12:26 PM
Alright, perform these steps:

1) sudo bash

2) /etc/init.d/mysql stop

3) Run in bash: echo "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('SomeNewPassword');" >> ~/user.sql

Where SomeNewPassword is the new password you want for the MySQL root account

4) Run: mysqld_safe --init-file=~/user.sql &

5) Kill the mysqld_safe process (ps aux | grep mysqld_safe, get the process id, then kill -9 it)

6) Run: /etc/init.d/mysql start

7) try mysql -u root -p

8) Enter the password you supplied in step 3 when you are prompted for a password

It should connect then.
<snip>
When I type http://localhost in the url nothing appears anymore](*,):redface:

dataw0lf
November 1st, 2007, 05:35 PM
Calm down. Restart Apache.