Databases

Fix MySQL "Access denied for user 'root'@'localhost'"

MySQL rejects the root login. Usually it's the wrong password, or on Ubuntu, root logs in with sudo.

Fill these in once
  1. Try logging in without a password
    Terminal
    mysql -u root

    Homebrew's MySQL starts with no root password.

  2. Then set one
    Terminal
    mysql_secure_installation
  1. Log in as root through sudoNeeds sudo
    Terminal · Ubuntu / Debian
    sudo mysql

    On Ubuntu, root uses your Linux login instead of a password.

  2. Optional: give root a password for apps that need oneNeeds sudo
    Terminal · Ubuntu / Debian
    sudo mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'change-me';"
  1. Log in with the root password you set during install
    PowerShell
    mysql -u root -p
  2. Still denied? Force a network login
    PowerShell
    mysql -u root -p -h 127.0.0.1