In MySQL, “localhost” has a special meaning, it connects via a Unix socket. So it’s different from connecting to a TCP socket at 127.0.0.1.
Either mysqld was started without the option to create a Unix socket, or you have not allowed the account to connect to localhost, perhaps the password is wrong. I notice you have not specified a password, probably one was set in the server.
BTW it’s a good idea to create separate accounts for each use of the server rather than reuse the root account, even though that root is not related to the Linux root account, except for that fact that if you run mysql without -u to specify the login, it defaults to the current username.
I aggree with you that a separate account for each use would be a good idea. Nevertheless, when you install MySQL thanks to Yast, you get USER=root, without password and it works like that. Today, I still connect to mysqmadmin with those values.
What I don’t clearly understand is what sets up the link between mysqld and ‘localhost’. I explored my.cfg, but there is nothing there. In the Yast panel ‘Network Services’ -> ‘Hostname’, 127.0.0.1 has no alias, but I don’t feel it’s the right place where to do it.
So where could I set up a link between mysqld and ‘localhost’ or any other name?
I don’t understand what you mean by a link between mysqld and localhost.
When clients connect to localhost, they are sent to the path of the Unix socket specified in my.cnf. This probably happens because clients use a common client library. To actually connect to port 3306 at 127.0.0.1, you have to specify 127.0.0.1 in the connection parameters, then you get a TCP connection. In a sense, the MySQL client libraries hijack the name “localhost” to mean connect to the Unix socket, instead of resolving localhost to an IP address as you would normally expect.
That is the way MySQL is supplied; if you read 5.3.1 General Security Guidelines, you will see that the first thing you should do when using MySQL is to provide passwords for ‘root.’ Unless you are really planning to keep the anonymous users, you should delete them. If you keep the anonymous users, you need either to grant them only the privileges they need for whatever you are going to allow anyone to do to your tables.
MySQL is not intended to be run as ‘root’ in normal use; that is solely for administration.
Thank you for the explanation. So, connecting through the socket clearly doesn’t work, while connecting through TCP (127.0.0.1) does. Everything looks correct in my.cnf and the socket (/var/lib/mysql/mysql.sock) is there. Username (root) and password (void) are the same in every case.
So what?
Don’t know. Perhaps the PHP error is misleading and the error is actually something else underneath. Have you looked in /var/log/apache2/error_log to see if there are more clues?