What is the correct location of mysql.sock file on linux
The socket declaration should be located under [mysqld]
in your my.cnf
. If you have declared it there and still pointing to another place such as tmp
, then your my.cnf
file that you have been editing is not being read when mysql starts or there is another my.cnf
overriding the one you have been editing. The case may also be that there is a second Socket
declaration in the same my.cnf
file that is overriding the one you expect to be read at start by mysql.
You can check its absolute path by logging into mysql and running:
mysql> show variables like 'socket';
+-----------------------------------------+-------------------------------+
| Variable_name | Value |
+-----------------------------------------+-------------------------------+
| socket | /yourpath/mysql.sock |
+-----------------------------------------+-------------------------------+
1 rows in set (0.00 sec)
There is no right or wrong place on linux for saving sockets, except on external devices which are unmounted at any time or folders which are emptied from time to time. You need to configure where you want to place it.
For the MySQL console client there is something more to consider: The client seems to use the socket configuration value provided in [client] instead of [mysqld]. If you have a multiple MySQL server setup like i have (4.1, 5.5, 5.7), you probably want to use a "--defaults-extra-file" option with the socket written in the [client] part for each different server. The socket needs to be the same as the one defined in the my.cnf [mysqld] part used on the server. Here is an example defaults-extra-file.cnf, remember to change the user, password and socket to your needs - and keep it on access rights 400 (there is a password stored after all):
[client]
user = guardian
password = I-4m.Gr00t!
host = localhost
socket = /var/run/mysqld/mysqld-5.6.sock
This only applies though if you are using "localhost" as the value for "host". If you have multiple local addresses (127.0.0.2, 127.0.0.3), you need to leave the config variable for "host" on "localhost" to use the configured socket. Otherwise the client will connect to the server using TCP.
This was tested on my Debian 9.5 Server with MySQL 4.1.22, 5.5.49 and 5.7.19 and their respective clients.