LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-13-2005, 11:49 PM   #1
anindyanuri
Member
 
Registered: Jan 2005
Location: India
Distribution: Kubuntu Gutsy
Posts: 450

Rep: Reputation: 30
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
 
Old 04-14-2005, 05:02 PM   #2
twsnnva
Member
 
Registered: Oct 2003
Location: Newport News, Va
Distribution: Debian
Posts: 246

Rep: Reputation: 30
Does /var/lib/mysql/mysql.sock exist?
 
Old 04-14-2005, 05:21 PM   #3
Boby
Member
 
Registered: Feb 2004
Posts: 781

Rep: Reputation: Disabled
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
 
Old 04-14-2005, 05:28 PM   #4
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Try 127.0.0.1 instead of localhost for the hostname. Then what happens?
 
Old 04-14-2005, 05:41 PM   #5
twsnnva
Member
 
Registered: Oct 2003
Location: Newport News, Va
Distribution: Debian
Posts: 246

Rep: Reputation: 30
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

Last edited by twsnnva; 04-14-2005 at 05:42 PM.
 
Old 04-14-2005, 10:04 PM   #6
anindyanuri
Member
 
Registered: Jan 2005
Location: India
Distribution: Kubuntu Gutsy
Posts: 450

Original Poster
Rep: Reputation: 30
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
 
Old 04-14-2005, 10:33 PM   #7
anindyanuri
Member
 
Registered: Jan 2005
Location: India
Distribution: Kubuntu Gutsy
Posts: 450

Original Poster
Rep: Reputation: 30
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
 
Old 04-14-2005, 10:55 PM   #8
anindyanuri
Member
 
Registered: Jan 2005
Location: India
Distribution: Kubuntu Gutsy
Posts: 450

Original Poster
Rep: Reputation: 30
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
 
Old 04-14-2005, 11:04 PM   #9
anindyanuri
Member
 
Registered: Jan 2005
Location: India
Distribution: Kubuntu Gutsy
Posts: 450

Original Poster
Rep: Reputation: 30
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
 
Old 04-15-2005, 05:50 AM   #10
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
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?
 
Old 04-15-2005, 09:14 AM   #11
anindyanuri
Member
 
Registered: Jan 2005
Location: India
Distribution: Kubuntu Gutsy
Posts: 450

Original Poster
Rep: Reputation: 30
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.
 
Old 04-16-2005, 08:40 AM   #12
anindyanuri
Member
 
Registered: Jan 2005
Location: India
Distribution: Kubuntu Gutsy
Posts: 450

Original Poster
Rep: Reputation: 30
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

Last edited by anindyanuri; 04-16-2005 at 08:46 AM.
 
Old 04-22-2005, 11:15 AM   #13
anindyanuri
Member
 
Registered: Jan 2005
Location: India
Distribution: Kubuntu Gutsy
Posts: 450

Original Poster
Rep: Reputation: 30
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

Last edited by anindyanuri; 04-22-2005 at 11:16 AM.
 
Old 04-22-2005, 09:44 PM   #14
masand
LQ Guru
 
Registered: May 2003
Location: INDIA
Distribution: Ubuntu, Solaris,CentOS
Posts: 5,522

Rep: Reputation: 69
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
 
Old 04-22-2005, 11:08 PM   #15
anindyanuri
Member
 
Registered: Jan 2005
Location: India
Distribution: Kubuntu Gutsy
Posts: 450

Original Poster
Rep: Reputation: 30
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
 
  


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
probs with connecting to mysql database using php ankscorek Linux - Software 4 08-16-2005 07:40 PM
Connecting to Mysql using PHP mahugl Linux - Software 1 01-31-2004 11:20 AM
mysql and connecting from php on apache with moregroupware seriv Linux - Software 1 07-25-2003 08:11 AM
I need help connecting to mysql with php Pete Dogg Programming 4 06-08-2003 12:52 AM
connecting mysql by php norbs Linux - Software 1 07-25-2002 01:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:52 AM.

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