LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Error connecting to MySQL from PHP (https://www.linuxquestions.org/questions/linux-software-2/error-connecting-to-mysql-from-php-313049/)

anindyanuri 04-13-2005 11:49 PM

Error connecting to MySQL from PHP
 
I am new to PHP and MySQL.

In case of entering into MySQL if I enter the following command
Code:

>$ mysql -h host_name -u user_name -p
password : xxxx

I can easily enter into mysql server and I can do what I need.

The problem is that if I enter the same from PHP with the following command :
Code:

$link=mysql_connect ('host_name', 'user_name', 'password') OR die('ERROR : ' .mysql_error())
I am getting an error :
Quote:

ERROR : Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (13)
Where is the problem? How do I rectify it? Please need your best suggestion.

Regards
anindyanuri

twsnnva 04-14-2005 05:02 PM

Does /var/lib/mysql/mysql.sock exist?

Boby 04-14-2005 05:21 PM

Hello!

Have you compiled your PHP wright?
Look where you have your mysql.sock
Code:

$ mysql -h host_name -u user_name -p
Enter password:

mysql> status
--------------
...
UNIX socket:            /tmp/mysql.sock
...
--------------

I think you need to compile PHP like this
Code:

./configure ... --with-mysql-sock=/tmp/mysql.sock --with-mysql=/path/to/mysql_directory/
If it doesn't work, create a php file with this code
PHP Code:

<?php

phpinfo
();

?>

Start it in the browser and look if MySQL is enabled.

Hope this helped!
Boby

Proud 04-14-2005 05:28 PM

Try 127.0.0.1 instead of localhost for the hostname. Then what happens?

twsnnva 04-14-2005 05:41 PM

If the socket location is the problem as Boby suggests, instead of recompiling php you could:

a. edit the "mysql.default_socket =" line in php.ini
or
b. create a symlink from /tmp/mysql.sock to /var/lib/mysql/mysql.sock

anindyanuri 04-14-2005 10:04 PM

Quote:

Originally posted by twsnnva
Does /var/lib/mysql/mysql.sock exist?
Yes, it does. Giving you the effect of ls-l /var/lib/mysql/
Code:

$ ls -l /var/lib/mysql/
total 36
drwx------  2 mysql mysql 4096 Apr  3 18:40 menagerie
drwx------  2 mysql mysql 4096 Apr  1 22:27 mydata
drwx------  2 mysql mysql 4096 Mar 26 23:12 mysql
srwxrwxrwx  1 mysql mysql    0 Apr 15 08:24 mysql.sock
drwx------  2 mysql mysql 4096 Mar 26 23:12 test

Regards
anindyanuri

anindyanuri 04-14-2005 10:33 PM

Quote:

Originally posted by Boby
Hello!

Have you compiled your PHP wright?
Look where you have your mysql.sock
Code:

$ mysql -h host_name -u user_name -p
Enter password:

mysql> status
--------------
...
UNIX socket:            /tmp/mysql.sock
...
--------------


No, I have not compiled PHP as I am using the packaged version of PHP which comes with Fedora Core 3. I just installed it.

The mysql.sock exists in /var/lib/mysql/

Checked this status, and the result is...
Code:

mysql> status
--------------
mysql  Ver 11.18 Distrib 3.23.58, for redhat-linux-gnu (i386)

Connection id:          3
Current database:
Current user:          anindya@localhost
Current pager:          stdout
Using outfile:          ''
Server version:        3.23.58
Protocol version:      10
Connection:            Localhost via UNIX socket
Client characterset:    latin1
Server characterset:    latin1
UNIX socket:            /var/lib/mysql/mysql.sock
Uptime:                17 min 8 sec

Threads: 1  Questions: 4  Slow queries: 0  Opens: 6  Flush tables: 1  Open tables: 0 Queries per second avg: 0.004
--------------

Quote:


I think you need to compile PHP like this
Code:

./configure ... --with-mysql-sock=/tmp/mysql.sock --with-mysql=/path/to/mysql_directory/

Friend, I am a beginner in this trade, even on Linux. You wrote './configure ...', what is these three dots? Should I provide the command as usual with these three dots? I have checked that mysql.sock exists in /var/lib/mysql/, so please take a little bit pain to send me the exact command which I have to use.
Quote:


If it doesn't work, create a php file with this code
PHP Code:

<?php

phpinfo
();

?>

Start it in the browser and look if MySQL is enabled.

Hope this helped!
Boby

Yes, I have created a page using phpinfo; and it tells that MySQL is enabled. The output of MySQL part is...
Code:

mysql
MySQL Support        enabled
Active Persistent Links        0
Active Links        0
Client API version        3.23.58
MYSQL_MODULE_TYPE        external
MYSQL_SOCKET        /var/lib/mysql/mysql.sock
MYSQL_INCLUDE        -I/usr/include/mysql
MYSQL_LIBS        -L/usr/lib/mysql -lmysqlclient

Directive        Local Value        Master Value
mysql.allow_persistent        On        On
mysql.connect_timeout        60        60
mysql.default_host        no value        no value
mysql.default_password        no value        no value
mysql.default_port        no value        no value
mysql.default_socket        no value        no value
mysql.default_user        no value        no value
mysql.max_links        Unlimited        Unlimited
mysql.max_persistent        Unlimited        Unlimited
mysql.trace_mode        Off        Off

Please help me
Regards
anindyanuri

anindyanuri 04-14-2005 10:55 PM

Quote:

Originally posted by Proud
Try 127.0.0.1 instead of localhost for the hostname. Then what happens?
If i change the 'localhost' to '127.0.0.1' in my original .php file, the error message changes to :
Code:

Access denied for user: 'anindya@localhost.localdomain' (Using password: YES)
What to do?

Regards
anindyanuri

anindyanuri 04-14-2005 11:04 PM

Quote:

Originally posted by twsnnva
If the socket location is the problem as Boby suggests, instead of recompiling php you could:

a. edit the "mysql.default_socket =" line in php.ini

The extract of the above in /etc/php.ini file is :
Code:

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
mysql.default_socket =

Is the above right for me?
Quote:

Originally posted by twsnnva
or
b. create a symlink from /tmp/mysql.sock to /var/lib/mysql/mysql.sock

I do not know how to create the symlink. Please guide me how do I do that.

Regards
anindyanuri

Proud 04-15-2005 05:50 AM

Ok, you shouldn't need that symlink as the socket is already in the right place, and works for your mysql client program, just not PHP.
The socket worked for the client app but not PHP, and the tcp/ip connection worked for PHP but was told by MySQL that the user was not permitted from that hostname.
So you seem to have a problem with PHP for the socket, and maybe MySQL for the 127.0.0.1 route.
Have all apps been installed from precompiled official .rpm files? Are there any updates for FC3 regards these apps?

anindyanuri 04-15-2005 09:14 AM

Quote:

Originally posted by Proud
Ok, you shouldn't need that symlink as the socket is already in the right place, and works for your mysql client program, just not PHP.
The socket worked for the client app but not PHP, and the tcp/ip connection worked for PHP but was told by MySQL that the user was not permitted from that hostname.
So you seem to have a problem with PHP for the socket, and maybe MySQL for the 127.0.0.1 route.
Have all apps been installed from precompiled official .rpm files? Are there any updates for FC3 regards these apps?

I have updated apache before a couple of months. At that time I did not find any problem and no error message.

anindyanuri 04-16-2005 08:40 AM

Some improvement is there.................

If I write :
Code:

$link = mysql_connect('127.0.0.1', 'user_name', 'password')
  or die('ERROR in connection : ' . mysql_error());

I am getting the error message, but if I access mysql as the default user I am being passed and there is no error. Thus, if I write the above line as follows :
Code:

$link = mysql_connect('127.0.0.1','','')
    or die('ERROR in connection : ' . mysql_error());

I am not getting any error, and I can get access to the databases that comes with the mysql.
Still I get error if I want to connect the databases which I have created with my own 'user_name' and 'password'.
What is the problem? Why I cannot connect with my 'user_name' and 'password'? It is to be mentioned that my 'user_name' and 'password' is already granted for all privileges.

Please help
Anindyanuri

anindyanuri 04-22-2005 11:15 AM

Case - I
I am giving the output of 'netstat -anl' command while I am connected to mysql as user 'anindya' and host 'localhost':
Porto RefCnt Flags Type State I-Node Path
Unix 2 [ACC] STREAM LISTENING 8016 /var/lib/mysql/mysql.sock
Unix 3 [ ] STREAM CONNECTED 14886 /var/lib/mysql/mysql.sock


Case - II
This is the output of the 'netstat -anl' command whicle I am connected to mysql as admin
Porto RefCnt Flags Type State I-Node Path
Unix 2 [ACC] STREAM LISTENING 8016 /var/lib/mysql/mysql.sock
Unix 3 [ ] STREAM CONNECTED 26733 /var/lib/mysql/mysql.sock


In second case I can able to connect to mysql from PHP but in Case-I, myself fails to connect to mysql from php.

What to do?

Regards

masand 04-22-2005 09:44 PM

hey

looks like selinux is enabled for http and mysql

follow this thread

http://drupal.org/node/14874

http://forums.mysql.com/read.php?11,...1482#msg-21482

regards

anindyanuri 04-22-2005 11:08 PM

Read those articles. The problem is exactly same which I am facing. Thank you Masand for your pin-point accuracy. I shall now try for the solutions, hope it will work. I'll inform what happens for me.

Regards
anindyanuri


All times are GMT -5. The time now is 02:28 PM.