简体   繁体   中英

Unable to connect MySQL using 'root'@'IP_Address' in Vagrant

I am trying to connect MySQL using Vagrant VM . I can access MySQL using root@localhost in vagrant ssh , but when I connect using root@192.168.33.10 which is the IP used for Vagrant VM, I am getting access denied error.

This is my Vagrant file settings:

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.

   config.vm.network "forwarded_port", guest: 80, host: 8080
   config.vm.network "forwarded_port", guest: 3306, host: 3306

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.

   config.vm.network "private_network", ip: "192.168.33.10"

root@192.168.33.10 is already added in mySQL, and permissions are given

mysql> select User,Host from mysql.user;
+-----------+---------------+
| User      | Host          |
+-----------+---------------+
| root      | 192.168.33.10 |
| mysql.sys | localhost     |
| root      | localhost     |
+-----------+---------------+
3 rows in set (0.00 sec)

MySQL 5.7 mysqld.cnf

I have tried bind-address to '0.0.0.0' and bind-address '192.168.33.10'.

Still error message shows:

vagrant@vagrant-ubuntu-trusty:/etc/mysql/mysql.conf.d$ mysql -u root@192.168.33.10 -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root@192.168.33.10'@'localhost' (using password: YES)

Look carefully at the error message. The error message is indicating that the username supplied to the MySQL server is:

 'user@192.168.33.10'

And the host that MySQL is seeing the connection from is:

 'localhost'

Here's how the MySQL client is interpreting these options:

 mysql -u root@192.168.33.10 -p

-u user supplies a string value, in this case, it's seeing 'root@192.168.33.10' as the username value. The @ sign and the digits and dots are just part of the username string. There's no no special meaning given to those characters, it's just a string. And MySQL server is going to look for a row in the mysql.users table that has User value that matches that string. (

To find a match, MySQL is also going to look at the value in the Host column. That is going to have to match the host the connection is seen as coming from. What that means is 'root'@'localhost' is a different user than 'root'@'192.168.33.10' .

-p prompt for password

Omitted are any command options for the protocol to use, the host to connect to, the port number, etc. MySQL default when we don't supply any of that is

-h localhost

as if that is what we had specified.


If we want to connect to the MySQL server via the TCP/IP protocol, and not using a Unix socket, we need to specify -h hostname or -h ipaddress , or specify --protocol=TCP

The hostname " localhost : has a special meaning in MySQL. That's a connection through a Unix socket, not through the loopback IP address we might expect.


For example, to connect to the MySQL server on the local machine, listening on the default port 3306, using the loopback IP address:

  mysql -h 127.0.0.1 -u root -p

Most of this information is covered pretty well in the MySQL Reference Manual.

http://dev.mysql.com/doc/refman/5.7/en/connecting.html

Also, by default, MySQL uses reverse DNS lookup to resolve IP addresses to hostnames.

If we want to use IP addresses in the Host column of the mysql.users table, we need to disable reverse DNS lookup. We do that by including this option:

skip_name_resolve

In the my.cnf configuration file.

正确的命令是

mysql -u root -h 192.168.33.10 -p

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM