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

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

嘿伙计们我正在尝试学习laravel和im跟随tutorail但我似乎无法通过VM运行连接到我的数据库 灯。 我已经通过每一个帖子看了什么都没有工作 p>

当我运行命令 php artisan migrate code>时我得到以下错误 p> \ n

[PDOException]
SQLSTATE [HY000] [2003]无法连接到'10 .0.1.22上的MySQL服务器'(111) code> p>

我甚至按照这个发布并获得了 以下错误 p>

[PDOException]
SQLSTATE [HY000] [2002]无法通过socket'/ opt / lampp / var /连接到本地MySQL服务器 mysql / mysql.sock'(2) code> p>

我的laravel应用程序在端口 8000 code>上运行,但我的phpmyadmin在 10.0.1.22 / phpmyadmin code>我不确定这可能是问题,我仍然是一个初学者,所以我的调试解决方案是有限的。 p>

这是我的 database.php code>文件和我的 .envi code>文件 p>

数据库。 php p>

 'mysql'=>  [
'driver'=>  'mysql',
'host'=>  env('DB_HOST','10 .0.1.22'),
'数据库'=>  env('DB_DATABASE','task'),
'username'=>  env('DB_USERNAME','root'),
'密码'=>  env('DB_PASSWORD','test'),
'charset'=>  'utf8',
'collat​​ion'=>  'utf8_unicode_ci',
'前缀'=>  '',
'严格'=>  false,
],
  code>  pre> 
 
 

和我的.envi文件 p>

  APP_ENV = local 
APP_DEBUG = true \  nAPP_KEY = HBzhX830sIrNbZ2hdB23DGAuGa4mj4IL 
 
DB_HOST = 10.0.1.22 
DB_DATABASE =任务
DB_USERNAME =根
DB_PASSWORD =测试
 
CACHE_DRIVER =文件
SESSION_DRIVER =文件
QUEUE_DRIVER =同步
 
MAIL_DRIVER = SMTP 
MAIL_HOST = mailtrap.io \  nMAIL_PORT = 2525 
MAIL_USERNAME = null 
MAIL_PASSWORD = null 
MAIL_ENCRYPTION = null 
  code>  pre> 
 
 

感谢所有帮助 p> div>

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

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']