无法使用Laravel连接到本地MySQL服务器 [英] Could not connect to local MySQL server with Laravel

查看:511
本文介绍了无法使用Laravel连接到本地MySQL服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行此命令find / -name my.cnf时,我找到了my.cnf

When I run this command find / -name my.cnf I find two locations for my.cnf

  • /opt/lampp/etc/my.cnf
  • /etc/mysql/my.cnf
  • /opt/lampp/etc/my.cnf
  • /etc/mysql/my.cnf

我不知道MySQL服务器使用的是哪一个?

I don't get which one is used by MySQL server?

而且,当我运行命令php artisan migrate时,出现此错误

And, when i run command php artisan migrate , I get this error

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

您都可以非常清楚地看到显示/var/run/mysqld/mysqld.sock的错误路径(实际上,在我的系统/桌面中.运行文件夹"中没有文件夹名称"mysqld")

You can all see very clearly the error path showing /var/run/mysqld/mysqld.sock (Actually, in my system/desktop. There is no folder name "mysqld" inside "run folder")

I have a `mysql.sock` file inside /opt/lampp/var/mysql/mysql.sock

那么,可能是什么问题. ??请帮忙. 我在这里我不存在的绑定地址问了这个问题. cnf文件-Laravel 3个小时前,但直到现在都没有响应.

So, what can be the issue. ?? Please help. I asked the question here bind-address not present in my.cnf file - Laravel 3 hours before, but no response until now.

推荐答案

首先,

  • 使用$ php -i |grep php\.ini命令在系统中找到您的 php.ini 文件,或单击
  • Find your php.ini file in your system using $ php -i |grep php\.ini command OR click where can I find the php.ini for php-cli.
  • Open php.ini file.
  • And, make sure these lines are present or not.

a)extension=mysql.so b)extension=pdo_mysql.so

现在,键入php artisan migrate命令.我确定您会遇到错误

Now, type php artisan migrate command. I'm sure you will get error

无法通过套接字连接到本地MYSQL服务器

can't connect to local MYSQL server through socket

现在,

  • 将绑定地址从localhost更改为127.0.0.1
  • 运行/opt/lampp/bin/php.
  • 运行此程序后,如果您得到
  • Change bind-address from localhost to 127.0.0.1
  • Run /opt/lampp/bin/php.
  • After running this if you get

无法加载动态库"

"unable to load dynamic library"

然后,删除php_mssql.dll extension(适用于非Windows)

Then, remove php_mssql.dll extension (for non-windows)

  • 如果未出现错误,请直接进入 Project-Name-Folder/config/database.php 文件,并添加此代码'unix_socket' => '/opt/lampp/var/mysql/mysql.sock'
  • If not getting error, come directly to Project-Name-Folder/config/database.php file and add this code 'unix_socket' => '/opt/lampp/var/mysql/mysql.sock',

找到mysql.sock的完整路径,并将其添加

Find the complete path of mysql.sock, and add it

'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),
    'database'  => env('DB_DATABASE', 'danishLara'),
    'username'  => env('DB_USERNAME', 'root'),
    'password'  => env('DB_PASSWORD', ''),
    'unix_socket'   => '/opt/lampp/var/mysql/mysql.sock', //Add this line here
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
],

这篇关于无法使用Laravel连接到本地MySQL服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆