mesqueeb mesqueeb - 1 year ago 108
SQL Question

Manual set up of MySQL locally for running Laravel artisan migrate (without homestead) ERROR 2002 (HY000)

I'm not using Laravel's Homestead, but just the manual approach.

However, I can't seem to get Laravel mysql migrations to work even though I've installed mysql through homebrew...

I get many errors, under which:

SQLSTATE[HY000] [2002] Connection Refused


ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

Upon many digging into the matter I found out the answer. See below for the solution!

Answer Source

① Check if mysql is properly installed and also set up.

brew info mysql

If it's not installed then google for how to install 'mysql through homebrew'. If it is installed do not forget to set up your mysql by doing what homebrew tells you:

We've installed your MySQL database without a root password. To secure it run:

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start

In your terminal do everything above. If you get the following error upon mysql -uroot:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

Then try: mysql -uroot -h I don't know why but it selects the wrong host by default.

If this still doesn't work maybe you already set a password, in that case you need to add -p to the command: mysql -uroot -h -p

If you got in properly: That's great! Let's create our first Database.

② Creating the database you'll work with.

So if you didn't use Laravel Homestead you'll have to create your database first. Once you got into your mysql through the commands above it should say: mysql> in the terminal now.

Then just write: CREATE DATABASE my_db; where you can write any name instead of my_db. That's it!

③ Setting up your Laravel settings.

In your Laravel project folder there will be a .env file. Open that and look inside, and search for the following part:


Even though I didn't install Laravel through homestead, it has all the 'homestead' settings... Here's what you need to change:

  1. Check if the DB_HOST is set to
  2. Set the DB_DATABASE to the one you created. In this case my_db
  3. Set a preferred DB_USERNAME or try root.
  4. Set the DB_PASSWORD to the password for MySQL you choose at the very beginning, when you set up your mysql through homebrew.


You're done. Try opening the terminal again, go to your Laravel project folder and php artisan migrate.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download