In an SSH session on your hosting, the mysql client lets you query your databases from the command line, and mysqldump lets you make a copy of them. This article explains how to connect, and what to do if the client returns error 2002 "Can't connect to local MySQL server through socket".
In short: over SSH, run mysql -u username -p database_name and enter the password. On error 2002, remove the faulty socket setting or force a TCP connection with mysql -h 127.0.0.1 -P 3306 -u username -p database_name.
Prerequisites
Before you start
- The connection settings are the ones the Apanel console shows in Databases → MySQL, in the Database configuration block: Hostname
localhost and Port 3306.
- With
localhost (or without the -h option), the client goes through the server's local socket file. With -h 127.0.0.1, it goes through the local network (TCP) on port 3306. Both reach the same database server.
- The
mysql and mysqldump commands are installed in your SSH environment.
How do I connect to MySQL over SSH?
-
Connect to your hosting via SSH.
-
Start the client, giving the user and the database:
mysql -u username -p database_name
-
Enter the user's password when prompted. The -p option without a value makes the client ask for the password, so it appears neither on screen nor in your command history.
-
Type exit to leave the client.
To export a database to a file, mysqldump takes the same options:
mysqldump -u username -p database_name > backup.sql
Troubleshooting
-
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '…': the client is looking for the socket at a path that does not exist on our servers (for example /var/run/mysqld/mysql.sock). That path usually comes from a socket= setting in a ~/.my.cnf file, a -S or --socket option in the command, or a script copied from another server. Remove that setting, or force a TCP connection:
mysql -h 127.0.0.1 -P 3306 -u username -p database_name
-
ERROR 1045 (28000): … Access denied for user: the username or password is wrong, or the user has no access to this database. Check them in the Apanel console, on the database's users tab (see Create a MySQL database and its user).
-
You prefer a graphical interface: phpMyAdmin, available from the Apanel console, does the same without SSH.
Going further