Laravel 5.1 - 连接到 MySQL 数据库 (MAMP)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31541814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:00:06  来源:igfitidea点击:

Laravel 5.1 - Connecting to MySQL Database (MAMP)

mysqldatabaselaravelpdomamp

提问by senty

There are topics online that are discussing this problem however, I couldn't find any tidy explanation of the problem or any solid answers for the question. What I am trying to achieve is connecting Laravel 5.1 to MySQL Database of MAMP.

网上有一些主题正在讨论这个问题,但是,我找不到对该问题的任何整洁的解释或对该问题的任何可靠的答案。我想要实现的是将 Laravel 5.1 连接到 MAMP 的 MySQL 数据库。



In my config>app.php:

在我的config>app.php 中:

   'default' => env('DB_CONNECTION', 'mysql'),


   'mysql' => [
        'driver'    => 'mysql',
        'host'      => 'localhost:8889',
        'database'  => 'test',
        'username'  => 'root',
        'password'  => 'root',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'unix_socket'   => '/Applications/MAMP/tmp/mysql/mysql.sock',
        'prefix'    => '',
        'strict'    => false,
    ],

In my .env:

在我的.env 中:

      DB_HOST=localhost
      DB_DATABASE=test
      DB_USERNAME=root
      DB_PASSWORD=root

I also have .env.example:(which I believe has no functionality)

我也有.env.example:(我认为它没有功能)

      DB_HOST=localhost
      DB_DATABASE=homestead
      DB_USERNAME=homestead
      DB_PASSWORD=secret

I also have create_users_table.phpand create_password_resets_table.phpin my database>migrations(even though I did not run any migration:make)

我的数据库中也有create_users_table.phpand >migrations(即使我没有运行任何迁移:make)create_password_resets_table.php



MAMP is directing and running the server successfully as it loads the project on localhost.

MAMP 正在成功引导和运行服务器,因为它在本地主机上加载项目。



Here is my MAMP settings:

这是我的 MAMP 设置:

And the testdatabase is created (with tables in it which I have previously created and used in my other projects, not Laravel.)

并且test创建了数据库(其中包含我之前在其他项目中创建并使用的表,而不是 Laravel。)



Even though everything seems correct to me, when trying to submit Auth form, I am getting this error:

即使一切对我来说似乎都是正确的,但在尝试提交 Auth 表单时,我收到此错误:

PDOException in Connector.php line 50: could not find driver

  1. in Connector.php line 50

  2. at PDO->__construct ('mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;dbname=test', 'root', 'root', array('0', '2', '0', false, false)) in Connector.php line 50

  3. at Connector->createConnection('mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;dbname=test', array('driver' => 'mysql', 'host' => 'localhost:8889', 'database' => 'test', 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2', '0', false, false)) in MySqlConnector.php line 22

and so on...

Connector.php 第 50 行中的 PDOException:找不到驱动程序

  1. 在 Connector.php 第 50 行

  2. 在 PDO->__construct ('mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;dbname=test', 'root', 'root', array('0', '2', '0' , false, false)) 在 Connector.php 第 50 行

  3. at Connector->createConnection('mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;dbname=test', array('driver' => 'mysql', 'host' => 'localhost:8889' , 'database' => 'test', 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', 'collat​​ion' => 'utf8_unicode_ci', 'unix_socket' = > '/Applications/MAMP/tmp/mysql/mysql.sock', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2' , '0', false, false)) 在 MySqlConnector.php 第 22 行

等等...

采纳答案by senty

Found my answer. Here is a way to fix it:

找到了我的答案。这是修复它的方法:

  • Start MAMP
  • On the top left, go to "MAMP" -> "Preferences"
  • Go to the "PHP" tab
  • Tick PHP 5.5.17 (or whatever you have) instead of the one which is ticked by default (5.6.1 -> 5.5.17 with he latest version of MAMP)
  • 启动 MAMP
  • 在左上角,转到“MAMP”->“首选项”
  • 转到“PHP”选项卡
  • 勾选 PHP 5.5.17(或任何你拥有的)而不是默认勾选的那个(5.6.1 -> 5.5.17 与他最新版本的 MAMP)

回答by sese smith

On mac or unix you have to include the socket path in the configuration database.php file

在 mac 或 unix 上,您必须在配置 database.php 文件中包含套接字路径

i.e 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',

IE 'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',

回答by Brian Scramlin

It was pretty simple for me, I added :8889 to the localhost in the .env file.

这对我来说非常简单,我在 .env 文件中将 :8889 添加到本地主机。

DB_HOST=localhost:8889

DB_HOST=本地主机:8889

This is because in the MAMP preferences, :8889 is the default port.

这是因为在 MAMP 首选项中,:8889 是默认端口。

回答by luke

The most important thing for me was defining the UNIX socket. Because I have another MYSQL on my machine - Laravel was trying to connect to a database in that MYSQL process.

对我来说最重要的是定义 UNIX 套接字。因为我的机器上有另一个 MYSQL - Laravel 试图连接到该 MYSQL 进程中的数据库。

Defining the UNIX for the MAMP database to be used worked perfectly. Try adding this to your MYSQL configuration in database.php

为要使用的 MAMP 数据库定义 UNIX 非常有效。尝试将其添加到 database.php 中的 MYSQL 配置中

   'mysql' => [
      'driver' => 'mysql',
      'host' => env('DB_HOST', '127.0.0.1'),
      'port' => env('DB_PORT', '3306'),
      'database' => env('DB_DATABASE', 'forge'),
      'username' => env('DB_USERNAME', 'forge'),
      'password' => env('DB_PASSWORD', ''),
      'unix_socket'   => '/Applications/MAMP/tmp/mysql/mysql.sock',
      'charset' => 'utf8mb4',
      'collation' => 'utf8mb4_unicode_ci',
      'prefix' => '',
      'strict' => true,
      'engine' => null,
    ],

回答by Channaveer Hakari

As far as I am concerned it doesn't make any sense to set in database.phpas many of them suggested.

就我而言,像许多人建议的那样在database.php 中进行设置没有任何意义。

Since this change would be mostly required in the development mode. So the proper way of setting the unix_socketis as below

由于在开发模式中主要需要此更改。所以设置unix_socket的正确方法如下

file: .env

文件:.env

DB_SOCKET='/Applications/MAMP/tmp/mysql/mysql.sock'

By doing the above way already .envis included in .gitignoreand won't create any other problem while your project is remotely deployed.

通过执行上述方式,.env已经包含在.gitignore 中,并且在远程部署项目时不会产生任何其他问题。

NOTE: I have tested this setting in Laravel 5.7 and above versions

注意:我已经在 Laravel 5.7 及以上版本中测试过这个设置