mysql - 如何更改运行 Docker MySql 实例的默认端口?
问题描述
我正在使用 docker-compose 版本 1.25.2。我想构建一个包含 MySql 5.7 数据库的 docker 映像,但我想更改 MySql 监听 3406 的默认端口。所以我构建了这个 docker-compose.yml 文件......
version: '3.3'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: 'maps_data'
# So you don't have to use root, but you can if you like
MYSQL_USER: 'myuser'
# You can use whatever password you like
MYSQL_PASSWORD: 'password'
# Password for root access
MYSQL_ROOT_PASSWORD: 'password'
ports:
# <Port exposed> : < MySQL Port running inside container>
- '3406:3406'
expose:
# Opens port 3406 on the container
- '3406'
# Where our data will be persisted
volumes:
- my-db:/var/lib/mysql
# Names our volume
volumes:
my-db:
但是,当我尝试运行我的图像时,它仍然显示数据库正在侦听旧端口 3306 ...
localhost:maps davea$ docker-compose build
db uses an image, skipping
localhost:maps davea$ docker-compose up --force-recreate
Recreating maps_db_1 ... done
Attaching to maps_db_1
db_1 | 2020-01-28 21:52:06+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.29-1debian9 started.
db_1 | 2020-01-28 21:52:06+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1 | 2020-01-28 21:52:06+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.29-1debian9 started.
db_1 | 2020-01-28T21:52:06.638642Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
db_1 | 2020-01-28T21:52:06.640167Z 0 [Note] mysqld (mysqld 5.7.29) starting as process 1 ...
db_1 | 2020-01-28T21:52:06.644667Z 0 [Note] InnoDB: PUNCH HOLE support available
db_1 | 2020-01-28T21:52:06.644823Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1 | 2020-01-28T21:52:06.644943Z 0 [Note] InnoDB: Uses event mutexes
db_1 | 2020-01-28T21:52:06.645052Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
db_1 | 2020-01-28T21:52:06.645371Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
db_1 | 2020-01-28T21:52:06.645476Z 0 [Note] InnoDB: Using Linux native AIO
db_1 | 2020-01-28T21:52:06.646032Z 0 [Note] InnoDB: Number of pools: 1
db_1 | 2020-01-28T21:52:06.646538Z 0 [Note] InnoDB: Using CPU crc32 instructions
db_1 | 2020-01-28T21:52:06.648667Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
db_1 | 2020-01-28T21:52:06.659389Z 0 [Note] InnoDB: Completed initialization of buffer pool
db_1 | 2020-01-28T21:52:06.662365Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
db_1 | 2020-01-28T21:52:06.675930Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
db_1 | 2020-01-28T21:52:06.691499Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
db_1 | 2020-01-28T21:52:06.691700Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
db_1 | 2020-01-28T21:52:06.727444Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
db_1 | 2020-01-28T21:52:06.729939Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
db_1 | 2020-01-28T21:52:06.730318Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
db_1 | 2020-01-28T21:52:06.732914Z 0 [Note] InnoDB: 5.7.29 started; log sequence number 12441945
db_1 | 2020-01-28T21:52:06.734291Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
db_1 | 2020-01-28T21:52:06.734886Z 0 [Note] Plugin 'FEDERATED' is disabled.
db_1 | 2020-01-28T21:52:06.737544Z 0 [Note] InnoDB: Buffer pool(s) load completed at 200128 21:52:06
db_1 | 2020-01-28T21:52:06.741378Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
db_1 | 2020-01-28T21:52:06.741605Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
db_1 | 2020-01-28T21:52:06.742582Z 0 [Warning] CA certificate ca.pem is self signed.
db_1 | 2020-01-28T21:52:06.743034Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
db_1 | 2020-01-28T21:52:06.743815Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
db_1 | 2020-01-28T21:52:06.743937Z 0 [Note] IPv6 is available.
db_1 | 2020-01-28T21:52:06.743989Z 0 [Note] - '::' resolves to '::';
db_1 | 2020-01-28T21:52:06.744214Z 0 [Note] Server socket created on IP: '::'.
db_1 | 2020-01-28T21:52:06.746354Z 0 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
db_1 | 2020-01-28T21:52:06.758673Z 0 [Note] Event Scheduler: Loaded 0 events
db_1 | 2020-01-28T21:52:06.759306Z 0 [Note] mysqld: ready for connections.
db_1 | Version: '5.7.29' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
实际上,当我尝试使用 docker-compose.yml 文件中的端口进行连接时,我无法...
localhost:maps davea$ mysql -u myuser -h 127.0.0.1 --port=7777 -p maps_data
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (61)
如何更改我的 docker MySql 实例运行的端口?
解决方案
它应该采用以下格式'external_port:internal_port'
:
ports:
- '3406:3306'
https://docs.docker.com/compose/compose-file/#ports
在内部,它仍在侦听其他容器的端口 3306,仅使用不同的端口公开给您的本地计算机 (3406)
要在内部公开不同的端口,您可以为 mysql 容器创建一个 DockerFile,更改 my.cnf 中配置的端口和公开的端口(在此示例中位于名为 的文件夹中mysql/
):
mysql/Dockerfile
FROM mysql:5.7
RUN sed -i 's/port\s*=\s*3306/port = 3406/' /etc/mysql/my.cnf
CMD ["mysqld"]
EXPOSE 3406
并修改你的docker-compose.yml
db:
image: mysql-local
build: ./mysql/
ports:
- '3406:3406'
推荐阅读
- javascript - Gatsby v2 项目中的缓存问题
- ibm-cloud - 在没有 procfile 的 IBM Bluemix 上部署非 Web 应用程序 (python)?
- android - Ctrl + C vs Ctrl + Z for pausing repo sync?
- r - 使用公式(而不是函数)指定 map2() 的选项
- java - 在云环境中,使用哪些技术让客户端上传要处理的文件的好方法是什么?
- mysql - 插入选择语句
- javascript - 错误类型错误:无法在 Angular 6 中读取 null 的属性“推送”
- java - 在 Android 上禁用 Firebase 离线功能
- javascript - create-react-app eslint error "Parsing error: 'import' and 'export' may appear only with 'sourceType: module'"
- vba - Replacement works only once