Jump to content

mysql_connect problem


autoloader

Recommended Posts

Hello,

 

Just reinstalled / upgraded a Fedora Core server with FC 21, MySQL 5.7.7 and PHP 5.6.8. After the installation I restored a database and added/granted access to the neccessary users.

 

But when running a PHP script in CLI mode I'm not able to connect to the database using mysql_connect. It say "Permission denied".

 

but using 'mysql -h <host> -u <user> -p <database>' on the same machine gives me the access.

(<host>, <user>, <database> and the password is the same as

 

Any suggestions how to fix this?

mysql> grant all privileges on foobar.* to 'foo'@'localhost' identified by 'someSecretPassword';
mysql> flush privileges;

In the PHP script:
$con = mysql_connect("localhost", "foo", "someSecretPassword");
// here it fails with "Permission denied".

$ mysql -h localhost -u foo -p foobar
Password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 49
Server version: 5.7.7-rc-log MySQL Community Server (GPL)
...
Link to comment
Share on other sites

This is my code:

  $dbh = @mysql_connect($dbhost, $dbuser, $dbpass);
  if (false === $dbh) {
    $err = @mysql_error();
    if ($triggerError) {
      throw new Exception("Could not connect to db: $err\n");
    }
  }

But perhaps you are right. It might be PHP that say I do not have access to mysql_connect!

 

It this a 'new feature' in php 5.7 ??? I have never ever experienced such a problem.

Link to comment
Share on other sites

Stop using @ error suppression. You should get an error message now:

 $dbh = mysql_connect($dbhost, $dbuser, $dbpass);
if (false === $dbh) {
$err = mysql_error();
if ($triggerError) {
throw new Exception("Could not connect to db: $err\n");
}
}

[edit] For whatever reason, indentation isn't working. [/edit]

 

Also, try using $dbhost = "127.0.0.1", see if that makes a difference.

 

And there is no such thing as PHP 5.7.

Edited by requinix
Link to comment
Share on other sites

Sorry, php 5.6 as I wrote in my first posting (5.7 was the Mysql version).

 

127.0.0.1 did work - thanks. Why did I not think about that in the first time. Seems like PHP does not resolve localhost to 127.0.0.1 while in the shell localhost seem to be OK.

Link to comment
Share on other sites

Actually it's something else. Using "localhost" clues the MySQL driver to use a socket, which is practically a file on the machine. Using "127.0.0.1" forces a TCP connection, which should definitely work.

 

So your mysqld server daemon may not be creating the socket with the right permissions. Check your MySQL setting to see what it's doing. Also find the socket file itself - it probably doesn't have read (or execute?) permissions set for your user. Simply restarting MySQL may fix it too.

Link to comment
Share on other sites

The permissions on the mysql.sock file seem to be OK:

[root@anakin etc]# cd /tmp
[root@anakin tmp]# ls -l m*
srwxrwxrwx 1 mysql mysql 0 May 20 12:34 mysql.sock
-rw------- 1 mysql mysql 6 May 20 12:34 mysql.sock.lock

[root@anakin tmp]# cd /
[root@anakin /]# ls -ld tmp
drwxrwxrwt. 8 root root 4096 May 20 16:44 tmp

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.