Reputation: 217
I have a centos EC2 with Laravel application. I also have MySQL installed on the same EC2 instance. It was working fine.
Now I decieded to move MYSQL to AWS RDS (MySQL Aurora). I can connect to AWS RDS through Heidi and query with no problem.
However in Laravel it throws exceptions. I changed the .env file credentials for DB_HOST, DB_DATABASE, DB_USERNAME, DB_PASSWORD.
See below for the exceptions:
1/3 PDOException in PDOConnection.php line 43:
SQLSTATE[HY000] [2002] Permission denied
1. in PDOConnection.php line 43
2. at PDO->__construct('mysql:host=correct_host;port=3306;dbname=correct_db', 'correct_username', 'correct_password', array('0', '2', '0', false, false)) in PDOConnection.php line 43
1/2 PDOException in PDOConnection.php line 47:
SQLSTATE[HY000] [2002] Permission denied
1/3 QueryException in Connection.php line 770:
SQLSTATE[HY000] [2002] Permission denied (SQL: select * from `users` where `users`.`id` = 1 limit 1)
Update
I just reverted the database credentials in .env to my old MySQL (installed on the same EC2 instance as the Laravel application). It works with with DB_HOST=localhost but if I use the actual ip instead, DB_HOST=127.0.0.1, it will throw the same exceptions.
Upvotes: 10
Views: 9560
Reputation: 2463
Edit your /etc/my.cnf like this:
bind-address = 0.0.0.0 # will accept requests from all IPs
Then in your mysql add a user and give him the proper permissions
grant all privileges on db_name.* to 'username'@'%' identified by 'password';
This enables the user to connect to the database from any IP
grant all privileges on db_name.* to 'username'@'your_ip' identified by 'password';
This enables the user to connect to the database from only one IP. This is the best practice, you don't want to allow connections from any IP, this is a bad security practice.
This will enable you to connect to the database from your external IP
Why your RDB instance is not working is not totaly clear, have you enabled remote connections?
Upvotes: 0
Reputation: 700
If you are running SELinux on your centos EC2, try switching off SELinux and test your application again. If it's SELinux that's causing this problem, it might well be because of the policy for external connection to database. You should switch SELinux back ON and enable the policy with this command.
sudo setsebool -P httpd_can_network_connect_db=1
Upvotes: 31