Skip to content
elephantoo

Installing MySQL 8 & clients

Lesson 2 of 31 15 min read

Install MySQL Server on Linux, macOS or Windows, secure it, and connect with the mysql client or a GUI.


Before you can write SQL you need two things: a MySQL server that stores the data and a client to talk to it. This lesson installs both on Linux, macOS or Windows (or in Docker), secures the server, creates a day-to-day user, and tours the mysql command-line client you will use for the rest of the course.

Which version?#

MySQL now has two release tracks:

  • LTS (Long-Term Support), for example 8.4 LTS: bug and security fixes only, supported for years. Choose this for servers.
  • Innovation releases (9.x): new features every few months, shorter support.

Everything in this course works on MySQL 8.0 and 8.4. Features that need a specific minor version (for example INTERSECT, added in 8.0.31) are labelled as they come up.

MariaDB is a community fork of MySQL. It is the default "mysql" package on Debian and some other distros. It is very similar, but not identical: roles, JSON and some newer features behave differently. When a lesson uses something MySQL-8-specific, it says so.

Installing on Ubuntu#

Ubuntu ships real MySQL 8 in its own repositories:

Terminal
sudo apt update
sudo apt install mysql-server
sudo systemctl status mysql     # should say "active (running)"
mysql --version

The package sets root@localhost to use the auth_socket plugin. Instead of a password, MySQL checks that you are the Linux root user, so you log in with sudo:

Terminal
sudo mysql

Installing on Debian, Fedora and RHEL#

On Debian, apt install default-mysql-server gives you MariaDB. For Oracle's MySQL, add the official repository from dev.mysql.com/downloads/repo/apt (the mysql-apt-config package), then install mysql-server with apt as above.

On Fedora:

Terminal
sudo dnf install community-mysql-server
sudo systemctl enable --now mysqld

On RHEL, Rocky or AlmaLinux, sudo dnf install mysql-server installs MySQL 8 from AppStream, and the service is also called mysqld.

Installing on macOS and Windows#

On macOS with Homebrew:

Terminal
brew install mysql
brew services start mysql
mysql -u root          # a fresh Homebrew install has no root password

On Windows, download the MySQL Community Server MSI from dev.mysql.com/downloads. The wizard sets the root password, installs MySQL as a Windows service and can add MySQL Workbench. Afterwards, open "MySQL Command Line Client" from the Start menu, or add C:\Program Files\MySQL\MySQL Server 8.4\bin to your PATH so that mysql works in PowerShell.

Installing with Docker (any OS)#

If you have Docker installed, this is the quickest clean setup, and it is easy to throw away afterwards:

Terminal
docker run --name mysql8 \
  -e MYSQL_ROOT_PASSWORD=change-me \
  -p 3306:3306 -d mysql:8.4

docker exec -it mysql8 mysql -u root -p

The data lives inside the container. Add -v mysql-data:/var/lib/mysql to keep it in a named volume that survives docker rm.

Securing a new server#

On a native install, run the hardening script once:

Terminal
sudo mysql_secure_installation

It offers to enable the password-strength component, then asks whether to remove anonymous users, disallow remote root login and remove the test database. Answer yes to all three.

Create your own user#

Don't do everyday work as root. Create a user with rights on your learning databases only:

SQL
CREATE USER 'dev'@'localhost' IDENTIFIED BY 'Str0ng-Passw0rd!';
GRANT ALL PRIVILEGES ON shop.* TO 'dev'@'localhost';

'dev'@'localhost' means "user dev, connecting from this machine". shop.* means every table in the shop database (it doesn't need to exist yet). Now connect as that user:

Terminal
mysql -u dev -p shop

The last argument is the database to USE automatically. You will learn much more about accounts in Users, privileges & security.

Connection options you'll use#

Terminal
mysql -u dev -p                     # prompt for the password
mysql -h db.example.com -P 3306 -u dev -p shop   # remote host and port
mysql -u dev -p -e "SELECT NOW();"  # run one statement and exit
mysql -u dev -p shop < schema.sql   # run a script file

Never type the password directly after -p on a shared machine (-pSecret), because it ends up in your shell history and the process list. To avoid typing it at all, store it in an option file that only you can read:

Terminal
cat > ~/.my.cnf <<'CNF'
[client]
user=dev
password=Str0ng-Passw0rd!
CNF
chmod 600 ~/.my.cnf

mysql_config_editor set --login-path=dev --user=dev --password does the same thing with an obfuscated file, and then mysql --login-path=dev connects.

A tour of the mysql client#

Once you are connected you'll see the mysql> prompt. Try these:

SQL
SELECT VERSION(), CURRENT_USER(), NOW();
Output
+-----------+----------------+---------------------+
| VERSION() | CURRENT_USER() | NOW()               |
+-----------+----------------+---------------------+
| 8.4.9     | root@localhost | 2026-09-30 10:15:02 |
+-----------+----------------+---------------------+

Wide results are easier to read vertically. End the statement with \G instead of ;:

SQL
SELECT VERSION(), CURRENT_USER(), NOW()\G
Output
*************************** 1. row ***************************
     VERSION(): 8.4.9
CURRENT_USER(): root@localhost
         NOW(): 2026-09-30 10:15:02

Handy client commands (these are client commands, not SQL):

CommandWhat it does
status or \sServer version, current database, connection info
\cCancel the statement you're typing
source file.sqlRun a SQL script
pager less -SScroll wide output sideways (Linux/macOS)
help SELECTBuilt-in syntax help
exit or \qQuit

Press Ctrl+C to cancel a running query, and use the up arrow to recall earlier statements.

Graphical clients#

The command line is worth mastering, but GUIs help you explore:

  • MySQL Workbench: Oracle's free tool for queries, schema diagrams and administration.
  • DBeaver Community: free, cross-platform and works with many databases.
  • VS Code extensions such as SQLTools, for querying from your editor.

All of them connect with the same four details: host, port (3306), user and password.

Troubleshooting#

ErrorUsual cause and fix
ERROR 2002 (HY000): Can't connect to local MySQL server through socketThe server isn't running. Try sudo systemctl start mysql (or mysqld).
ERROR 1045 (28000): Access denied for user 'root'@'localhost'Wrong password, or root uses auth_socket, so use sudo mysql.
ERROR 1049 (42000): Unknown database 'shop'Create it first: CREATE DATABASE shop;
command not found: mysqlThe client isn't installed or isn't on your PATH.

What's next#

With a server running and a client connected, you're ready to create databases and tables and learn how to change them later.

Check your understanding

Quick quiz

0/3 answered
  1. 1.On Ubuntu, right after sudo apt install mysql-server, how do you usually log in as the MySQL root user?

  2. 2.Which tool is designed to lock down a fresh MySQL installation (remove anonymous users, test database, etc.)?

  3. 3.In the mysql client, what does ending a query with \G instead of ; do?

Finished reading?

Mark this lesson complete to track your progress.