Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bellpep's avatar

MySQL not working on WSL or MacOS

So I'm following the Laravel 8 From Scratch series and have been stuck on this episode for about a week now because MySQL just will not work.

I started working with Laravel using WSL on Windows 10 but couldn't get MySQL to work as you can see in my previous thread.

I've switched to my Mac laptop (macOS Big Sur Version 11.6.2) because I wasn't having any luck with anything I found online/anything people suggested in my previous thread.

Even though I switched to a different OS I'm having a very similar issue. Here's my process:

brew install mysql
brew services start mysql
mysql -uroot -p

after running the last command I get this error:

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

I've looked through so many threads on so many different websites and nothing has worked. Is MySQL just a bad choice for Laravel and Mac/Linux operating systems? I really don't know where to go from here. This roadblock has lasted a week and I'm starting to feel like I'll never actually get MySQL to work properly.

In the episode I linked Jeffrey just simply installs MySQL and types mysql -uroot -p and it immediately works. Why is it so simple in the video yet my experience has been incredibly difficult?

If anyone has any insight on this I'd appreciate it a lot.

0 likes
13 replies
Tray2's avatar

I never had any issues installing MySQL on any operating system, so this sounds a bit strange to me.

Have you made sure that MySQL is actually up and running? It's normally not started automatically after installing it with brew.

You can check that with ps -ax | grep mysql

You should get an answer looking something like this

 ✝  ~/code/mb4   master±  ps -ax | grep mysql
 1124 ??         0:00.03 /bin/sh /usr/local/opt/mariadb/bin/mysqld_safe --datadir=/usr/local/var/mysql
 1645 ??         7:19.55 /usr/local/opt/mariadb/bin/mariadbd --basedir=/usr/local/opt/mariadb --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/opt/mariadb/lib/plugin --log-error=/usr/local/var/mysql/Patriks-iMac.err --pid-file=Patriks-iMac.pid
16939 ttys002    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox mysql

If you don't get an answer similar, try starting mysql with the following command.

brew services start mysql

And try again.

If it still doesn't work, this SO thread should be able help you.

https://stackoverflow.com/questions/15450091/error-2002-hy000-cant-connect-to-local-mysql-server-through-socket-tmp-mys

1 like
bellpep's avatar

@Tray2 So I run brew services start mysql and then ps -ax | grep mysql and this is what I get:

88933 ttys000    0:00.00 grep mysql

That's actually one of the stackoverflow posts I've gone through before. I've tried most of the top answers on there and no luck. For example with the first one when I get to the part where I have to run mysql_secure_installation I get the following error:

Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (38)

Every other solution I've tried from that thread leads to the same error.

Tray2's avatar

@bellpep This means that you haven't got mysql running

88933 ttys000    0:00.00 grep mysql

Did you get any error message when you ran brew services start mysql? Something like this maybe.

Error: No available formula with the name "mysql".?
1 like
sr57's avatar

@bellpep

Seems you have many difficulties with st that should be normally easy, using WSL2 adds a layer of complexity, I suggest you to invest some time to install a real Linux system , for instance in dual boot on your laptop. Doing this you'll get a standard way of doing things for servers and many people (here) will be able to help you.

1 like
bellpep's avatar

@sr57 The same issue is also happening on my mac laptop. Does macOS also add a layer of complexity?

richardhulbert's avatar

Another possibility is installing docker desktop and running the mariaDB image.

https://www.docker.com/products/docker-desktop/

in the terminal:

mkdir ~/maria_db_files

docker run -p 127.0.0.1:32771:3306 -v ~/maria_db_files:/var/lib/mysql --name mariadb -e MARIADB_ROOT_PASSWORD=root -d mariadb:10.3

This will download the image, make a container and run it.

The edit your .env file to:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=32771
DB_DATABASE=laravel_db
DB_USERNAME=root
DB_PASSWORD=root

make your database called laravel_db (or whatever) you can do this using any database editor. (you will need to connect to the server with the above creds)

and you are good to go. the advantage of having docker is that you can do the same thing with other images such as redis or other databases. Also as of Laravel 9 you can use the sail up command that basically runs your local laravel app in a docker container

1 like
bellpep's avatar

@richardhulbert Thank you for the response, I'll definitely try this. Do you have any database editors you recommend for macOS?

alexeight6's avatar

You might want to try using Laravel Sails. It will automatically setup your Docker environment for you including the MySQL/Database container. It's typically better to avoid installing the MySQL server on your host machine and rather inside an isolated environment like Docker or a VM. If you have the telnet package installed on your OS you can see if any service is running, including mysql by using this command:

telnet localhost 3306 ...

should yield something like this:

Trying 127.0.0.1...

Connected to localhost. Escape character is '^]'. 5.5.5-10.8.3-MariaDB-1:10.8.3+maria~jammy

bellpep's avatar

@alexeight6 You're referring to this correct? This is actually what I used to setup my Laravel project. Could this be the issue? Using Laravel Sail + trying to install MySQL through the terminal?

If I create a fresh project only using Laravel Sail, how would I go about connecting to the MySQL database? Would I be able to connect through the terminal?

alexeight6's avatar

@bellpep If you're using Laravel sails to run your project then the issue most likely is you're trying to connect to the wrong port. Or you're connecting from the wrong context. I've never used Sails before but I would assume a non standard port is getting forwarded to your host machine and not the typical port of 3306. Regardless you don't need to use your own MySQL client that you installed on your host machine to connect to MySQL you can do it from the docker container.

You can type "docker container ls" to see if the MySQL container is running. From there you can also see what port is getting forwarded to your host machine.

Assuming the docker container is running you can type something like:

docker container exec -it (docker container name) mysql -u(mysql username) -p(mysql password)

Edit: according to ... 9.x/sail#mysql they do forward 3306, but maybe that port is already in use and another port is getting used... I don't know.

Please or to participate in this conversation.