Laravel 连接到远程 mysql 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50555947/
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 17:45:40  来源:igfitidea点击:

Laravel connecting to remote mysql database

phpmysqllaravelshared-hosting

提问by Kasra GH

I'm trying to connect to my remote mysql database (infinityfree.net) in my Laravel (Version 5.6) application. This is my configuration in database.phpfile:

我正在尝试在我的 Laravel(版本 5.6)应用程序中连接到我的远程 mysql 数据库(infinityfree.net)。这是我在database.php文件中的配置:

'mysql' => [
        'driver' => 'mysql',
        'host' => 'sql201.epizy.com',
        'database' => 'epiz_22148680_LaravelHospital',
        'username' => 'epiz_22148680',
        'password' => 'Vxp7****R1k6',
        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix' => '',
    ],

I also changed the .envfile:

我还更改了.env文件:

DB_CONNECTION=mysql
DB_HOST=sql201.epizy.com
DB_PORT=3306
DB_DATABASE=epiz_22148680_LaravelHospital
DB_USERNAME=epiz_22148680
DB_PASSWORD=Vxp7****R1k6

However, when I try to connect to my database I get this error:

但是,当我尝试连接到我的数据库时,出现此错误:

PDOException::("SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a p period of time or established connection failed because connected host has failed to respond.

PDOException::("SQLSTATE[HY000] [2002] 连接尝试失败,因为连接方在一段时间后没有正确响应或建立连接失败,因为连接的主机未能响应。

I've seen this postand thisbut they were not answered.

我看过这个帖子这个,但他们没有回答。

回答by Jean Carlos Nu?ez Hernandez

Look the libraries of php-mysql are instaled?

看看php-mysql的库都安装好了?

look php -m, for view the modules.

查看 php -m,查看模块。

Just setting .env is enough to connecto for db

只需设置 .env 就足以连接数据库

回答by Puneet Chandhok

By default, remote access to the MySQL database server is disabled for security reasons. MySQL instance is only configured to listen for local connections. This is MySQL's default setting, but it won't work for a remote database setup since MySQL must be able to listen for an external IP address where the server can be reached.

默认情况下,出于安全原因,禁用对 MySQL 数据库服务器的远程访问。MySQL 实例仅配置为侦听本地连接。这是 MySQL 的默认设置,但它不适用于远程数据库设置,因为 MySQL 必须能够侦听可以访问服务器的外部 IP 地址。

To enable this, open up your mysqld.cnf file:

要启用此功能,请打开您的 mysqld.cnf 文件:

look for the following line,

寻找以下行,

bind-address            = 127.0.0.1

for the purpose of troubleshooting change it to 0.0.0.0.

出于故障排除的目的,将其更改为 0.0.0.0。

restart MySQL and check the connection again.

重新启动 MySQL 并再次检查连接。

if its working change 0.0.0.0 to yourWEBSERVERIPaddress

如果它的工作变化 0.0.0.0 到 yourWEBSERVERIPaddress

if you have any problems here is the complete tutorial.

如果您有任何问题,这里是完整的教程。

https://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html

https://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html