Connecting to mysql from 127.0.0.1 instead of from localhost

6

1

I have similar problem as here. The problem is that solution proposed there does not work for me:

Just for now I have users:

+----------+------------------------------------+
| user     | host                               |
+----------+------------------------------------+
| root     | 127.0.0.1                          |
| root     | localhost                          |

both without passwords (just for now. I know it is bad!).

I'm connecting to mysql using this command:

mysql -u root -h 127.0.0.1 --protocol=tcp

After I logged in, I get:

mysql -u root -h 127.0.0.1 --protocol=tcp
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.61-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> \s
--------------
mysql  Ver 14.14 Distrib 5.1.61, for redhat-linux-gnu (x86_64) using readline 5.1

Connection id:      5
Current database:   
Current user:       root@localhost
SSL:            Not in use
Current pager:      stdout
Using outfile:      ''
Using delimiter:    ;
Server version:     5.1.61-log Source distribution
Protocol version:   10
Connection:     127.0.0.1 via TCP/IP
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    latin1
Conn.  characterset:    latin1
TCP port:       3306
Uptime:         2 days 9 hours 2 min 13 sec

Threads: 1  Questions: 102  Slow queries: 0  Opens: 19  Flush tables: 1  Open tables: 12  Queries per second avg: 0.0
--------------

mysql> SELECT USER(),CURRENT_USER();
+----------------+----------------+
| USER()         | CURRENT_USER() |
+----------------+----------------+
| root@localhost | root@localhost |
+----------------+----------------+
1 row in set (0.00 sec)

mysql> 

QUESTION:

How to login as 'root'@127.0.0.1?

misiek

Posted 2014-04-23T09:52:09.053

Reputation: 61

2When it comes to sql 127.0.0.1 is NOT localhost. This is a quirk of mysql - localhost implies listening on a socket, not binding to a port. – davidgo – 2015-02-08T06:49:06.820

@mislek - You do understand localhost is 127.0.0.1 right? Its defined in hosts that way. – Ramhound – 2014-05-09T21:08:38.957

1Yes I know what you mean, but I don't think it is in 100% the same thing. Anyway it DOES make difference to MySQL with respect to permissions. – misiek – 2014-05-12T06:34:33.257

I promise you 127.0.0.1 is localhost. This is true on lamost any operating system with traditional networking support. If I were to hazard a guess if you got rid of one of the root users both @127.0.0.1 and @localhost would still work. – Ramhound – 2014-05-12T10:54:46.003

Answers

2

Good evening, it's @RolandoMySQLDBA, the author of that post you mentioned.

Look at the last three lines

Server version:     5.1.61-log Source distribution
Protocol version:   10
Connection:     127.0.0.1 via TCP/IP

The last line says 127.0.0.1 via TCP/IP so you are 100% using the TCP/IP protocol.

Did you run SELECT USER(),CURRENT_USER(); ?

If you did, it should say

root@127.0.0.1 | root@127.0.0.1

It would seem weird for the output to be

root@127.0.0.1 | root@localhost

Please notice the following: Since both root users have identical grants and identical passwords (none in this case), mysqld decided to pick root@localhost. At this point, you are probably wondering, "Why would mysqld make such a choice ?"

In the DBA StackExchange, I have this 2-year-old post (MySQL error: Access denied for user 'a'@'localhost' (using password: YES)). In that past, I describe exactly how mysqld peforms user authentication. Note the paragraphs from pages 486,487 of MySQL 5.0 Certification Study Guide

There are two stages of client access control:

In the first stage, a client attempts to connect and the server either accepts or rejects the connection. For the attempt to succeed, some entry in the user table must match the host from which the clent connects, the username, and the password.

In the second stage (which occurs only if a client has already connected sucessfully), the server checks every query it receives from the client to see whether the client has sufficient privileges to execute it.

The server matches a client against entries in the grant tables based on the host from which the client connects and the user the client provides. However, it's possible for more than one record to match:

As I mentioned from the Certification Book, it's possible for more than one record to match. Therefore, mysqld made the choice.

If you delete root@localhost from mysql.user, that's one way to get SELECT USER(),CURRENT_USER(); to match. Another way is to give root@127.0.0.1 a password.

SUMMARY

Having 2 root users with identical means of being used for user authentication is the root cause as to why you cannot see root@127.0.0.1. Notwithstanding, you are using TCP/IP.

SIDE NOTE

Please make sure 127.0.0.1 is defined in the OS. Run this:

cat /etc/hosts | grep -c "127\.0\.0\.1"

If you get 0, then the OS does not know about it. You would need to add 127.0.0.1 to /etc/hosts and restart the network and then mysql.

UPDATE 2014-04-26 20:00 EDT

What I am about to say may sound silly, but the Documentation uses TCP instead of tcp

On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option. For example:

shell> mysql --host=127.0.0.1
shell> mysql --protocol=TCP

The --protocol option enables you to establish a particular type of connection even when the other options would normally default to some other protocol.

Try using protocol=TCP like this

mysql -u root -h 127.0.0.1 --protocol=TCP -ANe"SELECT USER(),CURRENT_USER()"
mysql -u root -h localhost --protocol=TCP -ANe"SELECT USER(),CURRENT_USER()"

to see if it makes a difference.

UPDATE 2014-05-09 16:19

You need to look over the /etc/hosts carefully

If you see this in /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

then logging in as root@127.0.0.1 should work properly

If you see this in /etc/hosts

127.0.0.1       localhost

then root@127.0.0.1 stands no chance of working

RolandoMySQLDBA

Posted 2014-04-23T09:52:09.053

Reputation: 2 675

Are you saying that changing /etc/hosts in the way you prescribe is the ticket? – Matt Cremeens – 2016-08-21T18:38:58.490

1So there is no solution? mysql client will think you are foo@localhost even when using mysql -u foo -h 127.0.0.1? – CDuv – 2017-03-02T15:18:22.120

Hi, I'm happy that YOU noticed my question :). Well SELECT USER(),CURRENT_USER(); does not return any of the two results that you suggested - it says @'localhost' for both! (see updated session transcript). The hosts check returns 1. Maybe my mysql client is so smart that it translates 127.0.0.1 to localhost and connects to the server like this? – misiek – 2014-04-26T20:30:43.987

-1

PHP is still trying to use the default socket location. This problem can appear if you have moved the MariaDB/MySQL folder from /var/lib/mysql to another location. In order to solve the problem you have to define the new socket's location in the /etc/php.ini file.

mysqli.default_socket =/newDBLocation/mysql/mysql.sock

Watch out, depending on which driver you use, you may have to specify the pdo_mysql.default_socket=!

In order to check your current directory run the following command in mysql:

select @@datadir;

joelschmid

Posted 2014-04-23T09:52:09.053

Reputation: 239

This isn’t about PHP. It’s about the MySQL command line client. Not only that, it isn’t about moving the MySQL socket either. Got the wrong question perhaps? – Daniel B – 2017-02-17T10:34:23.843

hey Daniel, when I was searching for this error I got to this post, therefore I posted my solution, so other ppl with the same problem find the solution faster than I did... – joelschmid – 2017-02-19T09:04:17.077

That's just not how Super User works. Please follow the rules. – Daniel B – 2017-02-19T09:46:31.510