DeveloperBreeze

When setting up a MySQL database, you may encounter an issue where the root user is only allowed to connect via Unix sockets (localhost), but not via TCP/IP (127.0.0.1). This tutorial walks you through resolving the Access denied for user 'root'@'127.0.0.1' issue by granting the proper privileges to the root user for TCP connections.

Prerequisites:

  • You must have root access to your MySQL server.
  • You need to be able to connect to MySQL via the command line using Unix socket (by default localhost).

Step 1: Log in to MySQL Using localhost

First, log in to MySQL using the root user without specifying the host (localhost works via Unix socket):

mysql -u root -p

Once prompted, enter the MySQL root password.

Step 2: Grant Privileges for 127.0.0.1

By default, MySQL treats connections via localhost (Unix socket) and 127.0.0.1 (TCP/IP) differently. To resolve the issue, you need to grant privileges to root for connections via 127.0.0.1.

Inside the MySQL shell, run the following command:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION;

This grants all privileges to the root user for connections coming from 127.0.0.1.

Step 3: Set the Root Password for 127.0.0.1

Once you’ve granted privileges, you need to set the password for the root user when connecting from 127.0.0.1.

Run the following command to set or update the root password for TCP connections:

ALTER USER 'root'@'127.0.0.1' IDENTIFIED BY 'your_new_password';

Replace 'your_new_password' with the desired password.

Step 4: Flush Privileges

To ensure all changes take effect, flush the privileges:

FLUSH PRIVILEGES;

Step 5: Verify the Connection

Now exit the MySQL shell:

exit;

Next, verify the new connection by attempting to log in using 127.0.0.1 as the host:

mysql -h 127.0.0.1 -u root -p

When prompted, enter the new password you set earlier.

Step 6: (Optional) Update Laravel or Application Configuration

If you're working with a Laravel or any other application that uses MySQL, update your .env file or configuration file to use 127.0.0.1 as the DB_HOST instead of localhost:

DB_HOST=127.0.0.1
DB_DATABASE=your_database_name
DB_USERNAME=root
DB_PASSWORD=your_new_password

Make sure the port is correct (3306 for default MySQL installations).

Why This Happens:

  • MySQL treats connections from localhost (via Unix socket) and 127.0.0.1 (via TCP/IP) as distinct connections. By default, the root user may only have privileges for localhost, causing an "Access Denied" error when trying to connect from 127.0.0.1.

Conclusion:

This tutorial explains how to grant privileges to the MySQL root user for connections over TCP/IP (127.0.0.1). By following these steps, you can resolve access denied errors and ensure smooth connections to your MySQL database for both local development and remote applications.


Continue Reading

Discover more amazing content handpicked just for you

Tutorial
bash

How to Reset the MySQL Root Password Using DROP USER

Remove the line:

Or comment it out by adding a # at the beginning:

Oct 03, 2024
Read More
Tutorial
mysql

How to Resolve the "#1038 - Out of Sort Memory" Error in MySQL

  • On Windows:

Restart the MySQL service from the Services management console.

Aug 26, 2024
Read More
Cheatsheet
json

JSON Operations in MySQL: Examples and Use Cases

SELECT name
FROM users
WHERE JSON_CONTAINS(preferences->'$.languages', '"es"');

This query returns users who have "es" (Spanish) in their languages array.

Aug 21, 2024
Read More
Tutorial
mysql

Understanding and Using MySQL Indexes

CREATE INDEX idx_last_name ON users(last_name);

Composite indexes are useful when queries involve multiple columns. Use the following syntax:

Aug 12, 2024
Read More
Tutorial
mysql

How to Monitor MySQL Database Performance

  • Server Health: CPU, memory, and disk usage.
  • Top Queries: The most time-consuming queries.
  • InnoDB Metrics: Buffer pool size, read/write operations, and more.

MySQL Enterprise Monitor is a comprehensive tool for monitoring MySQL databases. It provides real-time performance insights and alerts.

Aug 12, 2024
Read More
Tutorial
mysql

How to Optimize MySQL Queries for Better Performance

CREATE INDEX idx_user_id ON users(user_id);
  • WHERE clause: Index columns frequently used in WHERE conditions to speed up searches.
  • JOIN clause: Index columns used in joins to improve join performance.
  • ORDER BY clause: Index columns used in sorting to avoid sorting operations.

Aug 12, 2024
Read More
Tutorial
mysql

Managing Transactions and Concurrency in MySQL

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;

You can set the isolation level for the current session or globally to affect all new connections.

Aug 12, 2024
Read More
Tutorial
mysql

Viewing the Database Size and Identifying the Largest Table in MySQL

mysql -u your_username -p

Enter your password when prompted. Once logged in, you'll be able to execute SQL commands to query the database.

Aug 12, 2024
Read More
Tutorial
bash mysql

How to Allow MySQL Port and Restrict Access to Specific IPs

sudo ufw status

You should see a rule allowing traffic on port 3306.

Aug 04, 2024
Read More
Code
php bash

Laravel Artisan Commands Cheatsheet

  php artisan migrate:reset
  • Refresh All Migrations

Aug 03, 2024
Read More
Tutorial
sql

Optimizing SQL Queries: Indexing and Query Optimization Techniques

  • Query Caching: Enable if supported by your DBMS.

Combine tables to reduce joins in read-heavy systems. Use with care.

Aug 03, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!