To enable remote connection to the MySQL server, port 3306 must be open for access, and the MySQL configuration must be set to accept external connections. This is usually done to provide data access from remote systems or to establish database connections between different servers in software projects.
1. Editing the MySQL Configuration
Connect to your server via SSH and open the MySQL configuration file with the following command:
nano /etc/my.cnf
If the following lines exist, remove them or disable them by adding #
at the beginning:
bind-address=127.0.0.1
skip-networking
These lines ensure that MySQL only accepts connections from localhost. Therefore, they should be deleted.
After saving, exit (CTRL + X > Y > Enter).
2. Restarting the MySQL Service
service mysqld restart
service mysql restart
Running both services on your system will not cause any harm.
3. Granting Remote Access Permission to the User
Connect to MySQL as root or an authorized user:
mysql -u root -p
Grant remote access to a user with the following command:
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;
The %
sign allows access from all IP addresses. For a specific IP address, it can be defined as 'username'@'ip_address'
.
4. Opening Port 3306 for CentOS 7 and Above
firewall-cmd --permanent --zone=public --add-port=3306/tcp
firewall-cmd --reload
These commands permanently open the TCP 3306 port and refresh the firewall settings.
5. Testing Port Openness
You can check whether the port is open from a remote client with the following command:
telnet server_ip 3306
or
nc -zv server_ip 3306
When these steps are completed, your MySQL server becomes accessible from remote systems via port 3306. For security, the IP permissions granted to the user should be limited, and if possible, access to the server from the external network should be preferred via VPN or bastion host instead of direct access.