MySQL: Access denied for user 'root'@'localhost'
Fix error 1045. Default passwords for XAMPP and MAMP, and how to log in on Linux where root uses socket login.
MySQL error 1045 means the username or password was rejected. The right login depends on your setup, so check the defaults below first.
Default logins
- XAMPP / WAMP: user root, empty password
- MAMP: user root, password root
- Linux MySQL 8: root often logs in with
sudo mysql, not a password
Create your own user
sudo mysqlCREATE USER 'app'@'localhost' IDENTIFIED BY 'Change-me-123';
GRANT ALL PRIVILEGES ON appdb.* TO 'app'@'localhost';
FLUSH PRIVILEGES;More things to check
- Your app still gets access denied
- The username and password in your app’s config must match exactly. Check the .env or wp-config.php file.
- phpMyAdmin cannot log in
- If you changed the root password, update config.inc.php in the phpMyAdmin folder.
- Forgot the root password
- Stop MySQL, start it with
--skip-grant-tables, log in, set a new password, then restart normally. Only do this on your own computer. - Use a separate user
- Create one user per project instead of using root everywhere. See the .env generator.
Questions about MySQL access denied
- What is the default MySQL root password in XAMPP?
- It is empty. The user is root with no password.
- How do I fix MySQL error 1045?
- Use the correct password for your setup. On Linux, log in with sudo mysql and create a normal user for your project.
Config generators for this
- docker-compose for MySQLRun a local MySQL database in Docker with your own user, password and port, and keep the data between restarts.
- docker-compose for WordPressRun WordPress and its database locally in Docker with one command and keep your site between restarts.
- .env file exampleCreate a .env file for your app: port, database address and a secret key.
- Docker Compose for MySQL and phpMyAdminRun MySQL and the phpMyAdmin web page on your computer with one docker compose command.
- MySQL my.cnf settingsA small MySQL or MariaDB override file for connections, memory, character set and the slow query log.