简体   繁体   English

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

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

I am trying to use laravel for the first time. 我正在尝试第一次使用laravel。 I opned the database.php file located in the config directory and then update the mysql config. 我选择位于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 homestead is coming from. 我不确定用户homestead是哪里来的。

How can I tell laravel to connect to mysql on port 3308? 如何告诉laravel在端口3308上连接到mysql?

I figured out the issue. 我发现了问题。 the file .env needs to be updated with the correct information .env文件需要使用正确的信息进行更新

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的替代版本和不同的DBs一起使用...此外,不要介意我的database.php与您的替代/不同的配置设置,这些用于高级配置。)

Add a 'port' section to your config/database.php , that looks like follows: 在您的config/database.php添加一个'port'部分,如下所示:

        '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 .env you can override the port setting as follows: 然后,在您的.env您可以覆盖port设置,如下所示:

DB_PORT=32769

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM