Introduction


When utilizing MySQL, you may find yourself in situations that necessitate the transfer of data between servers or the enabling of remote machines to connect to your database rather than relying solely on "localhost." However, allowing external access to your database can pose considerable security risks. It is crucial to assess whether this access is truly necessary for your particular circumstances before proceeding.


Before doing this, make sure you really need it , allowing access database to externally is a serious breach of security, still you need, let us go through steps.

You must have root access to database machine to make changes. As you are going edit "my.cnf" file. First task to find correct file. at Ubuntu it is usually /etc/mysql/my.cnf or in newer version , it may   be  at  /etc/mysql/mysql.conf.d/mysqld.cnf.


Step 1: Locate the MySQL Configuration File


On Ubuntu/Debian systems:

MySQL 5.7+: Edit /etc/mysql/mysql.conf.d/mysqld.cnf
Older versions: Check /etc/mysql/my.cnf

Tip: Confirm the correct file by searching for the [mysqld] section. Use:


Step 2: Configure MySQL to Listen Externally



    Step 3: Restart MySQL Service


    Ensure MySQL is listening on 0.0.0.0:3306 or *:3306.


    Step 4: Grant Remote User Access


        1. Connect to MySQL locally:

        2. Create a user with remote access privileges (replace userpassword, and database):


        Note: Replace % with a specific IP (e.g., 'user'@'192.168.1.5') for tighter security.


        Step 5: Configure Firewall Rules


        On Ubuntu (UFW):


        Cloud Providers: Adjust security groups (AWS/Azure/GCP) to allow inbound traffic on port 3306 from trusted IPs.


        Step 6: Test Remote Connection


        From an external machine:

        Troubleshooting:


        • Connection refused? Check firewall rules and MySQL error logs (/var/log/mysql/error.log).
        • "Access denied" error? Verify user privileges and passwords.

        Conclusion


        Allowing external access to MySQL can be beneficial; however, it introduces potential security vulnerabilities.

        To reduce these risks:

        • Limit access to only trusted IP addresses.
        • Implement robust passwords for MySQL user accounts.
        • Evaluate the option of establishing SSL encryption for secure connections.
        • Consistently review access logs for any unusual activity.

        By adhering to these guidelines, you can configure MySQL for external access securely while mitigating associated risks.