duancan8382 2015-12-02 07:38
浏览 54
已采纳

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 2015-12-02 08:00
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失