Error 2002 cannot connect to the database server: host, socket and port

The 2002 is different from the 1045: here the application never reached the database server. No password was refused, because no conversation ever took place. The commonest forms are Can't connect to local MySQL server through socket '...' (2), Connection refused and Connection timed out. What decides the cure is the address the application tried to connect to.

Reading the message

The message What it usually is
through socket ... (2) A connection to the local server through a file (a socket). Either the service is not running, or the socket path is not what the application expects.
Connection refused It reached the machine, but nothing listens on that port: the service is stopped, or the port is wrong.
Connection timed out The request got lost on the way. From outside a shared hosting server, ports 3306 and 5432 are not open, so this is the normal result of connecting from outside.
getaddrinfo failed / Name or service not known The server name does not exist or has a typo. The host was written wrongly.

On a hosting account, in order

1 The host is localhost. It is not the website address, the server IP, or the name of the old company’s server. Anyone bringing a site from elsewhere often brings the old host in the configuration file. The list of files is in where each application keeps its database connection details.
2 Try phpMyAdmin. If it opens and you can see the tables, the database is alive and the fault is the application’s. If phpMyAdmin does not open either, and other sites on the account fail at the same time, the service is down on our side: touch nothing and open a ticket.
3 Check whether the connection comes from outside. An application on another server cannot reach it directly. The route that works is the SSH tunnel: reaching MySQL through an SSH tunnel. The Remote MySQL screen alone does not open the port: Remote MySQL in cPanel.
4 If the error comes and goes, remember what the article on the lost connection explains: the account may be running out of connections at busy hours.

On a VPS with your own database server

On a VPS the database server is yours, and the 2002 nearly always means the service is not running. Check its state with systemctl status mariadb (on some installations the service is called mysql). If it stopped, the usual reasons are a full disk and a lack of memory, which the system settles by killing the biggest process; the log journalctl -u mariadb tells you what happened. If the service is running and the error talks about a socket, the path PHP looks for may be a different one: localhost connects through the socket and 127.0.0.1 connects through the port, and trying the second unblocks many cases.

On a VPS the commands are yours. Our support does not install or repair your database server. The limits are in how far our support goes, and the first steps with the machine in first steps with your VPS.
Connections that fail “without an error” are nearly always a closed port, not a password. See which ports are open.

Checked the host and phpMyAdmin still opens, but the site will not connect? Tell us the site address and the exact message.

Open a support ticket

SEE ALSO

Error 1045: the causes, in order

When the site says it cannot connect to the database

Reaching MySQL or PostgreSQL through an SSH tunnel, step by step

Which ports are open, and why your integration hangs without an error

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from $6.59/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?