Choose one route
MySQL Community Edition is free. You only need one of the routes below. All of them end in the same place: a server process running on your machine, listening on port 3306.
This course uses MySQL 8.4 LTS. A long-term support (LTS) release gets bug and security fixes for years, which makes it a sensible version to learn on. Newer "innovation" releases also exist; the lessons here run on them as well, except where a lesson says otherwise.
Windows
- Download the MySQL Installer from the official MySQL downloads page (
dev.mysql.com/downloads/). - Run it and choose Server only (or "Developer Default" if you also want Workbench, the graphical client).
- When the installer asks for the root password, choose a strong one and write it down in a password manager. The Password Generator can make one.
- Leave "Start the MySQL Server at System Startup" ticked, and finish the wizard.
The installer registers MySQL as a Windows service, so it starts by itself after a reboot.
macOS
With Homebrew installed, one command does everything:
brew install mysql@8.4
brew services start mysql@8.4The second command starts the server now and again at login. Homebrew installs the programs into a folder that is not on your PATH by default. The install output prints the exact export PATH=... line to add to your shell profile so that the mysql command works. Alternatively, download the DMG installer from the MySQL downloads page and follow its steps.
Ubuntu and Debian
sudo apt update
sudo apt install mysql-server
sudo systemctl status mysqlThe last command shows whether the service is active (running). On these systems the root account usually authenticates through the operating system, so you connect with sudo mysql instead of a password.
Docker (any system)
If you have Docker, a throwaway server takes one line. It is also the cleanest way to experiment, because deleting the container deletes everything:
docker run --name mysql-learn -e MYSQL_ROOT_PASSWORD=choose-a-password -p 3306:3306 -d mysql:8.4--name labels the container, -e sets the root password, -p 3306:3306 makes the port reachable from your machine, and -d runs it in the background. Connect with docker exec -it mysql-learn mysql -uroot -p.
Secure the root account
A fresh install can have weak defaults. The bundled script walks you through the important ones:
sudo mysql_secure_installationIt offers to set the root password, remove anonymous users, disallow remote root login and remove the test database. Answering "yes" to those is the right choice for a learning machine as well, because it builds good habits.
Remember the rule that matters most: root is for administration only. Real applications get their own limited user. The lesson on users and privileges shows how.
Check that it works
First check the client program exists:
mysql --versionYou should see a line such as mysql Ver 8.4.x. That tells you about the client. To see what the server is, connect and ask it (the next lesson shows how to connect):
SELECT VERSION();The answer depends on which server you installed. For a default install you will see a value starting with 8.4. If mysql --version works but the connection fails with "Can't connect to MySQL server", the server is not running or is listening somewhere else. Start the service (brew services start mysql@8.4, sudo systemctl start mysql, or the Windows Services panel) and try again.
Common mistakes
- Installing two servers on port 3306. Only one program can listen on a port. If a second install fails to start, an older one is probably still running.
- Losing the root password. Write it down at once. Resetting it is possible but fiddly.
- Confusing the client and server versions.
mysql --versionreports the client. UseSELECT VERSION();for the server. - Opening port 3306 to the whole internet. A database should be reachable only from your own machine or private network.
Exercise
Install MySQL using the route that suits your system, then answer: (1) which command proves the client is installed, (2) which port should the server be listening on, and (3) which statement tells you the server version once you are connected?
Show one possible answer
mysql --version- Port 3306 (unless you chose a different one during setup).
SELECT VERSION();
Key points
- Pick one install route: the MySQL Installer on Windows, Homebrew or the DMG on macOS, apt on Ubuntu or Debian, or Docker on any system.
- The server (mysqld) must be running before any client can connect; the default port is 3306.
- Set a strong root password and never use the root account for your applications. You will create limited users later.
- Check the install with mysql --version for the client and SELECT VERSION(); for the server you are actually connected to.