如何使用 Laravel 框架 5.1 在端口 3308 上连接到 MySQL 数据库?

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

How to connect to MySQL database on port 3308 using laravel framework 5.1?

phpmysqllaravellaravel-5

提问by Junior

I am trying to use laravel for the first time. I opned the database.php file located in the config directory and then update the mysql config.

我第一次尝试使用 Laravel。我打开了位于 config 目录中的 database.php 文件,然后更新了 mysql 配置。

but every time I try to do this command php artisan migrate:install

但每次我尝试执行此命令时 php artisan migrate:install

I get this [PDOException] SQLSTATE[HY000] [2002] No connection could be made because the target machi ne actively refused it.

我得到这个 [PDOException] SQLSTATE[HY000] [2002] 无法建立连接,因为目标机器主动拒绝它。

I have to let laravel to connect to a different port somehow.

我必须让 laravel 以某种方式连接到不同的端口。

I have tried the following and none worked.

我尝试了以下方法,但都没有奏效。

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', '10.15.1.5'),
        'port'      => '3308',
        'database'  => env('DB_DATABASE', 'mydb_dev'),
        'username'  => env('DB_USERNAME', 'user'),
        'password'  => env('DB_PASSWORD', 'pass'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

and this

和这个

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', '10.15.1.5:3308'),
        'database'  => env('DB_DATABASE', 'mydb_dev'),
        'username'  => env('DB_USERNAME', 'user'),
        'password'  => env('DB_PASSWORD', 'pass'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

and this

和这个

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', '10.15.1.5'),
        'port'      => env('DB_PORT', '3308'),
        'database'  => env('DB_DATABASE', 'mydb_dev'),
        'username'  => env('DB_USERNAME', 'user'),
        'password'  => env('DB_PASSWORD', 'pass'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

finally, I tried this

最后,我试过这个

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => '10.15.1.5:3308',
        'database'  => env('DB_DATABASE', 'mydb_dev'),
        'username'  => env('DB_USERNAME', 'user'),
        'password'  => env('DB_PASSWORD', 'pass'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

this gives me a different error

这给了我一个不同的错误

Access denied for user 'homestead'@'10.xxxxxx' (using password: YES)

I am not sure where is the user homesteadis coming from.

我不确定用户homestead来自哪里。

How can I tell laravel to connect to mysql on port 3308?

如何告诉 Laravel 在端口 3308 上连接到 mysql?

采纳答案by Junior

I figured out the issue. the file .env needs to be updated with the correct information

我想通了这个问题。文件 .env 需要用正确的信息更新

回答by CenterOrbit

I know you figured it out, but of all the attempts you provided, the answer you gave wasn't clear. For those looking in the future, here is what you need:

我知道您已经想通了,但是在您提供的所有尝试中,您给出的答案并不清楚。对于那些展望未来的人,这里是你需要的:

(This is assuming Laravel 5.1 using a Postgres DB, but should work with alternate versions of Laravel, and different DBs... also, don't mind the alternate/different config settings that my database.php has as opposed to yours, these were for advanced configurations.)

(这是假设 Laravel 5.1 使用 Postgres DB,但应该与 Laravel 的替代版本和不同的 DB 一起使用...此外,不要介意我的 database.php 与您的不同的替代/不同配置设置,这些用于高级配置。)

Add a 'port'section to your config/database.php, that looks like follows:

'port'向您的 中添加一个部分config/database.php,如下所示:

        'pgsql' => [
        'read' => [
            'host' => env('DB_READ', 'localhost')
        ],
        'write' => [
            'host' => env('DB_WRITE', 'localhost')
        ],
        'port'      => env('DB_PORT', '5432'),
        'driver'   => 'pgsql',
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset'  => 'utf8',
        'prefix'   => '',
        'schema'   => env('DB_SCHEMA', 'public'),
        'options'  => array(
            PDO::ATTR_PERSISTENT => env('DB_PERSISTENT', false),
        ),
    ],

Then in your .envyou can override the portsetting as follows:

然后在您.env可以覆盖port设置如下:

DB_PORT=32769