The MySQL 8.0 command line client (mysql) is a text-based program that connects to a MySQL or MariaDB server, runs SQL statements, and prints the results in your terminal or Command Prompt. It ships with the MySQL 8.0 Server and MySQL Shell installation packages, so if you installed the server you almost certainly already have it. The shortest working set of steps is: open your terminal, run mysql -u root -p, type your password, and you land at a mysql> prompt where you can type SQL ending with a semicolon.

What You'll Need

  • A running MySQL 8.0 server (local or on a network host you can reach).
  • The mysql client program, installed with the server or as a separate client-only package.
  • A database user account and password. The root account works for setup tasks.
  • The hostname or IP address of the server, and the port if it is not the default 3306.
  • A terminal: Terminal on macOS or Linux, Command Prompt or PowerShell on Windows. On Windows you can also launch the preconfigured "MySQL 8.0 Command Line Client" shortcut from the Start menu, which starts the client and asks only for a password.

Step-by-Step

  1. Open a terminal in a folder you can write to. The client writes history and log files relative to where it starts, so avoid system directories. On Windows, the Start menu shortcut is the simplest route because it already knows where the client binary lives.
  2. Connect to the server. Type the connection command, replacing values with your own:
    mysql -h localhost -P 3306 -u root -p
    The -h flag is the host, -P (capital) is the port, -u is the user, and -p tells the client to prompt for a password. Do not put a space between -p and your password; leaving it out is safer because the password then never appears in your shell history.
  3. Enter your password when prompted. Nothing is echoed as you type, which is normal. If the account has no password, omit -p entirely rather than pressing Enter at the prompt.
  4. Confirm you are connected. You should see a welcome banner and a prompt like mysql>. Run SELECT VERSION(); to verify you reached a MySQL 8.0 server.
  5. See what databases exist. Run SHOW DATABASES;. The client executes nothing until it sees a semicolon, so a statement that appears to hang is usually just waiting for its terminator.
  6. Select a database. Run USE your_database_name; so subsequent queries apply to it. The prompt changes to include the database name, which is a useful confirmation.
  7. Run a query. For example SELECT user, host FROM mysql.user; or a simple SELECT * FROM your_table LIMIT 10;. Results print as a text table with a row count at the bottom.
  8. Learn the safe exit and the quit commands. Type quit or \q and press Enter to close the session cleanly. If you are stuck mid-statement, type \c to cancel it without quitting.
  9. Try the common one-liners. For scripted work you can skip the interactive prompt entirely: mysql -u root -p -e "SHOW DATABASES;" runs a single statement and exits. To run a file, use mysql -u root -p your_database_name < script.sql, or from inside the session, source /path/to/script.sql.
  10. Get help from inside the client. Type help for a list of commands, help SELECT for syntax help on a statement, and status to see connection details, the server version and current database. A vertical output format is available with \G in place of the semicolon, which is far easier to read for wide rows.

Menu labels, flags and startup shortcuts can differ slightly by client version, platform and how the server was installed, so match what your own installer offered rather than assuming.

Common Mistakes

  • Forgetting the semicolon. The client keeps waiting and looks frozen. Add ; and press Enter, or type \c to abandon the buffer.
  • Using the wrong port flag. -p means password; -P means port. Mixing them produces a password prompt you did not expect or a connection refused error.
  • Putting the password on the command line. -pMyPassword works but exposes the password to other users and to shell history. Prefer the prompt, or a properly protected option file.
  • Connecting as localhost when the socket is not where the client expects. On Unix-like systems, localhost can mean a local socket file rather than TCP. If you need TCP, use -h 127.0.0.1 instead.
  • Expecting interactive editing everywhere. Arrow keys and history recall depend on the readline library being available; on some minimal Windows setups they behave poorly. This is a client nicety, not a server problem.
  • Running destructive statements casually. DROP DATABASE, DELETE without a WHERE clause, and UPDATE without a WHERE clause act immediately. Take a backup before structural changes.
  • Assuming MariaDB behaves identically. The client connects fine to MariaDB servers, but some 8.0-specific features and authentication plugins differ.

Tips

  • Use SHOW WARNINGS; right after a statement that returned a warning; the summary line tells you how many were raised.
  • Set a default database at connect time by adding it as the last argument: mysql -u root -p mydb.
  • Redirect output to a file with > results.txt when a query returns more rows than your scrollback buffer holds.
  • Keep a .sql file for anything you will run more than once; scripts are far easier to review, version and rerun than typed statements.
  • Use \s as a shorthand for status and \h for help when you want to type less.
  • Run prompt \u@\h:\d> to show user, host and database in the prompt, so you always know where a statement will land.

Troubleshooting

If that did not work, work through these in order, simplest first:

  • command not found or 'mysql' is not recognized: the client folder is not on your PATH. Add the MySQL bin directory to PATH, use the full path to the binary, or on Windows use the Start menu shortcut.
  • Can't connect to local MySQL server through socket: the server is not running, or the socket path differs. Start the service (sudo systemctl start mysql on many Linux systems, or the Windows service manager) and retry.
  • Can't connect to MySQL server on 'host' (10061) or a timeout: wrong host or port, the server not listening on the network, or a firewall blocking it. Confirm the host and port, and check whether the server is bound to network interfaces.
  • Access denied for user: the username, password, host grant or authentication plugin is wrong. Check the account's host part (a user may exist only for localhost) and verify the password by signing in with a client that supports the account's plugin.
  • Unknown database: the database name is misspelled or does not exist. Run SHOW DATABASES; to list what is actually there.
  • Garbled characters or blank output: the terminal's character encoding or the connection charset does not match the data. Set the client charset explicitly at connect time.
  • As a last resort, reinstall the client package from the official MySQL download or your operating system's package manager. This replaces the client binary; it does not delete your server data.

Frequently Asked Questions

Is the command line client the same as MySQL Shell?

No. mysql is the traditional client; MySQL Shell (mysqlsh) is a separate program with JavaScript and Python modes in addition to SQL. Both connect to the same server.

How do I run a .sql file?

Either pass it through standard input (mysql -u root -p dbname < file.sql) or, inside an open session, use source /full/path/file.sql.

Why does the prompt not return after I press Enter?

The client is still reading a statement and waiting for its semicolon. Add one, or type \c to cancel.

Can I connect to a remote server?

Yes, with -h set to the server's address. The server must allow remote connections for your account and be reachable through any firewall between you and it.

Does the client store my password?

You can save it in an option file so you are not prompted each time, but the file must be readable only by your user account. Avoid typing the password inline in shared shells.

How do I leave the client?

Type quit, exit or \q and press Enter.