0
votes

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:

[PDOException] SQLSTATE[HY000] [2002] Connection Refused

and

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!

1
can you show me what error occurred ?!Hiren Makwana
It's ok, I already have the solution! Thakns!!mesqueeb

1 Answers

2
votes

① 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:
    mysql_secure_installation

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 got errors, first check if your mysql server is running:

mysql.server start

Then try mysql_secure_installation and/or mysql -uroot to try and get in.

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

In case of this error try: mysql -uroot -h 127.0.0.1 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 127.0.0.1 -p

If even this fails maybe it's a permission problem. Try:

sudo chown -R _mysql:mysql /usr/local/var/mysql
sudo mysql.server start

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:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

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

④ DONE

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