How do you allow remote connections to a MySQL server?

IntermediateHands-onMySQL
Answer

Short answer

Three things must all be true: MySQL must listen on a network interface (bind-address), a user must exist for the remote host, and the firewall / security group must allow port 3306 from the client.

Step 1: listen on the network

# /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address = 0.0.0.0        # or the server's private IP
sudo systemctl restart mysql
sudo ss -tlpn | grep 3306     # should show 0.0.0.0:3306 (or the private IP)

Step 2: a user for that host

CREATE USER 'shop_app'@'10.0.11.%' IDENTIFIED BY 'Str0ng-Pass!';
GRANT SELECT, INSERT, UPDATE, DELETE ON shop.* TO 'shop_app'@'10.0.11.%';

Step 3: open the port — only to the app

sudo ufw allow from 10.0.11.0/24 to any port 3306 proto tcp

In AWS: the DB security group allows 3306 from the app's security group, not from 0.0.0.0/0.

Test from the app server

nc -zv 10.0.21.10 3306
mysql -h 10.0.21.10 -u shop_app -p shop -e "select 1"

What to say in the interview

"Set bind-address to a reachable interface, create the user for the client's host pattern with least privilege, and open 3306 only from the app tier in the firewall or security group. Then test with nc and the mysql client from the app server."

All Databases (MySQL, PostgreSQL, MongoDB) questions Roadmap: MySQL basics

More on MySQL