Search code examples
mysqllaravelterminalmigrationdatabase-migration

cannot migrate in new Laravel project


So, I am trying to create a laravel project from scratch.
I ran the following:

composer global require "laravel/installer=~1.1" //this code was not necessary
composer create-project laravel/laravel my-project-name
php artisan serve

Now I got the new laravel project ready to go, and tried to run:

php artisan migrate

but it returned an error:

SQLSTATE[HY000] [2002] Connection refused

I looked around other answers to similiar questions, but none of them solved the issue.
I also tried creating a new mysql user, but it returned another error:

SQLSTATE[HY000] [1698] Access denied

My current env:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=user
DB_PASSWORD=password

Solution

  • This problem was due to permission issues.
    I could not migrate using root because Ubuntu needs sudo now, so I created a new user following the steps in option 2 of the first answer here.

    One thing I had to change was that I had to run

    UPDATE user SET plugin='mysql_native_password' WHERE User='YOUR_SYSTEM_USER';
    

    instead of

    UPDATE user SET plugin='auth_socket' WHERE User='YOUR_SYSTEM_USER';
    

    Putting it all together it would be:

    sudo mysql -u root
    
    mysql> USE mysql;
    mysql> CREATE USER 'YOUR_SYSTEM_USER'@'localhost' IDENTIFIED BY 'YOUR_PASSWD';
    mysql> GRANT ALL PRIVILEGES ON *.* TO 'YOUR_SYSTEM_USER'@'localhost';
    mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='YOUR_SYSTEM_USER';
    mysql> FLUSH PRIVILEGES;
    mysql> exit;
    
    sudo service mysql restart