Как изменить порт по умолчанию, на котором работает мой экземпляр Docker MySql? - PullRequest
0 голосов
/ 29 января 2020

Я использую docker -составить версию 1.25.2. Я хочу создать образ docker, содержащий базу данных MySql 5.7, но я бы хотел изменить порт по умолчанию, на котором 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?

1 Ответ

1 голос
/ 29 января 2020

Это должно быть в формате 'external_port:internal_port':

ports:
  - '3406:3306'

https://docs.docker.com/compose/compose-file/#ports

Внутренне, все еще слушает порт 3306 для других контейнеров, только с использованием другого порта, доступного для вашего локального компьютера (3406)

Для предоставления другого порта внутри вы можете создать DockerFile для mysql контейнера изменение настроенного порта в 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'
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...