Database connection and SQL Shell

MySQLBeginner
Practice Now

Introduction

MySQL and MariaDB are relational database systems used to store and organize application data. Unlike ordinary files, a database server is designed to handle structured queries, concurrent access, permissions, and persistent storage in a controlled way.

That means you do not normally open database files with a text editor and change them by hand. You interact through a database client, and the server enforces rules about syntax, permissions, and consistency.

To manage data, you need to know how to talk to the database server. In this lab, you will learn the fundamental command-line workflow for connecting to MySQL, entering the SQL shell, and navigating databases interactively.

You will practice the core workflow of any database administrator:

  1. Connecting to the server securely.
  2. Exploring what databases are stored on the server.
  3. Selecting a specific database to work with.
  4. Disconnecting safely when finished.

Connect to the Database Server

To interact with the MySQL database, you use the mysql command-line client. This tool allows you to send commands to the server and view the results.

In this step, you will log in as the root user. The root user is the super-admin account that has full access to everything on the database server.

Be careful not to confuse the database root user with the Linux root user. They are different administrative identities living in different systems, even if they both represent high privilege.

Run the following command in your terminal:

mysql -u root -p

Explanation of the flags:

  • -u root: Specifies the username you want to use (root).
  • -p: Tells the client to prompt you for a password.

After you press Enter, the system will ask for the password.

Type root and press Enter.

Security Note: When typing passwords in a Linux terminal, no characters will appear on the screen (no stars or dots). This is a security feature to prevent people looking over your shoulder from seeing the password length. Just type root blindly and press Enter.

Once authenticated, your prompt will change to mysql>. This indicates you have left the Linux shell and are now inside the MySQL environment.

From this point onward, commands are sent to the database server as SQL statements. That is why the prompt changes: you are in a different command language and context now.

Explore Available Databases

Now that you are connected, you can see what data is stored on the server. A single MySQL server can manage many different databases (e.g., one for a blog, one for a user directory, one for metrics).

To list all databases accessible to your user, run the following SQL command at the mysql> prompt:

SHOW DATABASES;

Crucial Rule: In SQL, commands must end with a semicolon ;.

This is part of how the SQL client knows where one statement ends. In an interactive shell, the semicolon acts like a clear "execute now" marker.

This tells the server "I have finished typing my command, please execute it now."

If you forget the semicolon and press Enter, you will see a -> prompt. This means the server is waiting for you to finish the command. Just type ; and press Enter to fix it.

You should see a list including standard system databases like information_schema, mysql, performance_schema, and sys.

Select a Database

Before you can read or write data tables, you must tell MySQL which database you want to work with. This sets your "context" so you don't have to type the database name in every single query.

A single database server can host many separate databases. The USE command changes your current context so later SQL statements are interpreted against the selected database.

We will select the internal system database named mysql, which stores user accounts and privileges.

Execute the following command:

USE mysql;

You should see the message: Database changed.

Note: The USE command is a special exception that doesn't strictly require a semicolon, but it is a good habit to use one anyway.

Exit the SQL Shell

When you are finished with your database tasks, you should close the connection properly. This returns you to the standard Linux command line.

To leave the MySQL shell, type:

exit

You will see the message Bye, and your prompt will return to the familiar labex:~/project$.

Tip: You can also use quit or the keyboard shortcut Ctrl + D to exit.

Summary

In this lab, you learned the "Hello World" of database interaction:

  • Connecting securely using mysql -u -p.
  • Viewing available databases with SHOW DATABASES;.
  • Switching contexts with USE <database>;.
  • Exiting the environment safely.

These four simple commands are the foundation for everything else you will do with MySQL.