MySQL / MariaDB CLI
Manage MySQL and MariaDB databases from the command line — connect, query, back up, restore, and troubleshoot common errors.
MySQL CLI is the command-line client for connecting to MySQL and MariaDB database servers to run queries, manage users, back up data, and troubleshoot connection and permission errors.
What MySQL CLI Does and When to Use It
The
mysql command-line client connects to a MySQL or MariaDB server and provides an interactive SQL prompt. Administrators use it to create databases, manage user privileges, run queries, and import or export data. The companion
mysqldump tool creates logical backups of databases.
The MySQL CLI is the standard tool for database administration tasks that phpMyAdminalso performs through a web interface. Use the CLI for scripted operations, automated backups via crontab, large data imports, and environments where a web interface is not available.
How to Install MySQL / MariaDB
=== "Ubuntu / Debian"
```bash
sudo apt install mysql-server
```
MariaDB (a MySQL-compatible fork):
```bash
sudo apt install mariadb-server
```=== "macOS (Homebrew)"
```bash
brew install mysql
```Common Tasks with MySQL
How to Connect to MySQL from the Command Line
mysql -u root -pThe MySQL client prompts for the root password and opens an interactive SQL prompt.
How to Create a Database with MySQL
CREATE DATABASE myapp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;How to Back Up a Database with mysqldump
mysqldump -u root -p myapp > myapp_backup.sqlFor detailed instructions, see How to back up and restore a MySQL database.
MySQL Troubleshooting
| Error | Cause | Fix |
|---|---|---|
ERROR 1045 (28000): Access denied for user | Wrong password, auth plugin mismatch, or host restriction | → Full article |
ERROR 2002 (HY000): Can't connect through socket | MySQL not running, wrong socket path | → Full article |
ERROR 1049 (42000): Unknown database | Database name typo or case sensitivity on Linux | → Full article |
ERROR 2003 (HY000): Can't connect to MySQL server on host | bind-address restriction, firewall, or skip-networking | → Full article |
Table is marked as crashed and should be repaired | MyISAM table corruption | → Full article |
Related Tools and Guides
phpMyAdminprovides a web-based GUI for MySQL administration. XAMPPbundles MySQL/MariaDB with Apache and PHP for local development. Crontabschedules automated mysqldump backups.