簡體   English   中英

在docker中運行MySQL時出現“訪問被拒絕”錯誤

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

我目前正在嘗試學習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      -

非常感謝您的幫助,因為我不知道接下來要看什么並且整日都在戰斗。

顯然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> 

希望這可以在將來對其他人有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM