CentOS LNMP环境数据库连接问题可按以下步骤排查解决:
检查服务状态
systemctl status mysql
或 systemctl status mariadb
。systemctl start mysql
启动服务。验证配置文件
/etc/my.cnf
或 /etc/mysql/my.cnf
,确认端口(默认3306)、监听地址等设置正确。排查网络与防火墙
ping
测试服务器连通性,telnet IP 端口
检查端口是否开放(如3306)。firewall-cmd --add-port=3306/tcp --permanent
,然后 firewall-cmd --reload
。处理权限与认证
GRANT ALL PRIVILEGES ON *.* TO '用户名'@'%' IDENTIFIED BY '密码';
FLUSH PRIVILEGES;
bindIp
允许本地访问(如 bindIp=0.0.0.0
)。查看日志定位问题
/var/log/mysql/error.log
或 /var/log/mariadb/error.log
,排查启动或连接错误。/var/log/nginx/error.log
,确认是否因PHP-FPM或数据库连接导致502等问题。其他常见问题
netstat -tuln | grep 3306
检查端口占用,修改配置文件避免冲突。setenforce 0
,若问题解决需调整安全策略。参考来源: