How to Allow Remote Access to MySQL

Pre-requisites:

A system with CentOS Stream 9 installed and running
Access to a terminal or command line
Root or sudo privileges
MySQL 8.0

In this guide, we will show you how to how to setup a user account and access a MySQL server remotely on a Linux system.

You will need to open MySQL's default port 3306 to allow traffic to MySQL

Open port

firewall-cmd --permanent --zone=public --add-port=3306/tcp

Reload the firewall to apply the changes and make the newly opened port accessible

firewall-cmd --reload

Firewalld doesn't automatically update its rules until you explicitly reload it

List open ports

firewall-cmd --zone=public --list-ports

The telnet command allows you to check if the port is opened for communication. If the connection is successful, you see the message: Connected to <host_IP>. If the connection fails, you see the message: Unable to connect to remote host.

The syntax for using the "telnet" command to connect to a specific host and port is: 

telnet [host_address] [port_number]

"host_address" is the IP of the server you want to connect to

"port_number" is the specific port you want to access on that server

Show MySQL users and hosts they are allowed to connect from:

select user, host from mysql.user;

Grant all privileges to user in MySQL 8.0

CREATE USER 'user'@'localhost' IDENTIFIED BY 'PASSWORD';
GRANT ALL ON *.* TO 'user'@'ip_address';
FLUSH PRIVILEGES;

Make sure to replace "ip_address" with the specific IP address you want to allow connections from.

Learn

Related articles

Getting Started with React

In this guide, we will cover the basics of setting up a local development environment, starting a simple React project, and the basics of how it works.

How to Create Bash Aliases

This tutorial demonstrates how to use the alias command to create personalized shortcuts, which can help you save time and feel less frustrated.

Using Git for PHP Development

This guide walks through the fundamentals of Git. In this tutorial, we will show you Git commands. For Linux you can use the built-in terminal.

How to Connect to MySQL with Laravel

In this guide, you will learn how to connect your Laravel application to your MySQL database.

How do you change the default SSH Port on CentOS Stream 9?

Changing the default SSH port adds an extra layer of security by reducing the risk of your password being cracked from a brute force attack.

What is Inheritance in PHP?

In this tutorial we will explain inheritance in PHP, a mechanism that allows a child class to inherit properties and behaviors from a parent class.