X
    Categories: Laravel

SQLSTATE[HY000] [2002] Can’t connect to local MySQL server through socket Laravel

SQLSTATE[HY000] [2002] Can’t connect to local MySQL server socket – Laravel

I am using Laravel5.2 Homestead with vagrant and virtualbox.

When I run php artisan migrate I got the following Error SQLSTATE[HY000] [2002] Can’t connect to local MySQL server through socket ‘/Applications/MAMP/tmp/mysql/mysql.sock’ (2). Because of this error I have struggled long time, After many research the following
solution solved my problem.

Inside my App directory I have run the following Command

vagrant destroy && vagrant up

It will destroy the current vagrant setup and will installed it newly.

After you enter the above command :

Are you sure you want to destroy the ‘default’ VM? [y/N] :  y // Enter y -> It will instralled the vagrant set up

It will execute the following lines

default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...
==> default: Removing hosts
Password:
==> default: Running cleanup tasks for 'shell' provisioner...
==> default: Running cleanup tasks for 'shell' provisioner...
==> default: Running cleanup tasks for 'shell' provisioner...
==> default: Running cleanup tasks for 'shell' provisioner...
==> default: Running cleanup tasks for 'shell' provisioner...
==> default: Running cleanup tasks for 'shell' provisioner...
==> default: Running cleanup tasks for 'shell' provisioner...
==> default: Running cleanup tasks for 'shell' provisioner...
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'laravel/homestead'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'laravel/homestead' is up to date...
==> default: A newer version of the box 'laravel/homestead' is available! You currently
==> default: have version '0.4.4'. The latest is version '0.5.0'. Run
==> default: `vagrant box update` to update.
==> default: Setting the name of the VM: firstapp
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 80 => 8000 (adapter 1)
    default: 443 => 44300 (adapter 1)
    default: 3306 => 33060 (adapter 1)
    default: 5432 => 54320 (adapter 1)
    default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if its present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default: 
    default: Guest Additions Version: 5.0.12
    default: VirtualBox Version: 4.2
==> default: Checking for host entries
==> default: adding to (/etc/hosts) : 192.168.10.10  firstapp  # VAGRANT: 4c4f5c76bba0366e03cac20c2b4b41cd (default) / 8a064c05-feea-454f-9081-0464face6f88
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /Users/marimuthu-mac/firstApp
    default: /home/vagrant/firstapp => /Users/marimuthu-mac/firstApp
==> default: Running provisioner: shell...
    default: Running: inline script
==> default: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCX06LOVxeRgLgA+mTrWn52aGl02JwEVTrU8r7Lm16pJ5avt1od1TZq1Vw+59NJ8PGaZIe7CkOccezHbJjovJEih2sKqvsFyLyPlkzGwGjyARTFh346ibvtflhVD+VVIPUh+L9VXNg+woeC+0//490WPsgPUmvmkiYtsT61B9goh877Qwxd+O0X0ZhIOB99LCn8SxFL8F73BXoEhV8kRrU4deRESSqUL3q7z+9PYxpbWWAGA67ajLYdCZarjk300e9HSJDOBRjTJwB8Fpo7lhmZooWl1mQCc9napWIlNcKnJCOJkluhotBtLfYaOqZCsaDdUlf+9IrAxzPQQ7OC5UZb marimuthu-mac@138.143.204.49-ras.beamtele.net
==> default: Running provisioner: shell...
    default: Running: inline script
==> default: Running provisioner: shell...
    default: Running: /var/folders/vy/gt86b9j557zgmj7fjhdkm7rw0000gn/T/vagrant-shell20160804-2539-ij4y6k.sh
==> default: Running provisioner: shell...
    default: Running: /var/folders/vy/gt86b9j557zgmj7fjhdkm7rw0000gn/T/vagrant-shell20160804-2539-s66owc.sh
==> default: nginx stop/waiting
==> default: nginx start/running, process 2292
==> default: php7.0-fpm stop/waiting
==> default: php7.0-fpm start/running, process 2310
==> default: Running provisioner: shell...
    default: Running: /var/folders/vy/gt86b9j557zgmj7fjhdkm7rw0000gn/T/vagrant-shell20160804-2539-19ml1ys.sh
==> default: mysql: 
==> default: [Warning] Using a password on the command line interface can be insecure.
==> default: Running provisioner: shell...
    default: Running: /var/folders/vy/gt86b9j557zgmj7fjhdkm7rw0000gn/T/vagrant-shell20160804-2539-rttobj.sh
==> default: Running provisioner: shell...
    default: Running: /var/folders/vy/gt86b9j557zgmj7fjhdkm7rw0000gn/T/vagrant-shell20160804-2539-1dhx3ie.sh
==> default: Running provisioner: shell...
    default: Running: inline script
==> default: You are running composer with xdebug enabled. This has a major impact on runtime performance. See https://getcomposer.org/xdebug
==> default: Updating to version 1.2.0.
==> default:     Downloading: Connecting...
==> default: 
==> default:     Downloading: 100%
==> default:          
==> default: 
==> default: Use composer self-update --rollback to return to version 1.0.0

Once you reinstalled the vagrant


Go inside the vagrant box by entering below command :

vagrant ssh
cd /vagrant

Example :

broadband:firstApp marimuthu-mac$ vagrant ssh
Welcome to Ubuntu 14.04.4 LTS (GNU/Linux 3.19.0-25-generic x86_64)

* Documentation: https://help.ubuntu.com/
vagrant@firstapp:~$ cd /vagrant

It will take you to the vagrant box.

Now run you migration command

php artisan migrate

Example :

vagrant@firstapp:/vagrant$ php artisan migrate
Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table

It will migrate your tables successfully.

Hope this will help someone.
If you like this article please don’t forget to share and comment.
Thanks!.
Happy coding…

 

Marimuthu: