简体   繁体   中英

Getting “Access Denied” error in MySQL when running it in docker

I am currently trying to learn docker and am trying to connect to a MySQL instance in a container from the host running the docker engine. I am currently experiencing the following error after my containers have been deployed:

ahopkins@Ubuntu:~$ mysql -uadmin -ppassword --protocol=tcp
mysql: [Warning] Using a password on the command line interface can be 
insecure.
ERROR 1045 (28000): Access denied for user 'admin'@'172.17.0.1' (using 
password: YES)

Here's my setup:

ahopkins@Ubuntu:~$ docker version
Client:
 Version:      18.05.0-ce
 API version:  1.37
 Go version:   go1.9.5
 Git commit:   f150324
 Built:        Wed May  9 22:16:13 2018
 OS/Arch:      linux/amd64
 Experimental: false
 Orchestrator: swarm

Server:
 Engine:
 Version:      18.05.0-ce
 API version:  1.37 (minimum version 1.12)
 Go version:   go1.9.5
 Git commit:   f150324
 Built:        Wed May  9 22:14:23 2018
 OS/Arch:      linux/amd64
 Experimental: false

ahopkins@Ubuntu:~$ docker images mysql
REPOSITORY          TAG                 IMAGE ID            CREATED SIZE
mysql               latest              a8a59477268d        6 weeks ago         445MB

Current latest tag from Docker Hub is 8.0.11:

ahopkins@Ubuntu:~$ docker pull mysql:8.0.11
8.0.11: Pulling from library/mysql
Digest:

sha256:d60c13a2bfdbbeb9cf1c84fd3cb0a1577b2bbaeec11e44bf345f4da90586e9e1
Status: Downloaded newer image for mysql:8.0.11

I am using the following Linux version and kernel versions:

ahopkins@Ubuntu:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04 LTS
Release:    18.04
Codename:   bionic
ahopkins@Ubuntu:~$ uname -a
Linux Ubuntu 4.15.0-23-generic #25-Ubuntu SMP Wed May 23 18:02:16 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

I know for a fact that my password is correct. I have verified that as I can log into the database from the container running MySQL without an issue.

This is exactly what I did to get to the point I am at:

ahopkins@Ubuntu:~$ docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
f2aa67a397c4: Pull complete 
1accf44cb7e0: Pull complete 
2d830ea9fa68: Pull complete 
740584693b89: Pull complete 
4d620357ec48: Pull complete 
ac3b7158d73d: Pull complete 
a48d784ee503: Pull complete 
f122eadb2640: Pull complete 
3df40c552a96: Pull complete 
da7d77a8ed28: Pull complete 
f03c5af3b206: Pull complete 
54dd1949fa0f: Pull complete 
Digest: sha256:d60c13a2bfdbbeb9cf1c84fd3cb0a1577b2bbaeec11e44bf345f4da90586e9e1
Status: Downloaded newer image for mysql:latest

ahopkins@Ubuntu:~$ docker run -d --name mysql-01 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=testing mysql
c21bf959fd33406213f4f5718f5a9664b86d5cbeb773c66b4f2ec7cf7812c300

ahopkins@Ubuntu:~$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
c21bf959fd33        mysql               "docker-entrypoint.s…"   30 seconds ago      Up 28 seconds       0.0.0.0:3306->3306/tcp   mysql-01


ahopkins@Ubuntu:~$ docker exec -it mysql-01 bash
root@c21bf959fd33:/# mysql -uroot -ppassword
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.11 MySQL Community Server - GPL

Copyright (c) 2000, 2018, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| testing            |
+--------------------+
5 rows in set (0.00 sec)

So far things look exactly like I would expect. All looks good. now I proceed with doing some database work:

mysql> CREATE USER 'admin'@'172.17.0.1' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.04 sec)

mysql> ALTER USER 'admin'@'172.17.0.1' IDENTIFIED WITH mysql_native_password;
Query OK, 0 rows affected (0.04 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'172.17.0.1';
Query OK, 0 rows affected (0.03 sec)

mysql> SHOW GRANTS FOR 'admin'@'172.17.0.1';
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for admin@172.17.0.1                                                                                                                                                                                                                                                                                                                                                              |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, CREATE ROLE, DROP ROLE ON *.* TO `admin`@`172.17.0.1` |
| GRANT BACKUP_ADMIN,BINLOG_ADMIN,CONNECTION_ADMIN,ENCRYPTION_KEY_ADMIN,GROUP_REPLICATION_ADMIN,PERSIST_RO_VARIABLES_ADMIN,REPLICATION_SLAVE_ADMIN,RESOURCE_GROUP_ADMIN,RESOURCE_GROUP_USER,ROLE_ADMIN,SET_USER_ID,SYSTEM_VARIABLES_ADMIN,XA_RECOVER_ADMIN ON *.* TO `admin`@`172.17.0.1`                                                                                                  |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

Now, when I try to connect to the MySQL server from the host, I get this:

ahopkins@Ubuntu:~$ mysql -uadmin -ppassword --protocol=tcp
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'admin'@'172.17.0.1' (using password: YES)

I can see the port open using netstat:

hopkins@Ubuntu:~$ netstat -anlp | grep 3306
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp6       0      0 :::3306                 :::*                    LISTEN      -

Any help is greatly appreciated, as I have no clue what to look at next and have been fighting this all day.

Apparently there was an issue with the MySQL port (3306), as I was pretty sure there's nothign else using it, but when I changed to port 3308, it worked:

ahopkins@Ubuntu:~$ mysql -P 3308 -uroot -p --protocol=tcp
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.11

Copyright (c) 2000, 2018, 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> 

Hoping this can help somebody else in the future.

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