Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct location of mysql.sock file on linux

Tags:

linux

mysql

I have a mysql.sock file located at:

/tmp/mysql.sock

But the my.cnf is pointing to this location:

socket=/var/lib/mysql/mysql.sock

Is this correct?

like image 213
Victor Avatar asked Oct 27 '15 20:10

Victor


People also ask

Where is mysql sock in Linux?

The MySQL server's socket file is named mysqld. sock and on Ubuntu systems it's usually stored in the /var/run/mysqld/ directory. This file is created by the MySQL service automatically.

What is sock file in Linux?

A SOCK file is a Unix domain socket file. It is used to communicate with another program, process, or server. Unix users typically create SOCK files using the socket command. Sockets are software components that allow programs to communicate with each other and servers.

How do I open mysql socks?

Check the Unix socket connection from the command line In the command line, run the following command: mysql -u root -p -S /var/run/mysqld/mysql. sock . Type a password for your root user and press Enter .


2 Answers

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)
like image 140
BK435 Avatar answered Oct 11 '22 18:10

BK435


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.

like image 40
Mihovil Bubnjar Avatar answered Oct 11 '22 20:10

Mihovil Bubnjar