Day 4: Securing MySQL and Essential my.cnf Settings
Objective
Secure a MySQL 8.4 LTS server on Oracle Linux 9 with my.cnf settings, TLS-only connections and a source-restricted firewall rule, a hands-on lab for DBAs. By the end of Day 4, mysql84-prod01 listens on one address, refuses unencrypted connections, skips host name lookups and accepts MySQL traffic only from the admin host dba-admin01, and we have a least-privilege monitoring account to prove it.
The concept in two minutes
Where MySQL reads its settings. On Linux, mysqld reads option files in a fixed order: /etc/my.cnf, /etc/mysql/my.cnf, SYSCONFDIR/my.cnf, $MYSQL_HOME/my.cnf, any --defaults-extra-file, ~/.my.cnf, and finally DATADIR/mysqld-auto.cnf (settings saved with SET PERSIST). Files read later win. An !includedir line pulls in every file ending in .cnf from a directory, so we keep our own settings in one small file and leave the package's file alone. MySQL ignores any option file that is world-writable, and mysqld --validate-config checks the files before a restart.
What "secure by default" still leaves open. MySQL 8.4 from the official repository already generates a random temporary root password, installs the password validation component, uses caching_sha2_password, and has mysql_native_password disabled by default. Still open after installation: the server listens on every interface, accepts unencrypted TCP, resolves client host names, and (after Day 1) port 3306 is open to the whole lab subnet. We close these with bind_address, require_secure_transport, skip_name_resolve and one firewalld rich rule.
Video walkthrough
▶ Watch on YouTube: https://youtu.be/bzrS9YRoR5Y
Architecture
Prerequisites
| Item | Lab value |
|---|---|
| Database host | mysql84-prod01.dba365.lab · 192.168.56.31 · Oracle Linux 9 · 2 vCPU, 4 GB RAM, 40 GB disk |
| Admin host | dba-admin01.dba365.lab · 192.168.56.10 · Oracle Linux 9 · 1 vCPU, 2 GB RAM |
| Software | MySQL 8.4 LTS Community Server from the MySQL Yum repository (service mysqld); MySQL client from Oracle Linux 9 AppStream on dba-admin01 |
| Access | root on both hosts; the MySQL root password |
| Builds on | Day 1 (lab build, MySQL 8.4 installed and running). See all DBA365 labs and our MySQL labs. |
Implementation
Step 1 — Check the service and add an OS user (mysql84-prod01, root)
# systemctl is-active mysqld
active
# useradd -m dba
# passwd dba
What this does: confirms MySQL is running and creates an ordinary OS user, so we never run database client tools at a root prompt.
Step 2 — See which option files MySQL reads (mysql84-prod01, dba)
# su - dba
$ mysqld --verbose --help 2>/dev/null | grep -A1 'Default opt'
Default options are read from the following files in the given order:
(list starts /etc/my.cnf /etc/mysql/my.cnf ... ends ~/.my.cnf)
$ grep -Ev '^#|^$' /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
What this does: shows the option-file search order and the settings the MySQL package already placed in /etc/my.cnf. The exact middle entries of the list depend on how the binary was built; the first entry is always /etc/my.cnf. 2>/dev/null hides harmless warnings that mysqld prints when run as a non-mysql user.
Step 3 — Run mysql_secure_installation (mysql84-prod01, dba)
$ mysql_secure_installation
(enter the MySQL root password, then answer:)
Remove anonymous users? y
Disallow root login remotely? y
Remove test database and access? y
Reload privilege tables now? y
All done!
$ exit
What this does: lets you keep a strong root password and removes anonymous accounts, remote root logins and the test database. If you ran it on Day 1, running it again is safe.
Step 4 — Write our settings in their own file (mysql84-prod01, root)
# mkdir -p /etc/my.cnf.d
# grep -q '^!includedir /etc/my.cnf.d' /etc/my.cnf || \
echo '!includedir /etc/my.cnf.d' >> /etc/my.cnf
# cat > /etc/my.cnf.d/dba365-security.cnf <<'EOF'
[mysqld]
bind_address = 192.168.56.31
mysqlx_bind_address = 192.168.56.31
require_secure_transport = ON
skip_name_resolve = ON
local_infile = OFF
max_connections = 200
innodb_buffer_pool_size = 1G
EOF
What this does: keeps our server settings in one small file that /etc/my.cnf includes. bind_address and mysqlx_bind_address limit both listeners to the lab address; require_secure_transport rejects unencrypted TCP (the local socket still works); skip_name_resolve stops DNS lookups, so accounts must use IP addresses; local_infile=OFF keeps client-side file loading off (already the default, written down so it stays explicit). innodb_buffer_pool_size=1G suits a 4 GB lab VM; Day 11 covers buffer pool sizing properly.
Step 5 — Validate, restart and check the listeners (mysql84-prod01, root and dba)
# chmod 644 /etc/my.cnf.d/dba365-security.cnf
# restorecon -R /etc/my.cnf.d
# su - dba
$ mysqld --validate-config && echo 'config OK'
config OK
$ exit
# systemctl restart mysqld
# systemctl is-active mysqld
active
# ss -ltnH | awk '{print $4}' | grep -E ':3306$|:33060$'
192.168.56.31:3306
192.168.56.31:33060
What this does: sets safe permissions (MySQL ignores world-writable option files), restores the SELinux label, checks the options for errors without starting the server (no output means no errors), restarts MySQL and confirms both ports listen only on the lab address.
Step 6 — Allow port 3306 only from dba-admin01 (mysql84-prod01, root)
# firewall-cmd --permanent --remove-service=mysql
success
# firewall-cmd --permanent --add-rich-rule=\
'rule family="ipv4" source address="192.168.56.10/32" service name="mysql" accept'
success
# firewall-cmd --reload
success
# firewall-cmd --list-rich-rules
rule family="ipv4" source address="192.168.56.10/32" service name="mysql" accept
What this does: removes the subnet-wide MySQL rule opened on Day 1 and allows TCP 3306 only from the admin host. If the service was never added, firewalld prints Warning: NOT_ENABLED: mysql and continues. Port 33060 (X Protocol) stays closed in the firewall because we do not open it.
Step 7 — Create a least-privilege monitoring account (mysql84-prod01, dba)
# su - dba
$ mysql -u root -p
mysql> CREATE USER 'dbamon'@'192.168.56.10'
-> IDENTIFIED BY '<StrongPass#1>';
mysql> GRANT PROCESS, REPLICATION CLIENT ON *.*
-> TO 'dbamon'@'192.168.56.10';
mysql> GRANT SELECT ON performance_schema.*
-> TO 'dbamon'@'192.168.56.10';
mysql> SELECT user, host FROM mysql.user ORDER BY user;
+------------------+---------------+
| user | host |
+------------------+---------------+
| dbamon | 192.168.56.10 |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+---------------+
mysql> quit
$ exit
What this does: creates a monitoring account that can connect only from 192.168.56.10, with only the privileges a monitoring tool needs, and proves there are no anonymous or remote root accounts. Replace <StrongPass#1> with your own password; the validate_password policy needs upper and lower case, a digit, a special character and at least 8 characters.
Step 8 — Prepare the admin host (dba-admin01, root)
# dnf -y install mysql
# useradd -m dba
# su - dba
What this does: installs the MySQL command-line client from the Oracle Linux 9 AppStream repository (an 8.0 client, which connects to 8.4 servers with caching_sha2_password and TLS) and switches to the ordinary dba user.
Validation
Run these on dba-admin01 as OS user dba.
$ mysql -h 192.168.56.31 -u dbamon -p --ssl-mode=DISABLED
Enter password:
ERROR 3159 (HY000): Connections using insecure transport are prohibited while --require_secure_transport=ON.
$ mysql -h 192.168.56.31 -u dbamon -p \
-e "SHOW SESSION STATUS LIKE 'Ssl_version'"
Enter password:
+---------------+---------+
| Variable_name | Value |
+---------------+---------+
| Ssl_version | TLSv1.3 |
+---------------+---------+
$ mysql -h 192.168.56.31 -u dbamon -p -e \
"SHOW GLOBAL VARIABLES WHERE Variable_name IN \
('bind_address','local_infile','max_connections', \
'require_secure_transport','skip_name_resolve')"
Enter password:
+--------------------------+---------------+
| Variable_name | Value |
+--------------------------+---------------+
| bind_address | 192.168.56.31 |
| local_infile | OFF |
| max_connections | 200 |
| require_secure_transport | ON |
| skip_name_resolve | ON |
+--------------------------+---------------+
What this does: the unencrypted attempt is refused, the default (encrypted) connection succeeds over TLS 1.3, and all five settings match dba365-security.cnf.
Troubleshooting
| Error or symptom | Cause | Fix |
|---|---|---|
ERROR 3159 (HY000): Connections using insecure transport are prohibited while --require_secure_transport=ON. | Client or application driver connects without TLS (--ssl-mode=DISABLED or an old driver). | Use --ssl-mode=REQUIRED (or VERIFY_CA/VERIFY_IDENTITY with your CA) and enable TLS in the application's connection string. |
ERROR 1130 (HY000): Host '192.168.56.10' is not allowed to connect to this MySQL server | No account matches the client address. With skip_name_resolve=ON, accounts defined with host names (e.g. 'dbamon'@'dba-admin01') never match. | Create or rename the account with the IP address or subnet, e.g. RENAME USER 'dbamon'@'dba-admin01' TO 'dbamon'@'192.168.56.10'; |
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.56.31:3306' (113) | Error 113 (no route to host): the firewall rejects the source address. Error 111 (connection refused): nothing listens on that address, e.g. a wrong bind_address. | Check firewall-cmd --list-rich-rules and the client's IP; check ss -ltn on the server and bind_address. |
mysqld does not start after the change; /var/log/mysqld.log shows unknown variable | A typo in an option name or value in the new .cnf file. | Run mysqld --validate-config as dba, fix the reported line, then systemctl restart mysqld. |
Settings silently not applied; warning World-writable config file ... is ignored | The option file is writable by everyone, so MySQL skips it. | chmod 644 /etc/my.cnf.d/dba365-security.cnf and restart. |
Production considerations
Lab vs production
- Security: allow 3306 only from the application and admin subnets that need it, never
0.0.0.0/0. Replace the server's self-generated certificates with certificates from your own CA and have clients use--ssl-mode=VERIFY_IDENTITY. AddREQUIRE SSL(orREQUIRE X509) per account as defence in depth. Give each application its own account with only the privileges it needs; never share root. No default or example passwords. - Sizing:
innodb_buffer_pool_sizeis often 50–75% of RAM on a dedicated database server;max_connectionsshould follow measured peak connections plus headroom, not a guess. - HA and backup: keep the same .cnf file on every replica or cluster member (in version control), and take a backup before any configuration change.
- Rollback plan: keep a copy of the old file; to roll back, remove
/etc/my.cnf.d/dba365-security.cnf(or restore the copy) and restartmysqld; restore the firewall with--remove-rich-rule/--add-service=mysql; drop the account withDROP USER 'dbamon'@'192.168.56.10';. Before turning onrequire_secure_transportin production, check that every application connects with TLS, or schedule the change with the application teams.
Practice exercise
- Create an application account
shopappthat can connect only from the lab subnet and hasSELECT, INSERT, UPDATE, DELETEonshopdb.*only. Then show its grants.
Hint: withskip_name_resolve, use an address pattern such as'shopapp'@'192.168.56.%', and check withSHOW GRANTS FOR .... - Find out which option file set
max_connections.
Hint: queryperformance_schema.variables_infoforVARIABLE_SOURCEandVARIABLE_PATH.
Next lab
Day 5 · Oracle: managing pluggable databases (clone, unplug and plug) on ora19-prod01. Follow all DBA365 labs.
Need help hardening your own MySQL servers? See our Remote Support services.
Comments
Post a Comment