红联Linux门户
Linux帮助

MySQL中socket和pid-file的作用

发布时间:2017-04-08 10:45:21来源:linux网站作者:upHailin
1.MySQL 连接方式
(1):TCP/IP 套接字方式
这种方式会在TCP/IP 连接上建立一个基于网络的连接请求,一般是client连接跑在Server上的MySQL实例,2台机器通过一个TCP/IP 网络连接。
C:\Users\gechong>mysql -h 192.168.1.10 -uroot -p
Enter password: *
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.5.20-log MySQL Community Server (GPL)
Copyright (c) 2000, 2011, 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>
这里的客户端是Windows,向IP为192.168.1.10 的服务器上的MySQL实例发起了TCP/IP请求,连接成功后就可以使用MySQL了。
(2):UNIX域套接字
UNIX域套接字并不是网络协议,所以只能在MySQL客户端和数据库实例在一台服务器上使用,用户可以在配置文件中指定套接字文件,在登录mysql的时候可以加上socket。使用socket你无须定义连接host的具体IP地址,只要为空或127.0.0.1就可以。
–socket=/tmp/mysql.sock
mysql> SHOW VARIABLES LIKE ‘socket’;
+—————+—————–+
| Variable_name | Value |
+—————+—————–+
| socket | /tmp/mysql.sock |
+—————+—————–+
1 row in set (0.00 sec)
#mysql -uroot -S/tmp/mysql.sock
 
2.MySQL pid文件
MySQL pid 文件记录的是当前 mysqld 进程的 pid,pid 亦即 Process ID。
未指定 pid 文件时,pid 文件默认名为 主机名.pid,存放的路径在默认 MySQL 的数据目录。
通过mysqld_safe启动MySQL时,mysqld_safe会检查pid文件,如果pid文件不存在,不做处理;如果文件存在,且pid已占用则报错;如果文件存在且pid未占用,则删除pid文件。
mysqld 启动后会通过 create_pid_file 函数新建 pid 文件,通过getpid() 获取当前进程 pid 并将 pid 写入 pid 文件。
因此,通过 mysqld_safe 启动时, MySQL pid 文件的作用是:防止同一个数据库被启动多次。
 
本文永久更新地址:http://www.linuxdiyf.com/linux/29836.html