Setting up a local MySQL database
Introduction
A working MySQL setup has two parts: the MySQL server (mysqld), which stores your data and accepts connections, and a client that talks to it. This guide uses the mysql command-line client, which every installation method below includes.
There are two broad ways to run the server on your own computer:
- In a container. The official
mysqlDocker image is the quickest option on any operating system. The server is isolated from the rest of your system, you can run several versions side by side, and removing it leaves nothing behind. For most development work, this is the option we recommend. - As a native installation. MySQL's package repositories and installers set MySQL up as a system service that starts with your computer. Choose this if you can't or don't want to use Docker.
After the installation, the guide shows how to lock the server down, create a database and an account for your application, manage the service, and remove MySQL again. Jump to the part you need:
Choosing a MySQL version
Since MySQL 8.1, MySQL has had two release tracks:
- Long-term support (LTS) releases change as little as possible within a series. Updates contain bug and security fixes, and nothing is removed. An LTS series is supported for eight years (five years of premier support and three of extended support). The current LTS series are MySQL 8.4 and MySQL 9.7.
- Innovation releases add features every quarter and may change behavior or remove features from one release to the next. Each is supported only until the next one comes out. The current one is MySQL 26.7.
MySQL 9.7 is the last release line with a sequential version number. Later Innovation and LTS releases use calendar versions in the form YY.M, the year and month of the release: MySQL 26.7 is the July 2026 release. An LTS release keeps its YY.M version for as long as it is supported.
MySQL 8.0 stopped receiving updates when it moved to Oracle Lifetime Sustaining Support on April 21, 2026, and MySQL 5.7 did in October 2023 (see MySQL's end-of-life notices).
This guide installs MySQL 9.7, the newest LTS series, which gives a new project the longest support window. If you are matching an existing server, install the series it runs instead; the sections below point out where you choose the version. One difference that can decide the question: MySQL 9.7 no longer includes the mysql_native_password authentication plugin. MySQL 8.4 still ships it, disabled by default, for old clients that can't use the default caching_sha2_password.
Be careful with unversioned names. The Docker tag mysql:latest and the Homebrew formula mysql both install the Innovation release (26.7 at the time of writing), so a routine update can move you to a new release with different behavior. Always name the series, as in mysql:9.7 or mysql@9.7.
At the time of writing, the current releases are 9.7.2 and 8.4.11.
Running MySQL in Docker
You need Docker installed: Docker Desktop on macOS and Windows, or Docker Engine on Linux. The commands below are for a Unix shell such as bash or zsh (on Windows, use WSL).
First, generate a random password for MySQL's administrative root account and store it in a file that only you can read:
The umask in the subshell makes sure the file is created readable only by you, without a moment where others could read it. Keep this file out of version control. Then start the server:
Each option has a job:
-p 127.0.0.1:3306:3306makes the server reachable on port 3306 of your computer, but only from your computer. Without the127.0.0.1part, Docker publishes the port on all of the host's addresses, which makes your database reachable from the network. If port 3306 is already in use, pick another host port, such as127.0.0.1:3307:3306, and connect to that port.-v mysql-data:/var/lib/mysqlkeeps the data directory in a named volume, so your databases survive when you remove or recreate the container.MYSQL_ROOT_PASSWORD_FILEtells the image to read the root password from the mounted file. Unlike-e MYSQL_ROOT_PASSWORD=..., this keeps the password out of your shell history and out of the output ofdocker inspect. It doesn't hide the password from the container itself: the file stays mounted, and the image's startup script passes the password on to the server process as an environment variable. The image only uses the password to initialize an empty data directory; after that, it is stored in the database.mysql:9.7follows the 9.7 LTS series: pulling it again gets you the latest 9.7 patch release, never a new series. Usemysql:8.4for MySQL 8.4.
On the first start, the container initializes the data directory, runs a temporary server to set the root password, and then starts the real server. Wait until docker logs mysql shows ready for connections with port: 3306:
Now connect with the mysql client inside the container, and enter the password from mysql-root-password.txt when you're asked for it:
If you have a mysql client installed on your computer, you can also connect through the published port:
Use 127.0.0.1 rather than localhost here. The client treats localhost as a request to connect through a Unix socket file instead of TCP, and that socket only exists inside the container, so the connection fails with ERROR 2002 (HY000): Can't connect to local MySQL server through socket.
The image creates root accounts for both localhost and % (any host), so anyone who can reach the port can try to log in as root. Binding the port to 127.0.0.1 is what keeps them out. If you'll only ever administer the server with docker exec, you can also add -e MYSQL_ROOT_HOST=localhost to the docker run command before the first start. The image then creates only root@localhost, and logging in as root through the published port fails with ERROR 1130 (HY000): Host '…' is not allowed to connect to this MySQL server.
The image can also create a database and a user on the first start, through the MYSQL_DATABASE, MYSQL_USER and MYSQL_PASSWORD variables (the user gets all privileges on that database). This guide creates them by hand instead, in Creating a database and a user for your application, so that you can see and choose the privileges. The image's documentation lists all of its options.
Installing MySQL on Linux
MySQL publishes its own package repositories for Debian and Ubuntu (APT) and for Red Hat Enterprise Linux, its rebuilds and Fedora (Yum). Distributions also ship their own packages, but these may be a different release series than you want, or not MySQL at all. Here is what mysql-server gets you from some current distributions' own repositories:
| Distribution | Distribution package |
|---|---|
| Ubuntu 26.04 LTS | MySQL 8.4 (mysql-server, 8.4.11 at the time of writing) |
| Ubuntu 24.04 LTS | MySQL 8.0 (mysql-server, 8.0.46), which is no longer supported by MySQL |
| Debian 13 | No mysql-server; default-mysql-server installs MariaDB 11.8 |
| Oracle Linux 9 (RHEL 9 compatible) | MySQL 8.0 (mysql-server, 8.0.46), plus a mysql:8.4 module stream |
MariaDB started as a fork of MySQL but is now a separate database with its own versions and features. This guide doesn't cover it.
Ubuntu and Debian: the MySQL APT repository
MySQL's APT repository has packages for the current Debian and Ubuntu releases, but only for the amd64 (x86-64) architecture. On an ARM machine, the repository has no server packages, and on Ubuntu, apt-get install mysql-server then quietly installs Ubuntu's own package instead. On ARM, use Docker or, on Ubuntu 26.04, the distribution package.
The repository is set up by a small configuration package. Download it from MySQL's APT repository page (the version below is the current one at the time of writing) and install it:
The package asks which MySQL products you want. On a system without MySQL, it preselects mysql-8.4-lts. To get MySQL 9.7, select MySQL Server & Cluster, choose mysql-9.7-lts, and then select Ok. You can change the series later with sudo dpkg-reconfigure mysql-apt-config.
To make the same choice without the dialog, for example in a script, set the answer before installing the package:
Either way, the package writes the repository to /etc/apt/sources.list.d/mysql.list. On Ubuntu 24.04 with MySQL 9.7 selected, it contains:
Now update the package lists and install the server:
During the installation, you're asked for a password for MySQL's root account. If you leave it blank, root is set up to use Unix socket authentication instead of a password: only the operating system's root user (for example, through sudo mysql) can log in as MySQL's root. On a development machine, that is a good choice, because there is no administrative password to leak. Securing the installation explains how it works.
The server starts right after the installation. Check it with systemctl status mysql. The packages from MySQL's repository don't limit which network addresses the server listens on, so continue with Listening only on your own machine.
MySQL's APT repository guide covers upgrades and replacing a distribution's MySQL.
Ubuntu and Debian: the distribution's own packages
On Ubuntu 26.04, the distribution's own package is MySQL 8.4 LTS, and it's the simplest way to get a supported MySQL:
The server starts right after the installation. Ubuntu's package configures root to use Unix socket authentication, so you log in as the administrator with sudo mysql, and it only listens on 127.0.0.1.
On Ubuntu 24.04, the same commands install MySQL 8.0, which no longer receives updates from MySQL. Use MySQL's APT repository or Docker instead. On Debian, the distribution's packages are MariaDB.
RHEL, Oracle Linux, Rocky Linux and AlmaLinux: the MySQL Yum repository
MySQL's Yum repository serves Red Hat Enterprise Linux and compatible distributions, as well as Fedora. It is set up by a release package; the commands below use the one for version 9 of these distributions. For version 8 or 10, or for Fedora, pick the matching package on MySQL's Yum repository page.
The mysql97 release package enables the MySQL 9.7 LTS repositories. To install MySQL 8.4 instead, switch the enabled repositories before you install the server:
On version 8 of these distributions, the system's own mysql module hides the packages from MySQL's repository until you disable it with sudo dnf module disable mysql. Version 9 doesn't need this step.
Install the server, and start it now and on every boot:
On its first start, the server initializes its data directory and creates a root account with a random temporary password, which it writes to its log:
Log in with that password (mysql -u root -p). The temporary password is already expired, so MySQL refuses to do anything until you choose a new one with ALTER USER. These packages also enable MySQL's password validation component, whose default policy requires at least eight characters, including an uppercase letter, a lowercase letter, a digit and a special character:
Like the APT packages, the server listens on all network addresses, so continue with Listening only on your own machine. MySQL's Yum repository guide has more details.
Installing MySQL on macOS
You can install MySQL with the Homebrew package manager or with MySQL's own installer package. Pick one: both run a server on port 3306, and MySQL's installer doesn't know about a Homebrew installation.
Homebrew
Install the formula for the 9.7 series and start it as a background service (use mysql@8.4 for MySQL 8.4; plain mysql is the Innovation release):
Versioned formulas like mysql@9.7 aren't linked into Homebrew's bin directory, so add their programs to your PATH. For zsh, the default shell on macOS, run the following and then open a new terminal:
According to the formula's notes, Homebrew initializes MySQL with a root account that has no password, and configures the server to accept connections only from your own machine. Anyone with an account on your Mac can therefore log in as root with mysql -u root, so set a password right away with mysql_secure_installation (see Securing the installation).
MySQL's installer package
- On MySQL's download page for MySQL 9.7, select macOS and download the DMG Archive for your Mac: ARM for Apple silicon, x86 for Intel.
- Open the disk image and run the installer package inside it. The default installation includes the server, a preference pane for starting and stopping it, and a launch daemon that starts it at boot.
- At the end of the installation, set a password for the
rootaccount and choose whether to start the server.
MySQL is installed in a versioned directory under /usr/local, with a /usr/local/mysql symbolic link pointing to it. Add its programs to your PATH, then open a new terminal and log in with the password you chose:
The installer doesn't create a configuration file, so the server listens on all network addresses. See Listening only on your own machine. MySQL's documentation describes the installer package and the launch daemon in more detail.
Installing MySQL on Windows
MySQL Installer, the tool that older guides use, isn't available for current releases: MySQL's download page says that MySQL 8.0 is the final series with MySQL Installer. MySQL 8.4 and 9.7 come as an MSI package that installs the server together with MySQL Configurator, a separate tool that sets it up.
MySQL's Windows downloads are for x86-64 only; there is no native build for Windows on ARM. On an ARM-based Windows PC, run MySQL in Docker instead: the official image includes an arm64 variant.
- On MySQL's download page for MySQL 9.7, select Microsoft Windows and download Windows (x86, 64-bit), MSI Installer. (The general download page starts with the newest release, which is an Innovation release, so make sure you have the LTS series you want.)
- Run the MSI as an administrator. When it finishes, let it start MySQL Configurator: the server can't run until it has been configured.
- In MySQL Configurator, the choices that matter for a local database are:
- Type and Networking: choose the Development configuration type, which uses the least memory. Keep TCP/IP on port 3306, but clear Open Windows Firewall port for network access unless other computers need to reach your database.
- Accounts and Roles: set the
rootpassword. You can create accounts for your applications here too, but the SQL steps below let you choose their privileges precisely. - Windows Service: MySQL runs as a Windows service. Its default name follows the version, so it's
MySQL97for MySQL 9.7. Clear Start the MySQL Server at System Startup if you'd rather start it only when you need it. - Sample Databases: optionally install the
sakilaandworldsample databases. - Apply Configuration: select Execute to apply your choices.
To connect, call the client by its full path in PowerShell, or add MySQL's bin directory to your PATH so that mysql works on its own:
MySQL Configurator's documentation describes every step.
Securing the installation
A local database often ends up holding copies of real data, so it's worth a few minutes to make sure only you can get into it.
How root logs in
The root account can do anything on the server, and each installation method sets it up differently:
| Installation method | How root authenticates |
|---|---|
| Docker image | Password from MYSQL_ROOT_PASSWORD_FILE, from any host that can reach the port |
| MySQL APT repository (Debian and Ubuntu) | The password you entered, or Unix socket authentication if you left it blank |
| Ubuntu's own packages | Unix socket authentication |
| MySQL Yum repository (RHEL and compatible) | The password you set to replace the temporary one |
| Homebrew | No password until you set one |
| macOS installer package and Windows MSI | The password you set in the installer |
With Unix socket authentication (the auth_socket plugin), MySQL doesn't check a password. Instead, it asks the operating system which user opened the connection through the local socket file, and only lets that user log in to the MySQL account with the same name. That's why the administrator logs in with sudo mysql, while a regular user can't log in as root at all, with or without a password:
This output is from Ubuntu 26.04's MySQL 8.4 package. The mysql.* accounts are locked accounts that MySQL uses internally, and debian-sys-maint is used by the package's maintenance scripts.
Running mysql_secure_installation
mysql_secure_installation is an interactive script that sets or changes the root password, removes anonymous accounts, removes root accounts that can log in from other hosts, and drops the test database. Run it with sudo mysql_secure_installation on Linux, or mysql_secure_installation with Homebrew.
A fresh MySQL 8.4 or 9.7 data directory has no anonymous accounts or test database, so on current versions the script matters mainly for the root password:
- Homebrew: run it to give
roota password. - Debian and Ubuntu with socket authentication: the script detects it and skips the password step with the message
Skipping password set for root as authentication with auth_socket is used by default. - MySQL Yum repository: replace the temporary password with
ALTER USERfirst, as shown above, and run the script afterward if you like. - Docker: you don't need it. The root password is already set, and the port binding protects the
root@%account.
Listening only on your own machine
By default, the MySQL server accepts TCP connections on all of your computer's network addresses: the bind_address setting defaults to *. On a laptop that joins other networks, that invites anyone on the same network to try your passwords. Some installation methods already restrict this:
- Docker: the
-p 127.0.0.1:3306:3306option only publishes the port on your own machine. - Ubuntu's own packages and Homebrew: their configuration sets the server's addresses to
127.0.0.1. - Windows: MySQL Configurator doesn't ask about listening addresses, but if you cleared its firewall option, Windows Firewall keeps other computers out.
For the packages from MySQL's APT and Yum repositories and for the macOS installer package, add these two lines to the [mysqld] section of the server's configuration file (/etc/mysql/mysql.conf.d/mysqld.cnf with the APT repository, /etc/my.cnf with the Yum repository; with the macOS installer package, create /etc/my.cnf containing a [mysqld] line followed by these two lines):
The second line applies to the X Protocol port (33060), which MySQL Shell and the X DevAPI connectors use. Restart the server (sudo systemctl restart mysql with the APT repository, sudo systemctl restart mysqld with the Yum repository, the preference pane on macOS), and check the result as root:
Creating a database and a user for your application
Your applications shouldn't connect as root. Instead, give each application its own database and an account that can only work with that database. If the application's credentials leak, or a bug runs the wrong statement, the damage stays inside that one database.
Log in as root with the method from your installation (docker exec -it mysql mysql -u root -p, sudo mysql, or mysql -u root -p). The statements in this section are written for a native installation.
With Docker, write 'app'@'%' wherever this section says 'app'@'localhost'. Connections through the published port don't come from localhost as far as the server inside the container is concerned, but from the address of Docker's network gateway. The % host lets the account connect from anywhere that can reach the server, which with the 127.0.0.1 port binding means your own machine. Don't create 'localhost' accounts for your applications in the container: the image runs the server with skip-name-resolve, and granting privileges to a 'localhost' account there returns Query OK, 0 rows affected, 1 warning, where warning 1285 says that the grant won't work in this mode.
Create the database, the account and its privileges:
Replace the password with one of your own. The statements do the following:
CREATE DATABASE appcreates an empty database namedapp.CREATE USER 'app'@'localhost'creates an account namedappthat can only connect from the same machine. The account uses MySQL's default authentication plugin,caching_sha2_password.- The
GRANTstatement lets the account read and change data (SELECT,INSERT,UPDATE,DELETE) and manage tables and indexes (CREATE,ALTER,DROP,INDEX,REFERENCES), but only in theappdatabase (app.*). If a separate account runs your schema migrations, leave out everything afterDELETE.
If you use Prisma ORM, prisma migrate dev also needs a temporary shadow database, which this account isn't allowed to create. Prisma's shadow database documentation for Prisma ORM 7 lists the privileges it needs on MySQL and explains how to use a shadow database you create yourself instead.
You can check the result with SHOW GRANTS (with Docker, SHOW GRANTS FOR 'app'@'%';):
USAGE means "no privileges": the account can log in, but can't do anything outside app.
Now log in as the new user, selecting the app database (with Docker, use mysql -h 127.0.0.1 -P 3306 -u app -p app from your computer or docker exec -it mysql mysql -u app -p app):
Create a table, and check that the account can use it but can't look at other databases or create new ones:
The last two errors are what you want to see. With Docker, the first error names your Docker network's gateway address instead of localhost, and the second names 'app'@'%'. To learn more about accounts and privileges, read how to manage users and authentication in MySQL and how to manage privileges with GRANT and REVOKE. To connect from other tools, see connecting to MySQL databases.
Managing the server and finding its files
Where MySQL keeps its configuration, data and log depends on how you installed it:
| Installation method | Start and stop | Configuration file | Data directory | Error log |
|---|---|---|---|---|
| Docker | docker start mysql, docker stop mysql | Mount extra .cnf files into /etc/mysql/conf.d | The mysql-data volume | docker logs mysql |
| MySQL APT repository | sudo systemctl start mysql, sudo systemctl stop mysql | /etc/mysql/mysql.conf.d/mysqld.cnf | /var/lib/mysql | /var/log/mysql/error.log |
| Ubuntu's own packages | sudo systemctl start mysql, sudo systemctl stop mysql | /etc/mysql/mysql.conf.d/mysqld.cnf | /var/lib/mysql | /var/log/mysql/error.log |
| MySQL Yum repository | sudo systemctl start mysqld, sudo systemctl stop mysqld | /etc/my.cnf | /var/lib/mysql | /var/log/mysqld.log |
| Homebrew | brew services start mysql@9.7, brew services stop mysql@9.7 | $(brew --prefix)/etc/my.cnf | $(brew --prefix)/var/mysql | A .err file in the data directory |
| macOS installer package | The MySQL preference pane | None; create /etc/my.cnf if you need one | /usr/local/mysql/data | /usr/local/mysql/data/mysqld.local.err |
| Windows MSI | The MySQL97 service, for example net stop MySQL97 in an administrator terminal | Rerun MySQL Configurator to change settings | C:\ProgramData\MySQL\MySQL Server 9.7\Data | A .err file in the data directory |
On Linux, sudo systemctl disable mysql (or mysqld) stops the server from starting at boot, if you'd rather start it only when you need it.
Removing MySQL
Removing MySQL's programs generally leaves your data in place, so you can reinstall without losing it. Delete the data directory yourself once you're sure you don't need it; if you might, export it first.
-
Docker: remove the container, then the volume. Removing the volume deletes all of your databases.
-
Debian and Ubuntu: remove the server and the packages that were installed with it.
/var/lib/mysqlstays. With the MySQL APT repository,sudo apt-get purge mysql-community-serveralso offers to delete the data directory (it doesn't by default). -
RHEL and compatible: remove the server and the repository's release package.
dnfalso removes the client and libraries that were installed with the server./var/lib/mysqlstays. -
Homebrew: stop the service and uninstall the formula. The data directory in
$(brew --prefix)/var/mysqlisn't part of the formula and stays. -
macOS installer package: the MySQL preference pane has an Uninstall option that removes the server and, optionally, the preference pane and the launch daemon.
-
Windows: uninstall MySQL Server from the list of installed apps in Windows' settings. If
C:\ProgramData\MySQL\MySQL Server 9.7is still there afterwards, it holds your data.
A hosted Postgres database for your next project, with connection pooling built in and a free plan. Explore Prisma Postgres →