To connect to a MySQL database hosted on Amazon EC2 from a remote server, the following steps need to be followed:
1. Verifying Remote Access Configuration
Ensure that the MySQL user you want to connect with has remote access privileges. This can be done by granting the user the GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' IDENTIFIED BY 'password'; privilege.
2. Modifying MySQL Configuration
Edit the MySQL configuration file (my.cnf) and add the following settings:
skip networking bind-address = 0.0.0.0
3. Restarting MySQL
After making the configuration changes, restart MySQL using the following command:
/etc/init.d/mysql restart
4. Verifying IP Address
Ensure that the EC2 instance has an elastic IP address assigned to it. The MySQL connection command should use this IP address instead of the local IP address.
5. Troubleshooting
If you still encounter connection errors after following these steps, check your AWS security group settings. Ensure that the appropriate inbound rules are in place to allow traffic to the MySQL port (usually TCP port 3306) from the remote server.
Specific Solution from Answer
Additionally, as suggested in the response provided, in case the bind-address in the MySQL configuration file is set to 127.0.0.1, it should be changed to 0.0.0.0 to allow connections from external IP addresses.
The above is the detailed content of How to Connect to a MySQL Database on Amazon EC2 from a Remote Server?. For more information, please follow other related articles on the PHP Chinese website!