丢失了文件localhost.localdomain.pid和mysql.sock,怎么办?

本文介绍了当MySQL因丢失localhost.localdomain.pid和mysql.sock文件而无法启动时的解决方法。包括如何找回MySQL进程ID并更新hostname.pid文件,以及如何处理mysql-bin日志文件丢失导致的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.localhost.localdomain.pid 保存的是MySQL所启动的后台服务进程的PID值,如果这个文件丢了,可以创建一些,然后把一个数字放入其中。
2./tmp/mysql.sock 文件的作用是 MySQL通过这个unix socket 文件来与本地的客户端进行通信的。如果此文件丢了,可以通过重新启动服务器来重新创建它。
不建议将此文件放到 /tmp目录下。


丢失后在mysql的安装目录是没有*.pid,mysql.sock文件造成mysql无法重启,解决方法是重新创建hostname.pid文件
(1)ps -ef |grep mysql 找出mysql的进程,如:6225;
(2)vi hostname.pid写入6225
(3)重新启动mysql正常.



如果不小心删除了mysql-bin.00000x 这样的日志文件,然后启动失败,去mysql-bin.index里清空一下

[root@localhost bin]# cat /etc/my.cnf [mysqld] bind-address=0.0.0.0 port=3306 user=mysql basedir=/usr/local/mysql datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock log-error=/data/mysql/mysql.err pid-file=/data/mysql/mysql.pid #character config character_set_server=utf8mb4 symbolic-links=0 explicit_defaults_for_timestamp=true default_authentication_plugin = mysql_native_password symbolic-links=0 plugin_load_add='component_reference_cache.so' [root@localhost bin]# journalctl -xe -u mysqld --no-pager 7月 14 17:24:42 localhost.localdomain systemd[1]: Starting MySQL 8.0 database server... ░░ Subject: mysqld.service 单元已开始启动 ░░ Defined-By: systemd ░░ Support: https://wiki.rockylinux.org/rocky/support ░░ ░░ mysqld.service 单元已开始启动。 7月 14 17:24:42 localhost.localdomain mysqld[40957]: 2025-07-14T09:24:42.446697Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release. 7月 14 17:24:42 localhost.localdomain mysqld[40957]: 2025-07-14T09:24:42.446841Z 0 [Warning] [MY-010091] [Server] Can't create test file /data/mysql/mysqld_tmp_file_case_insensitive_test.lower-test 7月 14 17:24:42 localhost.localdomain mysqld[40957]: 2025-07-14T09:24:42.446899Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead. 7月 14 17:24:42 localhost.localdomain mysqld[40957]: 2025-07-14T09:24:42.446924Z 0 [System] [MY-010116] [Server] /usr/libexec/mysqld (mysqld 8.0.41) starting as process 40957 7月 14 17:24:42 localhost.localdomain mysqld[40957]: 2025-07-14T09:24:42.449879Z 0 [Warning] [MY-010091] [Server] Can't create test file /data/mysql/mysqld_tmp_file_case_insensitive_test.lower-test 7月 14 17:24:42 localhost.localdomain mysqld[40957]: 2025-07-14T09:24:42.449886Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /data/mysql/ is case insensitive 7月 14 17:24:42 localhost.localdomain mysqld[40957]: 2025-07-14T09:24:42.450361Z 0 [ERROR] [MY-010187] [Server] Could not open file '/data/mysql/mysql.err' for error logging: Permission denied 7月 14 17:24:42 localhost.localdomain mysqld[40957]: 2025-07-14T09:24:42.450447Z 0 [ERROR] [MY-010119] [Server] Aborting 7月 14 17:24:42 localhost.localdomain mysqld[40957]: 2025-07-14T09:24:42.450568Z 0 [System] [MY-010910] [Server] /usr/libexec/mysqld: Shutdown complete (mysqld 8.0.41) Source distribution. 7月 14 17:24:42 localhost.localdomain systemd[1]: mysqld.service: Main process exited, code=exited, status=1/FAILURE ░░ Subject: Unit process exited ░░ Defined-By: systemd ░░ Support: https://wiki.rockylinux.org/rocky/support ░░ ░░ An ExecStart= process belonging to unit mysqld.service has exited. ░░ ░░ The process' exit code is 'exited' and its exit status is 1. 7月 14 17:25:43 localhost.localdomain systemd[1]: mysqld.service: Control process exited, code=exited, status=3/NOTIMPLEMENTED ░░ Subject: Unit process exited ░░ Defined-By: systemd ░░ Support: https://wiki.rockylinux.org/rocky/support ░░ ░░ An ExecStopPost= process belonging to unit mysqld.service has exited. ░░ ░░ The process' exit code is 'exited' and its exit status is 3. 7月 14 17:25:43 localhost.localdomain systemd[1]: mysqld.service: Failed with result 'exit-code'. ░░ Subject: Unit failed ░░ Defined-By: systemd ░░ Support: https://wiki.rockylinux.org/rocky/support ░░ ░░ The unit mysqld.service has entered the 'failed' state with result 'exit-code'. 7月 14 17:25:43 localhost.localdomain systemd[1]: Failed to start MySQL 8.0 database server. ░░ Subject: mysqld.service 单元已失败 ░░ Defined-By: systemd ░░ Support: https://wiki.rockylinux.org/rocky/support ░░ ░░ mysqld.service 单元已失败。 ░░ ░░ 结果为“failed”。 7月 14 17:30:58 localhost.localdomain systemd[1]: Starting MySQL 8.0 database server... ░░ Subject: mysqld.service 单元已开始启动 ░░ Defined-By: systemd ░░ Support: https://wiki.rockylinux.org/rocky/support ░░ ░░ mysqld.service 单元已开始启动。 7月 14 17:30:59 localhost.localdomain mysqld[41251]: 2025-07-14T09:30:59.223243Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release. 7月 14 17:30:59 localhost.localdomain mysqld[41251]: 2025-07-14T09:30:59.223383Z 0 [Warning] [MY-010091] [Server] Can't create test file /data/mysql/mysqld_tmp_file_case_insensitive_test.lower-test 7月 14 17:30:59 localhost.localdomain mysqld[41251]: 2025-07-14T09:30:59.223443Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead. 7月 14 17:30:59 localhost.localdomain mysqld[41251]: 2025-07-14T09:30:59.223469Z 0 [System] [MY-010116] [Server] /usr/libexec/mysqld (mysqld 8.0.41) starting as process 41251 7月 14 17:30:59 localhost.localdomain mysqld[41251]: 2025-07-14T09:30:59.226383Z 0 [Warning] [MY-010091] [Server] Can't create test file /data/mysql/mysqld_tmp_file_case_insensitive_test.lower-test 7月 14 17:30:59 localhost.localdomain mysqld[41251]: 2025-07-14T09:30:59.226390Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /data/mysql/ is case insensitive 7月 14 17:30:59 localhost.localdomain mysqld[41251]: 2025-07-14T09:30:59.226871Z 0 [ERROR] [MY-010187] [Server] Could not open file '/data/mysql/mysql.err' for error logging: Permission denied 7月 14 17:30:59 localhost.localdomain mysqld[41251]: 2025-07-14T09:30:59.226952Z 0 [ERROR] [MY-010119] [Server] Aborting 7月 14 17:30:59 localhost.localdomain mysqld[41251]: 2025-07-14T09:30:59.227071Z 0 [System] [MY-010910] [Server] /usr/libexec/mysqld: Shutdown complete (mysqld 8.0.41) Source distribution. 7月 14 17:30:59 localhost.localdomain systemd[1]: mysqld.service: Main process exited, code=exited, status=1/FAILURE ░░ Subject: Unit process exited ░░ Defined-By: systemd ░░ Support: https://wiki.rockylinux.org/rocky/support ░░ ░░ An ExecStart= process belonging to unit mysqld.service has exited. ░░ ░░ The process' exit code is 'exited' and its exit status is 1. 7月 14 17:32:00 localhost.localdomain systemd[1]: mysqld.service: Control process exited, code=exited, status=3/NOTIMPLEMENTED ░░ Subject: Unit process exited ░░ Defined-By: systemd ░░ Support: https://wiki.rockylinux.org/rocky/support ░░ ░░ An ExecStopPost= process belonging to unit mysqld.service has exited. ░░ ░░ The process' exit code is 'exited' and its exit status is 3. 7月 14 17:32:00 localhost.localdomain systemd[1]: mysqld.service: Failed with result 'exit-code'. ░░ Subject: Unit failed ░░ Defined-By: systemd ░░ Support: https://wiki.rockylinux.org/rocky/support ░░ ░░ The unit mysqld.service has entered the 'failed' state with result 'exit-code'. 7月 14 17:32:00 localhost.localdomain systemd[1]: Failed to start MySQL 8.0 database server. ░░ Subject: mysqld.service 单元已失败 ░░ Defined-By: systemd ░░ Support: https://wiki.rockylinux.org/rocky/support ░░ ░░ mysqld.service 单元已失败。 ░░ ░░ 结果为“failed”。 7月 14 17:42:23 localhost.localdomain systemd[1]: Starting MySQL 8.0 database server... ░░ Subject: mysqld.service 单元已开始启动 ░░ Defined-By: systemd ░░ Support: https://wiki.rockylinux.org/rocky/support ░░ ░░ mysqld.service 单元已开始启动。 7月 14 17:42:23 localhost.localdomain mysql-prepare-db-dir[41483]: Initializing MySQL database 7月 14 17:42:29 localhost.localdomain mysqld[41560]: 2025-07-14T09:42:29.622474Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release. 7月 14 17:42:29 localhost.localdomain mysqld[41560]: 2025-07-14T09:42:29.622487Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release. 7月 14 17:42:29 localhost.localdomain mysqld[41560]: 2025-07-14T09:42:29.624267Z 0 [Warning] [MY-010918] [Server] 'default_authentication_plugin' is deprecated and will be removed in a future release. Please use authentication_policy instead. 7月 14 17:42:29 localhost.localdomain mysqld[41560]: 2025-07-14T09:42:29.624296Z 0 [System] [MY-010116] [Server] /usr/libexec/mysqld (mysqld 8.0.41) starting as process 41560 7月 14 17:42:29 localhost.localdomain mysqld[41560]: 2025-07-14T09:42:29.626446Z 0 [ERROR] [MY-010187] [Server] Could not open file '/data/mysql/mysql.err' for error logging: Permission denied 7月 14 17:42:29 localhost.localdomain mysqld[41560]: 2025-07-14T09:42:29.626524Z 0 [ERROR] [MY-010119] [Server] Aborting 7月 14 17:42:29 localhost.localdomain mysqld[41560]: 2025-07-14T09:42:29.626659Z 0 [System] [MY-010910] [Server] /usr/libexec/mysqld: Shutdown complete (mysqld 8.0.41) Source distribution. 7月 14 17:42:29 localhost.localdomain systemd[1]: mysqld.service: Main process exited, code=exited, status=1/FAILURE ░░ Subject: Unit process exited ░░ Defined-By: systemd ░░ Support: https://wiki.rockylinux.org/rocky/support ░░ ░░ An ExecStart= process belonging to unit mysqld.service has exited. ░░ ░░ The process' exit code is 'exited' and its exit status is 1. 7月 14 17:43:30 localhost.localdomain systemd[1]: mysqld.service: Control process exited, code=exited, status=3/NOTIMPLEMENTED ░░ Subject: Unit process exited ░░ Defined-By: systemd ░░ Support: https://wiki.rockylinux.org/rocky/support ░░ ░░ An ExecStopPost= process belonging to unit mysqld.service has exited. ░░ ░░ The process' exit code is 'exited' and its exit status is 3. 7月 14 17:43:30 localhost.localdomain systemd[1]: mysqld.service: Failed with result 'exit-code'. ░░ Subject: Unit failed ░░ Defined-By: systemd ░░ Support: https://wiki.rockylinux.org/rocky/support ░░ ░░ The unit mysqld.service has entered the 'failed' state with result 'exit-code'. 7月 14 17:43:30 localhost.localdomain systemd[1]: Failed to start MySQL 8.0 database server. ░░ Subject: mysqld.service 单元已失败 ░░ Defined-By: systemd ░░ Support: https://wiki.rockylinux.org/rocky/support ░░ ░░ mysqld.service 单元已失败。 ░░ ░░ 结果为“failed”。 7月 14 17:43:30 localhost.localdomain systemd[1]: mysqld.service: Consumed 2.187s CPU time. ░░ Subject: Resources consumed by unit runtime ░░ Defined-By: systemd ░░ Support: https://wiki.rockylinux.org/rocky/support ░░ ░░ The unit mysqld.service completed and consumed the indicated resources. [root@localhost bin]# ls /var/lib/mysql/*.err ls: 无法访问 '/var/lib/mysql/*.err': 没有那个文件或目录 [root@localhost bin]# tail -n 50 /var/lib/mysql/localhost.err tail: 无法打开'/var/lib/mysql/localhost.err' 读取数据: 没有那个文件或目录 [root@localhost bin]#
最新发布
07-15
<think>我们正在处理MySQL启动失败的问题,错误信息是:"Could not open file /data/mysql/mysql.err for error logging: Permission denied",并且之前尝试查看日志文件时发现文件不存在。 根据引用[1]引用[2]中的信息,这类问题通常是由于权限问题导致的。在引用[2]中,用户通过检查错误日志文件(hostname.err)发现了权限被拒绝(Permission denied)的问题,并最终通过调整目录文件的权限解决了问题。 解决步骤: 1. 确认MySQL的错误日志文件路径。 在MySQL配置中,错误日志路径由`log_error`参数指定。如果未指定,则默认在数据目录下,文件名为`hostname.err`(其中hostname是服务器的主机名)或`mysql.err`。 根据错误信息,我们得知MySQL试图打开`/data/mysql/mysql.err`,但权限被拒绝。 2. 检查日志文件所在目录文件的权限。 MySQL服务运行的用户(通常是`mysql`)必须对日志文件所在的目录有写权限,并且对日志文件本身有写权限。 3. 如果日志文件不存在,MySQL会尝试创建它,但需要目录有写权限。如果目录不存在,则需要先创建目录。 4. 调整权限后,重新启动MySQL服务。 具体操作如下: ### 步骤1:确认MySQL的错误日志路径 查看MySQL的配置文件(通常是`/etc/my.cnf`或`/etc/mysql/my.cnf`),找到`log_error`的设置。 ```bash sudo grep "log_error" /etc/my.cnf /etc/mysql/my.cnf ``` 如果输出为空,则使用默认路径。默认路径在数据目录下,数据目录由`datadir`指定。我们可以通过以下命令查找数据目录: ```bash sudo grep "datadir" /etc/my.cnf /etc/mysql/my.cnf ``` 或者通过进程信息查看: ```bash ps aux | grep mysqld | grep -Po 'datadir=\K[^ ]+' ``` ### 步骤2:检查并修复权限 假设错误日志路径为`/data/mysql/mysql.err`,那么我们需要确保`/data/mysql`目录存在,并且MySQL用户(假设为`mysql`)对该目录有写权限。 #### 情况1:目录不存在 ```bash sudo mkdir -p /data/mysql ``` #### 情况2:目录存在但权限不正确 检查目录的权限所有者: ```bash ls -ld /data/mysql ``` 输出可能类似: ```bash drwxr-x--- 2 root root 4096 Jul 14 17:47 /data/mysql ``` 这里的所有者是root,而MySQL服务运行的用户是mysql,因此mysql用户没有写入权限。 更改目录的所有者权限: ```bash sudo chown mysql:mysql /data/mysql # 将目录所有者改为mysql用户组 sudo chmod 755 /data/mysql # 设置目录权限,允许mysql用户读写(注意:755中,所有者有rwx,组其他有rx) ``` 或者,更安全的做法是设置权限为750(只有所有者组有权限): ```bash sudo chmod 750 /data/mysql ``` #### 情况3:日志文件已存在但权限不正确 如果日志文件已经存在(可能是之前启动失败时创建的),检查其权限: ```bash ls -l /data/mysql/mysql.err ``` 如果文件的所有者不是mysql,则需要更改: ```bash sudo chown mysql:mysql /data/mysql/mysql.err sudo chmod 640 /data/mysql/mysql.err # 设置文件权限,允许mysql用户读写,组用户读 ``` ### 步骤3:重新启动MySQL服务 ```bash sudo systemctl restart mysql ``` ### 步骤4:检查MySQL服务状态日志 ```bash sudo systemctl status mysql ``` 如果启动成功,现在应该可以查看错误日志了: ```bash tail -n 50 /data/mysql/mysql.err ``` ### 步骤5:如果问题仍然存在 - 检查SELinux或AppArmor是否阻止了访问。如果是,可以暂时禁用SELinux或调整策略。 - 检查文件系统是否以只读方式挂载。 ### 附加说明:关于引用[2]中的案例 在引用[2]中,用户遇到了类似的问题,错误信息是文件找不到(Errcode: 13 - Permission denied),他们通过检查数据目录下的错误日志(hostname.err)发现了问题,并调整了相关目录的权限。因此,我们也可以查看数据目录下是否有错误日志(即使配置的日志路径不同,在启动过程中也可能在数据目录下生成日志),这可能会提供更多线索。 ### 总结 权限问题是MySQL无法写入错误日志的常见原因。通过确保MySQL用户对日志文件所在目录拥有适当的权限,通常可以解决此类问题。 --- ### 相关问题 1. 如何修改MySQL的错误日志路径? 2. MySQL启动失败还有哪些常见原因? 3. 如何配置SELinux以允许MySQL写入自定义日志目录?</think>### 解决 MySQL 8.0 启动失败:权限拒绝错误日志问题 #### 🔍 问题诊断 当 MySQL 启动失败并显示错误: ```bash Could not open file /data/mysql/mysql.err for error logging: Permission denied ``` 这表明 MySQL 服务(通常以 `mysql` 用户运行)**缺乏对日志目录的写权限**。结合引用[1][2]的案例,根本原因是: 1. **目录所有权错误**:日志目录 `/data/mysql` 被非 MySQL 用户占用 2. **权限不足**:MySQL 用户对目标路径无写权限(错误码 13) 3. **SELinux 限制**:强制访问控制阻止 MySQL 写入自定义路径 --- ### 🔧 完整解决步骤 #### 步骤 1:确认 MySQL 用户组 ```bash # 查看 MySQL 进程运行用户 ps aux | grep mysqld | grep -Po '(?<=^mysql)\S+' # 检查系统用户配置 grep mysql /etc/passwd ``` 通常用户为 `mysql` 或 `mysqld` #### 步骤 2:修复目录权限 ```bash # 创建日志目录(如果不存在) sudo mkdir -p /data/mysql/logs # 修正所有权权限 sudo chown -R mysql:mysql /data/mysql # 递归修改所有权 sudo chmod 750 /data/mysql # 设置目录权限 sudo chmod 640 /data/mysql/mysql.err # 设置日志文件权限 ``` #### 步骤 3:验证 SELinux 状态 ```bash # 检查 SELinux 是否启用 sestatus | grep "SELinux status" # 若启用,添加策略例外 sudo semanage fcontext -a -t mysqld_log_t "/data/mysql(/.*)?" sudo restorecon -Rv /data/mysql ``` #### 步骤 4:检查配置文件 在 `/etc/my.cnf` 中确认日志路径: ```ini [mysqld] log_error = /data/mysql/mysql.err # 确保路径正确 ``` #### 步骤 5:重启服务并验证 ```bash sudo systemctl restart mysql sudo systemctl status mysql # 检查服务状态 tail -f /data/mysql/mysql.err # 查看实时日志 ``` --- ### 💡 关键注意事项 1. **权限继承**: 确保父目录 `/data` 至少有 `rx` 权限给 MySQL 用户: ```bash sudo chmod o+x /data # 允许进入目录 ``` 2. **AppArmor/SELinux**: 若使用强制访问控制,需额外配置: ```bash # AppArmor 示例(Ubuntu) sudo nano /etc/apparmor.d/usr.sbin.mysqld # 添加:/data/mysql/** rwk, sudo systemctl reload apparmor ``` 3. **文件系统权限**: 使用 `namei` 检查完整路径权限链: ```bash sudo namei -l /data/mysql/mysql.err ``` --- ### 📚 根本原因分析 | 问题类型 | 检测命令 | 解决方案 | |---------|----------|---------| | **所有权错误** | `ls -ld /data/mysql` | `chown mysql:mysql /data/mysql` | | **权限不足** | `ls -l /data/mysql/mysql.err` | `chmod 640 /data/mysql/mysql.err` | | **SELinux 限制** | `audit2why -a` | `semanage fcontext` 添加策略 | | **父目录无权限** | `namei -l /data/mysql` | `chmod o+x /data` | > **重要**:MySQL 8.0 默认要求日志文件**必须存在**且可写。若文件不存在,需手动创建空文件: > ```bash > sudo touch /data/mysql/mysql.err > sudo chown mysql:mysql /data/mysql/mysql.err > ``` --- ### 🌐 相关问题 1. 如何配置 MySQL 将错误日志输出到 syslog? → 修改 `my.cnf` 添加 `syslog` 标签[^1] 2. MySQL 启动失败但无错误日志时如何诊断? → 使用 `journalctl -u mysql` 查看系统日志 3. 如何自动轮转 MySQL 错误日志以防止文件过大? → 配置 `logrotate` 或使用 `FLUSH ERROR LOGS` 命令[^2] [^1]: 引用自 MySQL 官方文档 [^2]: 基于 Linux 文件权限最佳实践
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值