大网数据广告活动
服务器运维问答

'/var/lib/mysql/mysql.sock' (13 "Permission denied") when accessing the socket as non mysql user

· 52CE 我爱测

I am trying to connect to local mysql server I use for development The server starts fine, but I can not connect to it as a non-root user. [root@somepc ]# mysql -u [someuser] -p[somepass] ERROR 2002 (HY000): Can't connect to loca…

问题原文

原帖链接 · nelaaro · CC BY-SA 3.0

I am trying to connect to local mysql server I use for development

The server starts fine, but I can not connect to it as a non-root user.

[root@somepc ]# mysql -u [someuser] -p[somepass]
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (13 "Permission denied")

As root, this works as expected. So that user does have permissions to access mysql.

mysql -u [user] -p[password]
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.30-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

When I check the permission of as my user

stat /var/lib/mysql/mysql.sock
stat: cannot stat '/var/lib/mysql/mysql.sock': Permission denied

What should these permission be for a non-root user?

已采纳解答

原帖链接 · nelaaro · CC BY-SA 3.0

The problem was related to the error on permissions on the socket file

namei -l /var/lib/mysql/mysql.sock                                                                                                                                               Wed 07 Feb 2018 12:53:54 SAST
f: /var/lib/mysql/mysql.sock
drwxr-xr-x root  root  /
drwxr-xr-x root  root  var
drwxr-xr-x root  root  lib
drwx------ mysql mysql mysql
                       mysql.sock - No such file or directory

As root I can see that the permissions on the socket file are

 ll /var/lib/mysql/mysql.sock
 srwxrwxrwx 1 mysql mysql 0 07.02.2018 12:42 /var/lib/mysql/mysql.sock=

So the problem is the directory for mysql as can be seen above.

drwx------ mysql mysql mysql

I fixed this by giving everybody access. As this is my local machine used for development.

sudo chmod go+rx /var/lib/mysql/

On a production machine, I would consider the correct users to the mysql group.

sudo chmod g+rx /var/lib/mysql/

grep mysql /etc/group
mysql:x:89:[someusers]

Please use common sense and replace whatever you need between the [] above

This now works as expected

mysql -u [user] -p[password]
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.30-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-02-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。