gpt4 book ai didi

mysql - 如何让 PHPMyAdmin 在两个不同的 docker 实例中查看 MySQL

转载 作者:行者123 更新时间:2023-11-29 09:33:28 30 4
gpt4 key购买 nike

我正在尝试启动 MySQL docker 实例,并希望使用 PHPMyAdmin 连接到该服务器。

运行 docker 的服务器主机名是 <ServerName>

我正在使用以下命令启动我的 MySQL docker 容器

docker run -P --name mysql-test -v storage-test:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=MyDataBase -e MYSQL_USER=me -e MYSQL_PASSWORD=mepass -d mysql:latest

它似乎正确启动并创建了数据库之后我启动 PHPmyAdmin 容器

docker run --name myadmin -d -e MYSQL_ROOT_PASSWORD=root -e PMA_HOST=ServerName -e PMA_VERBOSE=MyDataBase -e PMA_USER=me -e PMA_PASSWORD=mepass  -p 8080:80 phpmyadmin/phpmyadmin

我从 mySql 容器获取此日志

MySQL init process done. Ready for start up.

2019-10-01T11:36:35.909758Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2019-10-01T11:36:35.909856Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.17) starting as process 1
2019-10-01T11:36:37.735204Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-10-01T11:36:37.761004Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2019-10-01T11:36:37.776949Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.17' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
2019-10-01T11:36:37.887863Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060

但是,当我访问 PHPMyAdmin 博客时,出现以下错误:

  • MySQL 说:文档无法连接:设置无效。
  • mysqli_real_connect(): (HY000/2002): 连接被拒绝
  • phpMyAdmin 尝试连接到 MySQL 服务器,并且服务器拒绝连接。您应该检查主机、用户名和密码在您的配置中,并确保它们与管理员提供的信息相对应MySQL 服务器。

如果我从运行命令中删除 -e PMA_USER=me -e PMA_PASSWORD=mepass ,我将进入登录名,在那里我可以输入我的凭据,但是当尝试这样做时,我只会得到

  • mysqli_real_connect(): (HY000/2002): 连接被拒绝

任何人都可以看到我在这里做错了什么吗?

这是我正在使用的两个 docker 容器的链接

https://hub.docker.com/_/mysql

https://hub.docker.com/r/phpmyadmin/phpmyadmin/

问候

最佳答案

First of all, don't use -P when running your mysql container. -P (or --publish-all) will publish the ports of the MySQL container, which means MySQL will be accessible publicly. This is likely not what you want.

容器可以使用 docker 容器-容器网络相互连接;如果两个容器都连接到同一网络,则它们可以通过该网络相互连接,而无需公开端口。仅使用 -p(或 -P)来使应公开的端口可访问。

例如;

1。创建自定义网络;

docker network create myprivatenetwork

2。启动mysql容器

启动 mysql 容器,并将其连接到 myprivatenetwork。我删除了 -P 选项,这意味着该容器不可公开访问,但可以从其连接的网络进行访问。 (我将命令包装起来以使其更易于阅读)

docker run \
--name mysql-test \
--network myprivatenetwork \
-v storage-test:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_DATABASE=MyDataBase \
-e MYSQL_USER=me \
-e MYSQL_PASSWORD=mepass \
-d \
mysql:5.7

Note: I used mysql:5.7, because mysql:latest (which currently is MySQL 8) requires some additional configuration; see PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client In general, it's recommended to be specific about the version you want to run, and don't run :latest, as it may change to newer versions, which can cause your setup to fail.

3。启动 PhpMyAdmin 容器

启动PhpMyAdmin容器,并将其连接到同一网络; phpmyadmin 可以使用其名称(mysql-test)作为主机名连接 mysql 容器,并使用默认值MySQL 端口(3306)。对于 phpmyadmin 容器,我保留了 -p 以允许公开访问它(尽管您可能希望让它使用 TLS/SSL 运行)

docker run \
--name myadmin \
--network myprivatenetwork \
-d \
-e MYSQL_ROOT_PASSWORD=root \
-e PMA_HOST=mysql-test \
-e PMA_PORT=3306 \
-e PMA_VERBOSE=mysql-test \
-e PMA_USER=me \
-e PMA_PASSWORD=mepass \
-p 8080:80 \
phpmyadmin/phpmyadmin

4。访问http://localhost:8080在您的浏览器中

您现在应该能够在浏览器中访问 PhpMyAdmin,并查看 MySQL 数据库。

Note that PhpMyAdmin is not configured with a password or TLS/SSL, so if your machine is publicly accessible (internet, or your local network), this will allow others to access your database through phpmyadmin

关于mysql - 如何让 PHPMyAdmin 在两个不同的 docker 实例中查看 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58184360/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com