[英]Is there any way to connect docker mysql from host?
I created mysql docker container我创建了 mysql docker 容器
docker run -p 13306:3306 -d -e MYSQL_ROOT_PASSWORD="pass" -e MYSQL_DATABASE="db" --name mysql mysql:5.6.46
and I tried to connect to mysql我尝试连接到 mysql
mysql -u root -p -h localhost -P 13306
but I can't connect to mysql.但我无法连接到 mysql。
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
I must connect like this for some reason.出于某种原因,我必须像这样连接。 not use
docker exec -i -t mysql bash
不使用
docker exec -i -t mysql bash
you should try and expand your question a bit and add some more information about the errors you receive.您应该尝试稍微扩展一下您的问题,并添加一些有关您收到的错误的更多信息。
In my local development environment in order to connect to my MySQL database I give it a static ip address.在我的本地开发环境中,为了连接到我的 MySQL 数据库,我给了它一个静态 IP 地址。 I tend to use docker-compose and not run it from the docker command directly for simplicity.
为简单起见,我倾向于使用 docker-compose 而不是直接从 docker 命令运行它。 This is what my docker-compose.yaml file looks like for a mariaDB container :
这是我的 docker-compose.yaml 文件对于 mariaDB 容器的样子:
version: '3.7'
services:
maria:
container_name: maria
image: mariadb:10.4
restart: always
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_USER=user
- MYSQL_PASSWORD=user_password
volumes:
- ./maria:/var/lib/mysql
networks:
static:
ipv4_address: 172.30.0.10
networks:
static:
ipam:
driver: default
config:
- subnet: 172.30.0.0/16
After running the docker-compose up command I can now connect to the mysql shell with the command运行docker-compose up命令后,我现在可以使用命令连接到 mysql shell
mysql -uuser -puser_password -h 172.30.0.10
if you are running linux you can also add a line to your /etc/hosts file like : 172.30.0.10 mysql如果您正在运行 linux,您还可以在 /etc/hosts 文件中添加一行,例如:172.30.0.10 mysql
you can then connect with the command然后您可以使用命令连接
mysql -uuser -puser_password -h mysql
I'm pretty confident you can get the same results with a pure docker command but it just seems easier with docker-compose.我非常有信心使用纯 docker 命令可以获得相同的结果,但使用 docker-compose 似乎更容易。 Anyway I hope ths helps
无论如何,我希望这会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.