远程 Mysql 服务器 (AWS aurora) 上的 Laravel 权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42937925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:37:59  来源:igfitidea点击:

Laravel permission denied on remote Mysql server (AWS aurora)

mysqllaravelamazon-web-services

提问by unknown

I have a centos EC2 with Laravel application. I also have MySQL installed on the same EC2 instance. It was working fine.

我有一个带有 Laravel 应用程序的 centos EC2。我还在同一个 EC2 实例上安装了 MySQL。它工作正常。

Now I decieded to move MYSQL to AWS RDS (MySQL Aurora). I can connect to AWS RDS through Heidi and query with no problem.

现在我决定将 MYSQL 迁移到 AWS RDS (MySQL Aurora)。我可以通过 Heidi 连接到 AWS RDS 并毫无问题地进行查询。

However in Laravel it throws exceptions. I changed the .env file credentials for DB_HOST, DB_DATABASE, DB_USERNAME, DB_PASSWORD.

但是在 Laravel 中它会抛出异常。我更改了 DB_HOST、DB_DATABASE、DB_USERNAME、DB_PASSWORD 的 .env 文件凭据。

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.

我只是将 .env 中的数据库凭据恢复到我的旧 MySQL(安装在与 Laravel 应用程序相同的 EC2 实例上)。它与 DB_HOST=localhost 一起使用,但如果我使用实际的 ip,DB_HOST=127.0.0.1,它会抛出相同的异常。

回答by m1alesis

If you are running SELinuxon 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.

如果您在 centos EC2 上运行SELinux,请尝试关闭 SELinux 并再次测试您的应用程序。如果是 SELinux 导致了这个问题,很可能是因为外部连接到数据库的策略。您应该重新打开 SELinux 并使用此命令启用策略。

sudo setsebool -P httpd_can_network_connect_db=1

回答by Odyssee

Edit your /etc/my.cnt like this:

像这样编辑你的 /etc/my.cnt:

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

然后在你的 mysql 添加一个用户并给他适当的权限

grant all privileges on db_name.* to 'username'@'%' identified by 'password';

This enables the user to connect to the database from any IP

这使用户能够从任何 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.

这使用户能够仅从一个 IP 连接到数据库。这是最佳做法,您不想允许来自任何 IP 的连接,这是一种糟糕的安全做法。

This will enable you to connect to the database from your external IP

这将使您能够从外部 IP 连接到数据库

Why your RDB instance is not working is not totaly clear, have you enabled remote connections?

为什么您的 RDB 实例无法正常工作尚不清楚,您是否启用了远程连接?