Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't connect to MySQL server on 'localhost' (111) !! 1

Status
Not open for further replies.

hisham

IS-IT--Management
Nov 6, 2000
194
I used the following php script to test mysql connection via browzer:

<?php
$dbcon = mysql_connect(&quot;localhost:3306&quot;,&quot;myusername&quot;,&quot;mypassword&quot;);
if($dbcon){
echo &quot; connected... &quot;;
mysql_select_db(&quot;login&quot;,$dbcon);
$consult = mysql_query(&quot;Select * from users&quot;,$dbcon);
if($consult){
echo &quot;consult OK <BR>&quot;;
$res = mysql_num_rows($consulta);
echo &quot;No. of col. : &quot; . $res;
}else{
echo &quot;consult Error <BR>&quot;;
}
}else{
echo &quot;No conecction.<BR>&quot;;
}
?>
when i run this script I get the messege:
Warning: Can't connect to MySQL server on 'localhost' (111) in /usr/local/apache/htdocs/abc/msql.php on line 2

Warning: MySQL Connection Failed: Can't connect to MySQL server on 'localhost' (111) in /usr/local/apache/htdocs/abc/msql.php on line 2
No conecction.

Thanks in advance for any help.
 
Can you connect to mysql directly? does your webserver have enough permission to access mysql.

You shouldnt need the port in the connection. only if your database is on a different machine to that of your webserver.
----------------------------------------------------------
<?php
$dbcon = @mysql_connect(&quot;localhost&quot;,&quot;myusername&quot;,&quot;mypassword&quot;) or die ('Cannot Connect to Mysql server');

@mysql_select_db(&quot;login&quot;,$dbcon) or die ('Cannot select database');

$consult = mysql_query(&quot;Select * from users&quot;,$dbcon);
if(!empty($consult)){
echo &quot;consult OK <BR>&quot;;
$res = mysql_num_rows($consult);
echo &quot;No. of col. : $res&quot;;
}else{
echo &quot;consult Error or query returned nothing <BR>&quot;;
}
?>
***************************************
Party on, dudes!
[cannon]
 
Thank you KarveR for your answer,
Connecting by running /usr/local/mysql/bin/safe_mysqld & went fine, and I tried: ./mysqladmin –port=3306 version I get

Server version 3.23.49-log
Protocol version 10
Connection localhost via UNIX socket
UNIX socket /usr/local/mysql/tmp/mysql.socket
Uptime 31 min 49 sec

But when I tried to write a script that connected through Apache, the connection could not be made.
And I tried to connect using telnet from other machine, I get the error:
DHost ‘192.168.0.1’ is not allowed to connect to this MySQL server
Connection to host lost
And when I tried to connect using telnet from the same machine where the MySQL reside I get the error:
DHost ‘192.168.0.2’ is not allowed to connect to this MySQL server
Connection to host lost By foreign host.
Where I must do the configuration (permissions) to allow any IP to connect to MySQL server.
Thanks again.
 
in /etc theres a file called hosts.allow , in this you need an entry for your webserver.
It will look something like this:
----------------------------------------------------------
#
# hosts.allow This file describes the names of the hosts which are
# allowed to use the local INET services, as decided
# by the '/usr/sbin/tcpd' server.
#
add.the.ip.allowedhere
---------------------------------------------------------- ***************************************
Party on, dudes!
[cannon]
 
In the Doc. Of mysql said: A MySQL client on Unix can connect to the mysqld server in two different ways: Unix sockets, which connect through a file in the file system (default `/tmp/mysqld.sock') or TCP/IP, which connects through a port number. Unix sockets are faster than TCP/IP but can only be used when connecting to a server on the same computer. Unix sockets are used if you don't specify a hostname or if you specify the special hostname localhost.

I used ./mysqladmin version and I get:

Server version 3.23.49-log
Protocol version 10
Connection Localhost via Unix socket
UNIX socket /usr/local/mysql/tmp/mysql.sock
Uptime: 16 sec

While in the Doc. Of mysql said I must get too the following:

TCP port 3306

When I installed mysql I used:

./configure --prefix=/usr/local/mysql --localstatedir=/usr/local/mysql/data --with-unix-socket-path=/usr/local/mysql/tmp/mysql.socket

Is that mean I couldn’t connect to the mysql server from other computer? If yes, must I reinstall mysql?
and here is /etc/hosts.allow:

#
# hosts.allow This file describes the names of the hosts which are
# allowed to use the local INET services, as decided
# by the '/usr/sbin/tcpd' server.
#
ALL : localhost 127.0.0.1 : allow
ALL : 192.168.0.1 : allow
ALL : 192.168.0.2: allow

Trying to connect using telnet from other machine( Telnet> o 192.168.0.2 3306 ) still get the error:
DHost ‘192.168.0.1’ is not allowed to connect to this MySQL server
Connection to host lost. But I can telnet using other ports.

Thanks again .
 
check the permissions on this file /usr/local/mysql/tmp/mysql.sock and especially check that there are suitable permissions on the tmp directory containing mysql.sock. ***************************************
Party on, dudes!
[cannon]
 
Thanks KarveR,
Now I can connect in the same machine using the shell> mysql -p -u user mysql

but when i try to connect using your php script I get:
Cannot Connect to Mysql server

 
Dear Karver,
I too have the same problem of getting connected to the MySQL Server in a network.

Please suggest me some solutions, I have done all that you have adviced but no success.

Pleaes suggest, and thanks in advance.

Regards,
Phani.
 
Phani, hi, can you connect to mySQL if you are on the machine that runs mySQL?
type mysql at the command prompt, do you get errors, if so what are they?

If you don't get errors or the mysql machine is running OK, try adding port 3306 to your connection string.

e.g. 192.168.2.1:3306 ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top