duancan8382
duancan8382
2015-12-02 07:38

laravel无法连接到VM ​​LAMP上的mysql数据库

已采纳

hey guys im trying to learn laravel and im following the tutorail but i cant seem to connect to my database via VM running LAMP. I've looked thru every single post and nothing is still working

when i go run the command php artisan migrate i get the following error

[PDOException]
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '10.0.1.22' (111)

i even followed this post and got the following error

[PDOException]
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/opt/lampp/var/mysql/mysql.sock' (2)

my laravel app is running on port 8000 but my phpmyadmin is at 10.0.1.22/phpmyadmin im not sure if that could be the issue, im still a beginner so my debugging solutions are limited.

Here are my database.php file and my .envi file

database.php

        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', '10.0.1.22'),
            'database'  => env('DB_DATABASE', 'task'),
            'username'  => env('DB_USERNAME', 'root'),
            'password'  => env('DB_PASSWORD', 'test'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

and my .envi file

APP_ENV=local
APP_DEBUG=true
APP_KEY=HBzhX830sIrNbZ2hdB23DGAuGa4mj4IL

DB_HOST=10.0.1.22
DB_DATABASE=task
DB_USERNAME=root
DB_PASSWORD=test

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

thanks for all the help

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • du958642589 du958642589 6年前

    Change the DB_HOST to 127.0.0.1

    10.0.1.22 is the external address to your VM machine from your computer. Since Apache/PHP and MySQL are running on the same machine, there's no reason connect them with the external address but rather with the internal (127.0.0.1).

    When you create a user in MySQL you can set what IP'addresses it should accept connections from and as default, in most cases, the root user is usually set to only listen to internal calls (127.0.0.1).

    点赞 评论 复制链接分享
  • dongyong2906 dongyong2906 6年前

    Change your

    DB_HOST=10.0.1.22
    

    to

    DB_HOST = 127.0.0.1
    

    Because Loaclhost define on this port. Simply it means 127.0.0.1 == localhost

    $cfg['Servers'][$i]['host']

    点赞 评论 复制链接分享