MySQL Installation and Setup Guide

Every time you set up MySQL on a new machine, you end up searching for the same commands. This post covers OS-specific installation, running MySQL with Docker, and basic connection configuration.

Installation

Ubuntu/Linux

sudo apt-get update
sudo apt-get install mysql-server
mysql_secure_installation    # security setup
systemctl status mysql.service   # check status
sudo systemctl start mysql   # start MySQL

mysql_secure_installation walks you through setting a root password, removing anonymous users, and disabling remote root login. Always run this after a fresh install.

macOS

brew install mysql
mysql.server start
mysql -u root

With Homebrew, you can connect as root immediately without any security setup. Set a password later if needed.

Running with Docker

When you want to spin up MySQL locally without installing it on your OS, Docker is the easiest option.

docker run -d \
    --name local-mysql \
    -e MYSQL_ROOT_PASSWORD=rootpass \
    -e MYSQL_DATABASE=myapp \
    -p 3306:3306 \
    -v mysql-data:/var/lib/mysql \
    mysql:8.0 \
    --character-set-server=utf8mb4 \
    --collation-server=utf8mb4_unicode_ci

The -v mysql-data:/var/lib/mysql flag mounts a Docker volume so your data persists even if the container is removed. The last two lines set the default encoding to utf8mb4.

Resolving Port Conflicts

If port 3306 is already in use (e.g., by a locally installed MySQL), you'll get an address already in use error. Check which process is holding the port:

sudo lsof -i :3306

If it's a Homebrew-installed MySQL, stop it with:

mysql.server stop

Or kill the process directly by its PID:

sudo kill -9 <PID>

If you want to keep the existing MySQL running alongside Docker, change the host port instead:

-p 3307:3306

Connecting to the Docker Container

Once the container is running, connect with:

# From the host (requires mysql client)
mysql -h 127.0.0.1 -P 3306 -u root -prootpass

# Or from inside the container (no client needed)
docker exec -it local-mysql mysql -u root -prootpass

Managing the Docker Container

To stop or remove the container:

# Stop
docker stop local-mysql

# Stop + remove
docker stop local-mysql && docker rm local-mysql

# Remove the data volume entirely
docker volume rm mysql-data

Setting a Password

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';

Use this when you need to set or change the root password.

Connection & Basic Setup

Connecting to MySQL

mysql -u username -p

The -p flag prompts you to enter the password interactively.

Creating a Remote-Access User

-- Create user
CREATE USER 'username'@'%' IDENTIFIED BY 'password';

-- Grant all privileges
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'%' WITH GRANT OPTION;

-- Apply privileges
FLUSH PRIVILEGES;

The '%' means the user can connect from any host. In production, restrict this to specific IPs for security.

Allowing Remote Connections (Ubuntu)

# Edit config file
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf

# Comment out bind-address = 127.0.0.1, then restart
sudo /etc/init.d/mysql restart

By default, MySQL only allows local connections. To accept remote connections, comment out or change bind-address to 0.0.0.0, then restart the service.

Once you have MySQL installed and running, check out the MySQL Complete Guide for CRUD operations, indexing, table management, and other SQL syntax you'll use daily.