You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WSL2中Ubuntu 20.04环境连接MariaDB出现ERROR 2002 (HY000)错误求助

Fixing "Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)" in WSL 2 Ubuntu 20.04 for Zabbix

Hey there, let's work through this socket connection issue you're facing with MariaDB in your WSL 2 Ubuntu 20.04 environment for your Zabbix school lab. WSL has some unique quirks with service persistence and file paths, so here's a step-by-step breakdown to get you connected:

1. Verify MariaDB Service is Running

WSL doesn't always start system services automatically when you launch the instance. First, check if MariaDB is up:

sudo systemctl status mariadb

If it shows "inactive (dead)", start it with:

sudo systemctl start mariadb

To make sure it launches automatically when you open WSL later, enable it:

sudo systemctl enable mariadb

2. Locate the Actual MariaDB Socket File

The error mentions /run/mysqld/mysqld.sock, but in WSL, MariaDB might be using a different path. Run this command to find where the socket actually exists:

find / -name mysqld.sock 2>/dev/null

Common alternative paths in Ubuntu 20.04 include /var/run/mysqld/mysqld.sock. Note down the correct path once you find it.

3. Update Zabbix Configuration to Use the Correct Socket

Edit the Zabbix server configuration file to point to the real socket path:

sudo nano /etc/zabbix/zabbix_server.conf

Look for the line starting with # DBSocket= (remove the # to uncomment it) and set it to your found path, e.g.:

DBSocket=/var/run/mysqld/mysqld.sock

Save the file (Ctrl+O, Enter, then Ctrl+X) and restart Zabbix to apply changes:

sudo systemctl restart zabbix-server

4. (Alternative) Adjust MariaDB to Use the Expected Socket Path

If you prefer to make MariaDB use /run/mysqld/mysqld.sock instead, edit its configuration:

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

Find the socket parameter under the [mysqld] section and update it:

socket = /run/mysqld/mysqld.sock

Before restarting MariaDB, ensure the /run/mysqld directory exists and has the right permissions:

sudo mkdir -p /run/mysqld && sudo chown mysql:mysql /run/mysqld

Then restart MariaDB:

sudo systemctl restart mariadb

5. Fallback: Use TCP/IP Connection Instead of Socket

If socket issues persist, switch Zabbix to use a TCP connection instead. Edit the Zabbix config again:

sudo nano /etc/zabbix/zabbix_server.conf

Change DBHost from localhost to 127.0.0.1:

DBHost=127.0.0.1

Make sure MariaDB allows TCP connections by checking its config (/etc/mysql/mariadb.conf.d/50-server.cnf):

  • Ensure bind-address is set to 127.0.0.1 (for local only) or comment it out (to allow all connections)
  • Restart MariaDB and Zabbix afterward:
sudo systemctl restart mariadb zabbix-server

After trying these steps, test your Zabbix-MariaDB connection again. It should resolve the socket error in most WSL scenarios.

内容的提问来源于stack exchange,提问作者cheefx widdimaker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 22:52:43