Getting Started with MySQL Locally

Start MySQL locally, connect to the database, and run queries

🎯 Introduction

By the end of this article, you will be able to start MySQL locally, connect to the database, and run queries.


🧱 MySQL stores your application's data

MySQL is database management software that stores, queries, and updates data such as users and articles. The backend receives requests from the page and accesses MySQL; the browser does not hold database credentials directly.

One MySQL server can manage multiple databases, and each database contains tables. Each row in a table represents a record, and each column represents a field. A user table might have id and username columns to store user IDs and names.

SQL is the language used to work with databases; MySQL is the software that executes those statements. You can use SQL to query users or update a nickname, for example. Tables are related through IDs, so each article does not need to store a complete copy of its author's details.


🛠 Start a local practice environment

This course uses MySQL 8.4 LTS with the InnoDB storage engine. LTS stands for long-term support. The storage engine handles storing, reading, and writing data; InnoDB supports the transactions and row locks covered later.

Install and start Docker Desktop first. Docker runs MySQL in a container, keeping the database software separate from other software on your computer. Run the following command in your terminal:

docker run --name mysql-course \
    -e MYSQL_ROOT_PASSWORD=LocalDemo_84_only \
    -p 127.0.0.1:3306:3306 \
    -v mysql-course-data:/var/lib/mysql \
    -d mysql:8.4

--name sets the container name, and -d runs it in the background. MySQL accepts connections on port 3306, exposed here only to your local machine. If that port is already in use, change the first 3306 to 3307.

-v stores the data in a named volume called mysql-course-data, which persists independently of the container. Deleting the container does not automatically delete this named volume, but the volume itself still needs backups. The example password is for local practice only.

The Docker Desktop interface may change. Follow the installation and startup options available in your version. Make sure Docker is running before executing the command.

🔌 Connect to the server and check its version

The MySQL server stores data; the mysql client sends SQL to the server. Use the client inside the container:

docker exec -it mysql-course mysql -uroot -p

Enter the practice password you set earlier. At the mysql> prompt, run:

SELECT VERSION();

The result has the following format. Here, x represents the installed patch version; refer to your actual output:

VERSION()
8.4.x

This returns the specific patch version in the 8.4 series. The mysql:8.4 image is updated as patches are released, so your final version number may differ.

Try another query that does not depend on a table:

SELECT 'Connected successfully' AS message;

Following the steps in this article produces:

message
Connected successfully

The result contains one record with a column named message and the value “Connected successfully”. AS names the result column, and the semicolon marks the end of the SQL statement.


🔄 Reuse the environment next time

Use exit to leave the client. After restarting your computer, run the following in your terminal if the container is stopped:

docker start mysql-course

Then reconnect using the command above. Do not repeat docker run to create another container with the same name.

If the connection fails, first check whether the container is running:

docker ps -a --filter name=mysql-course

A STATUS of Up means it is running; Exited means it has stopped. If a connection fails immediately after startup, wait briefly and retry. If it keeps failing, check the startup logs:

docker logs mysql-course

Use the logs to check whether initialization has finished or the data directory has errors. Changing the password environment variable in the startup command does not automatically change the password stored in an existing data volume.


❓ Questions and Answers

Q1: Do I still need a MySQL server if I have a graphical database tool installed?

Yes. A graphical tool is also a client and must connect to a running database server.

Q2: If localStorage survives page refreshes, why do I still need a database?

localStorage can persist across refreshes, but it belongs to the current browser. A backend database centrally manages data shared across devices and users.

Q3: Can the backend use the root account directly?

root is an administrative account. The application integration article will create an application account that can only read and write data in the practice database.


🧪 Exercise

Connect to MySQL, run SELECT VERSION(), and record the actual version. Exit the client and reconnect to confirm that the server is still running.

-- Write your SQL or record your verification results here