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

Now, start MySQL again, which will now run without enforcing password restrictions.

sudo systemctl start mysql

Oct 03, 2024
Read More
Tutorial
mysql

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

Example:

SELECT * FROM your_table ORDER BY some_column LIMIT 1000;

Aug 26, 2024
Read More
Cheatsheet
json

JSON Operations in MySQL: Examples and Use Cases

SELECT preferences->'$.theme' AS theme
FROM users
WHERE name = 'John Doe';
  • Retrieve a scalar value from a JSON object:

Aug 21, 2024
Read More
Tutorial
mysql

Understanding and Using MySQL Indexes

SHOW INDEX FROM table_name;

For example, to view indexes on the users table:

Aug 12, 2024
Read More
Tutorial
mysql

How to Monitor MySQL Database Performance

MySQL Workbench provides a graphical interface for monitoring database performance. It includes tools for visualizing server status and performance metrics.

The Performance Dashboard includes reports on:

Aug 12, 2024
Read More
Tutorial
mysql

How to Optimize MySQL Queries for Better Performance

SELECT first_name, last_name FROM users LIMIT 10;

Query caching stores the results of a query, reducing execution time for repeated queries with the same parameters.

Aug 12, 2024
Read More
Tutorial
mysql

Managing Transactions and Concurrency in MySQL

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

Concurrency control is essential in multi-user databases to prevent conflicts and ensure data integrity. MySQL uses locks and other mechanisms to manage concurrency.

Aug 12, 2024
Read More
Tutorial
mysql

Viewing the Database Size and Identifying the Largest Table in MySQL

SELECT table_name AS "Table",
       ROUND((data_length + index_length) / 1024 / 1024, 2) AS "Size (MB)"
FROM information_schema.tables
WHERE table_schema = 'your_database_name'
ORDER BY (data_length + index_length) DESC
LIMIT 1;
  • table_name: The name of each table in the specified database.
  • ORDER BY (data_length + index_length) DESC: Orders the tables by size in descending order, so the largest appears first.
  • LIMIT 1: Limits the result to only the largest table.

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

  • Create a New Middleware
  php artisan make:middleware MiddlewareName

Aug 03, 2024
Read More
Tutorial
sql

Optimizing SQL Queries: Indexing and Query Optimization Techniques

SELECT e.name, d.department_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id;
-- Avoid
SELECT * FROM employees WHERE UPPER(name) = 'ALICE';
-- Prefer
SELECT * FROM employees WHERE name = 'Alice';

Aug 03, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!