详解MySQL中的pid与socket

吾爱主题 阅读:158 2024-04-02 08:03:13 评论:0
  • socket文件:当用Unix域套接字方式进行连接时需要的文件。
  • pid文件:MySQL实例的进程ID文件。

1.pid-file介绍

MySQL 中的 pid 文件记录的是当前 mysqld 进程的 pid ,pid 亦即 Process ID 。可以通过 pid-file 参数来配置 pid 文件路径及文件名,如果未指定此变量,则 pid 文件默认名为 host_name.pid ,存放的路径默认放在 MySQL 的数据目录。

建议指定 pid 文件名及路径,pid 目录权限要对 mysql 系统用户放开,具体配置可参考如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 # my.cnf 配置文件 [mysqld] pid- file   /data/mysql/tmp/mysqld .pid   # 查看mysqld进程 [root@localhost ~] # ps -ef|grep mysqld root       8670      1  0 Jun09 ?        00:00:00  /bin/sh  /usr/local/mysql/bin/mysqld_safe  --datadir= /data/mysql/data  --pid- file = /data/mysql/tmp/mysqld .pid mysql      9353   8670  0 Jun09 ?        00:01:23  /usr/local/mysql/bin/mysqld  --basedir= /usr/local/mysql  --datadir= /data/mysql/data  --plugin- dir = /usr/local/mysql/lib/plugin  --user=mysql --log-error= /data/mysql/logs/error .log --pid- file = /data/mysql/tmp/mysqld .pid --socket= /data/mysql/tmp/mysql .sock   # 查看pid文件内容  [root@localhost ~] # cat /data/mysql/tmp/mysqld.pid 9353

可以看到 pid 文件内容只有一行,记录了 mysqld 进程的 ID 。mysqld 进程启动后会通过 create_pid_file 函数新建 pid 文件,通过 getpid() 获取当前进程号并将进程 ID 写入 pid 文件。进程运行后会给 pid 文件加一个文件锁,只有获得 pid 文件写入权限的进程才能正常启动并把自身的 PID 写入该文件中,其它同一个程序的多余进程则自动退出。因此 pid 文件的作用是防止启动多个进程副本。

有时候可能会遇到因 pid 文件问题而启动失败的情况,这几类报错你可能遇到过:

Can‘t start server: can‘t create PID file: No such file or directory

ERROR! MySQL server PID file could not be found

ERROR! The server quit without updating PID file

上面几类 pid 相关报错解决方法其实都是类似的,首先要看下 error log 找到具体报错,然后查看配置文件,确保 pid 文件目录路径正确且有权限有空间,之后可以看下 mysqld 进程是否存在,若存在可手动 kill 掉,若有残留的 pid 文件也可以先删掉,一切排查就绪后,再次重新启动,一般即可成功。

2.socket文件介绍

socket 即 Unix 套接字文件,在类 unix 平台,客户端连接 MySQL 服务端的方式有两种,分别是 TCP/IP 方式与 socket 套接字文件方式。Unix 套接字文件连接的速度比 TCP/IP 快,但是只能连接到同一台计算机上的服务器使用。

通过设置 socket 变量可配置套接字文件路径及名称,默认值为 /tmp/mysql.sock (对于某些发行格式,目录可能有所不同)。参考配置如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 # my.cnf 配置文件 [mysqld] socket =  /data/mysql/tmp/mysql .sock [client] socket =  /data/mysql/tmp/mysql .sock   # 查看对应目录下的socket文件 root@localhost tmp] # ls -lh total 8.0K srwxrwxrwx 1 mysql mysql 0 Jun 10 15:19 mysql.sock -rw------- 1 mysql mysql 6 Jun 10 15:19 mysql.sock.lock   # 通过 -S 命令指定socket登录 [root@localhost ~] # mysql -uroot -pxxxx -S /data/mysql/tmp/mysql.sock mysql: [Warning] Using a password on the  command  line interface can be insecure. Welcome to the MySQL monitor.  Commands end with ; or \g. Your MySQL connection  id  is 12 Server version: 8.0.22 MySQL Community Server - GPL   Copyright (c) 2000, 2020, Oracle and /or  its affiliates. All rights reserved.   Oracle is a registered trademark of Oracle Corporation and /or  its affiliates. Other names may be trademarks of their respective owners.   Type  'help;'  or  '\h'  for  help. Type  '\c'  to  clear  the current input statement.   mysql> status -------------- mysql  Ver 8.0.22  for  Linux on x86_64 (MySQL Community Server - GPL)   Connection  id :          12 Current database: Current user:           root@localhost SSL:                    Not  in  use Current pager:          stdout Using outfile:           '' Using delimiter:        ; Server version:         8.0.22 MySQL Community Server - GPL Protocol version:       10 Connection:             Localhost via UNIX socket Server characterset:    utf8mb4 Db     characterset:    utf8mb4 Client characterset:    utf8mb4 Conn.  characterset:    utf8mb4 UNIX socket:             /data/mysql/tmp/mysql .sock Binary data as:         Hexadecimal Uptime:                 1 hour 27 min 31 sec   Threads: 3  Questions: 27  Slow queries: 0  Opens: 135  Flush tables: 3  Open tables: 56  Queries per second avg: 0.005

查看上述连接状态可知,MySQL 在本地可以通过 socket 方式连接。在本地登录时,如果 my.cnf 配置文件中的 [client] 部分没有指定 socket 文件路径,mysql 默认会去寻找 /tmp/mysql.sock ,所以如果 mysqld 服务启动的时候,生成的 socket 文件不是默认路径的话,登陆可能会报错(ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock')。其实 [mysqld] 部分及 [client] 部分都配置具体路径可避免此问题,也可以在 tmp 路径下建立软连接,如:ln -s /data/mysql/tmp/mysql.sock /tmp/mysql.sock 。同样的,socket 文件目录权限要对 mysql 系统用户放开。

总结:

本篇文章介绍了 MySQL 中的 pid 及 socket 文件的具体配置及作用。其实这两个参数还是比较好维护的,一开始配置好不要去动它就好了,若遇到重启报错的情况,根据错误日志慢慢来排查,细心的操作,总会找到问题的。

以上就是详解MySQL中的pid与socket的详细内容,更多关于MySQL pid与socket的资料请关注服务器之家其它相关文章!

原文链接:https://mp.weixin.qq.com/s/UDf08YdCu6BGevMdSRSfxw

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容