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
https://forums.phpfreaks.com/topic/296403-mysql_connect-problem/
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.

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.

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.