Debian 9下MariaDB运行但netstat未检测到3306端口监听求助
netstat -anp | grep 3306 Shows Nothing But MariaDB is Running (Debian 9) Hey there! Let's break down why this is happening and how to fix it—it's a super common gotcha with local MariaDB connections.
The Core Issue: You're Connecting via Unix Socket, Not TCP Port 3306
When you run mysql without specifying a host (-h), the client defaults to using a Unix domain socket instead of the TCP port 3306. MariaDB might be configured to only listen on this socket (and not the TCP port), which explains why netstat doesn't pick up port 3306 but you can still connect locally.
Step 1: Verify MariaDB's Network Configuration
First, let's check how MariaDB is set up. Run this command in your terminal:
mysql -e "SHOW VARIABLES LIKE '%socket%'; SHOW VARIABLES LIKE '%bind_address%'; SHOW VARIABLES LIKE '%skip_networking%';"
Look for these key outputs:
skip_networking: If this is set toON, MariaDB is not listening on any TCP port at all—it only accepts connections via the Unix socket.bind_address: If this is set to127.0.0.1butskip_networkingisOFF, it should listen on TCP port 3306 locally. A blank orNULLvalue might mean it's only using the socket.socket: This shows the path to the Unix socket file (usually/var/run/mysqld/mysqld.sock).
Step 2: Check for Unix Socket Listener
Since netstat -anp only shows TCP/UDP ports by default, use this command to see the Unix socket listener:
netstat -anpx | grep mysql
Or use the more modern ss tool (often more reliable than netstat):
ss -lx | grep mysql
You should see a line referencing the socket path from the earlier SHOW VARIABLES output.
Step 3: Enable TCP Port 3306 (If You Need It)
If you want MariaDB to listen on TCP port 3306 (for example, to connect remotely or use TCP locally), follow these steps:
- Open your MariaDB server configuration file. On Debian 9, this is typically:
nano /etc/mysql/mariadb.conf.d/50-server.cnf - Look for the line
skip-networking—if it exists, comment it out by adding a#at the start:# skip-networking - Ensure the
bind_addressline is set to the IP you want to listen on. For local-only TCP connections:
For allowing remote connections (use with caution, and secure your setup with firewalls and strong credentials):bind-address = 127.0.0.1bind-address = 0.0.0.0 - Restart MariaDB to apply changes:
systemctl restart mariadb - Now run
netstat -anp | grep 3306again—you should see a line like:tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 1234/mysqld
Quick Test to Confirm TCP vs Socket Connection
- Try connecting via TCP explicitly:
If this fails, MariaDB isn't listening on TCP port 3306. If it works, then both socket and TCP connections are enabled.mysql -h 127.0.0.1
内容的提问来源于stack exchange,提问作者tomsk

