在docker中运行mysql时出现“拒绝访问”错误

w41d8nur  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(796)

我目前正在尝试学习docker,并尝试从运行docker引擎的主机连接到容器中的mysql示例。部署容器后,我当前遇到以下错误:

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)

以下是我的设置:

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

docker hub的最新标签是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

我使用以下linux版本和内核版本:

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

我知道我的密码是正确的。我已经验证了这一点,因为我可以从运行mysql的容器登录到数据库而不会出现问题。
这正是我所做的,以达到我的观点:

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)

到目前为止,事情和我预期的一模一样。一切看起来都很好。现在我继续做一些数据库工作:

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)

现在,当我尝试从主机连接到mysql服务器时,我得到以下结果:

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)

我可以看到使用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      -

任何帮助都是非常感谢的,因为我不知道下一步该看什么,已经为此奋斗了一整天。

bogh5gae

bogh5gae1#

显然mysql端口(3306)有问题,因为我很确定没有其他使用它的标志,但是当我改为端口3308时,它工作了:

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>

希望这能在将来帮助别人。

相关问题