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

bellpep's avatar

Access Denied when trying to connect to MySQL through Ubuntu terminal

So I've been having issues getting MySQL to work on WSL 2 for days now and I cannot figure it out. I've searched through tons and tons of stackoverflow/laracast forum posts but nothing has worked so far.

I posted about this once before and thought I found a solution here but when I get to step 4 and enter grant all privileges on *.* to root@localhost identified by 'YourNewPassword' with grant option; I get this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by 'password' with grant option' at line 1

I'm using

  • Windows 10
  • WSL 2
  • Ubuntu 20.04.4
  • Laravel Version 9.17.0
  • MySQL Version 8.0.29

Here's my process:

I create a fresh Laravel project and start it up

curl -s https://laravel.build/example-app | bash
cd example-app
./vendor/bin/sail up

I install and start MySQL

sudo apt-get install mysql-server
sudo service mysql start

But when I try to connect to MySQL

sudo mysql -uroot -p
Enter password:

I get the following error:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Here's my .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=exampleapp
DB_USERNAME=root
DB_PASSWORD=password

I've also tried connecting to MySQL through phpMyAdmin but I get a Permission Denied error. I've been stuck on this for days and really have no idea what to do. If anyone has insight on this problem I'd appreciate it a lot. If there's any additional information I can provide please let me know and I'll respond with it ASAP.

0 likes
9 replies
sr57's avatar

Hi @bellpep

Did you setup the mysql root password?

On a server you should have the script "mysql_secure_installation" that you can manually execute to do the job.

bellpep's avatar

@sr57 So when I try running mysql_secure_installation I get this error:

Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Also often times when I come back to this issue the next day and I try running sudo service mysql start I get this after a few seconds:

 * Starting MySQL database server mysqld         [fail]

I also can't get into the mysql log directory /var/log/mysql, I just get a permission denied error so I'm unable to provide the contents of the log.

sr57's avatar

@bellpep

Seems you have pb with you mysql server, this should be the very first point to correct.

2 preliminary points

-1- service is the old command, and now should be an alias of the new one : systemctl - use this one.

-2- with root access (sudo) you should have access to your logs.

That said

-1- share the return of this cmd "systemctl status mysql"

if not active (that seems to be)

-2- share the return of this cmd "systemctl start mysql"

if fails (what's you share), maybe the log will show a quick solution

But, if, as I understood, you have no data (db) today, the best way should be to reinstall mysql

bellpep's avatar

@sr57 When I try running sudo systemctl start mysql I get this error:

System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

Sometimes when I do a fresh install of MySQL the server fails to start, but other times the server starts but then fails to stop. Here's a screenshot

pierreLarssonKhalifa's avatar

Hi how are you? uh I also had the same error several times with my SQL server so I had to delete the pack to reconfigure it. annoying thing because every time I'm faced with this! so to no longer have to do with it I had to install xampp ubuntu! but if you still want to solve the problem take a look at this link it explains how to communicate laravel and SQL (and sorry if my English is rusty I'm not English ^-^)

5balloons.info/install-laravel-ubuntu-php-mysql-composer/

bellpep's avatar

@pierreLarssonKhalifa When I get to the step where I have to run sudo mysql_secure_installation it asks me to enter the password for user root. I enter "password" and then get this error:

The user provided does not have enough permissions to continue.
mysql_secure_installation is exiting.

Also if I skip that step and do sudo mysql -u root -p and then ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; I get this error:

ERROR 1227 (42000): Access denied; you need (at least one of) the CREATE USER privilege(s) for this operation
pierreLarssonKhalifa's avatar

@bellpep before doing sudo mysql -u root -p, did you do SHOW GRANTS; ? normally once this request is done you had to be an anonymous user assigned to authenticate yourself in mysql. Just after that run this command mysql -uroot. By default, root@localhost has all rights and no password. I think that with that you can create users and databases with their privileges

sr57's avatar

@bellpep

System has not been booted with systemd as init

Didn't expect this with Ubuntu 20 ! Maybe due to the use of WSL2, I don't use it personally. So forget my advice with "systemctl" and use always 'service'

The user provided does not have enough permissions

Have you try to reinstall mysql ?

You can use this link to reset root privilege (hoping you don't have other change(s) in your setup)

https://stackoverflow.com/questions/1709078/how-can-i-restore-the-mysql-root-user-s-full-privileges

Please or to participate in this conversation.