我正在使用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'