[英]connect to docker mysql from host machine
我有docker-compose(只顯示MySQL部分):
site_mysql:
image: site/mysql:latest
build: ./images/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
volumes:
- site_mysqldata:/var/lib/mysql
networks:
- site_appnet
ports:
- "3306:3306"
我可以從容器內部連接到MySQL,但不能從本地主機連接,我得到的錯誤是主機site_mysql是未知的。
碼頭工人文件是:
FROM mysql:5.7
MAINTAINER Author
# The official MySQL docker image will run all .sh and .sql scripts found in this directory
# the first time the container is started.
COPY init.sql /docker-entrypoint-initdb.d
COPY my.cnf /etc/mysql/conf.d
my.cnf文件:
[mysqld]
# Always use UTC
default-time-zone='+00:00'
# To turn off strict mode (alas, we started out that way, will be work to turn it on)
sql-mode="NO_ENGINE_SUBSTITUTION"
max-allowed-packet = 16M
### Per-thread Buffers
sort-buffer-size = 2M
read-buffer-size = 128K
read-rnd-buffer-size = 256K
join-buffer-size = 256K
### Temp Tables
tmp-table-size = 64M
max-heap-table-size = 64M
#### Storage Engines
default-storage-engine = InnoDB
innodb = FORCE
init.sql:
CREATE DATABASE IF NOT EXISTS site;
CREATE DATABASE IF NOT EXISTS site_test;
CREATE USER 'site'@'%' IDENTIFIED BY 'secret';
GRANT ALL PRIVILEGES ON site.* TO 'site'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
一段時間以來,我在不同項目中使用相同的配置,到目前為止一切都還可以。 為什么我的主機無法識別site_mysql
任何想法?
更新
另外,我有:
site_memcached:
image: memcached:1.4-alpine
networks:
- videosite_appnet
在這里我可以使用site_memcached
作為主機名進行連接
如果從主機連接,則必須使用“ localhost”而不是“ site_mysql”
site_mysql: --> this is the servername within docker
image: site/mysql:latest
build: ./images/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
volumes:
- site_mysqldata:/var/lib/mysql
networks:
- site_appnet
ports:
- "3306:3306" --> here you open
port 3306 also on localhost (your host computer)
and bind it to the containers port 3306
我的個人配置(mac主機)是:(也許“重新啟動:總是”有幫助)
db:
image: "mysql:5"
ports:
- "3306:3306"
networks:
- main
volumes:
- ./mysql/config:/etc/mysql/conf.d:cached
- ./mysql/data:/var/lib/mysql:cached
- ./mysql/init:/docker-entrypoint-initdb.d:cached
restart: always
environment:
TZ: UTC
MYSQL_ROOT_PASSWORD: somepassword
要從主機連接到mySQL,請使用以下命令
docker exec -it mysql_container-name mysql -uroot -p
“ root”是MySQL數據庫的用戶名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.