6

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.


In my 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:

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

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

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

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


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


Here is my MAMP settings:

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


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

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

2
  • hello which version of MAMP you use? Commented Jul 21, 2015 at 15:28
  • @dyachenko hi, 3.0.7.1 Commented Jul 21, 2015 at 15:30

5 Answers 5

15

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

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

Sign up to request clarification or add additional context in comments.

4 Comments

Your answer was helpful to me as I couldn't establish a connection from laravel project to MySQL using mamp, but then I added DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock to my .env file and connection were established. Your answer gave me the idea. thanks and vote up.
2020: I'm using MacOS Mojave and this was indeed what I needed to establish a connection
@DerkJanSpeelman Not working for me stackoverflow.com/questions/60865114/…
@twister_void yeah maybe it's better to just get it all setup in Docker.
10

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

DB_HOST=localhost:8889

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

Comments

6

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.

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

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

Comments

3

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

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

file: .env

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

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

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

1 Comment

For me, it is not working for doing connections to remote from local laravel copy. stackoverflow.com/questions/60865114/…
2

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.