Installing MySQL 8 & clients
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:
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:
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:
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:
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:
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:
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:
'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:
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#
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:
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:
Wide results are easier to read vertically. End the statement with \G instead of ;:
Handy client commands (these are client commands, not SQL):
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#
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
1.On Ubuntu, right after
sudo apt install mysql-server, how do you usually log in as the MySQL root user?2.Which tool is designed to lock down a fresh MySQL installation (remove anonymous users, test database, etc.)?
3.In the mysql client, what does ending a query with
\Ginstead of;do?
Finished reading?
Mark this lesson complete to track your progress.