programing

Mariadb Docker 컨테이너가 데이터베이스 스키마를 사용한 초기화를 거부함

luckcodes 2022. 11. 7. 22:17

Mariadb Docker 컨테이너가 데이터베이스 스키마를 사용한 초기화를 거부함

다음 docker-compose를 사용하여 웹 서비스에서 사용되는 데이터베이스 서비스를 초기화합니다.

<!-- language: YAML -->
version: '3'
services:
  web:
    build: 
      context: .
      dockerfile: container-build/web/Dockerfile
    ports:
      - "8080:80"
    volumes:
    - /home/johndoe/src/proj:/var/www/public_html
    links:
    - db

  db:
    image: mariadb:10.4.7
    ports:
    - "6603:3306"
    restart: always
    environment:
    - MYSQL_DATABASE=mydb
    - MYSQL_ROOT_PASSWORD=12345678
    - MYSQL_USER=dbuser
    - MYSQL_PASSWORD=12345678

    volumes:
      - "mysql_data:/var/lib/mysql"
      - /home/johndoe/src/proj/data/schema.sql:/docker-entrypoint-initdb.d/schema.sql
volumes: 
   mysql_data: { driver: local }

그런 다음 다음과 같이 컨테이너를 제작하고 실행합니다.

docker-compose build
docker-compose up

로그에서 볼 수 있듯이 데이터베이스 mydb는 초기화되지 않습니다.컨테이너 내에서 셸을 실행하여 데이터베이스에 액세스하려고 하면 알 수 있습니다.따라서 루트 사용자가 생성되지 않고 데이터베이스도 생성되지 않으며 스키마가 Import되지 않습니다.그러나 흥미롭게도 환경변수는 컨테이너로 Import되었기 때문에 코타이너용 셸에서 사용할 수 있습니다.예를 들어 에코 $MYSql_USER prints 입니다.dbuser이것은 도커 파일 내에 정의되어 있는 것입니다.

<!-- language: plain -->
Recreating proj_db_1 ... done
Recreating proj_web_1 ... done
Attaching to proj_db_1, proj_web_1
db_1   | 2019-08-25  5:08:33 0 [Note] mysqld (mysqld 10.4.7-MariaDB-1:10.4.7+maria~bionic) starting as process 1 ...
db_1   | 2019-08-25  5:08:33 0 [Note] InnoDB: Using Linux native AIO
db_1   | 2019-08-25  5:08:33 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1   | 2019-08-25  5:08:33 0 [Note] InnoDB: Uses event mutexes
db_1   | 2019-08-25  5:08:33 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
db_1   | 2019-08-25  5:08:33 0 [Note] InnoDB: Number of pools: 1
db_1   | 2019-08-25  5:08:33 0 [Note] InnoDB: Using SSE2 crc32 instructions
db_1   | 2019-08-25  5:08:33 0 [Note] mysqld: O_TMPFILE is not supported on /tmp (disabling future attempts)
db_1   | 2019-08-25  5:08:33 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
db_1   | 2019-08-25  5:08:33 0 [Note] InnoDB: Completed initialization of buffer pool
db_1   | 2019-08-25  5:08:33 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   | 2019-08-25  5:08:33 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
db_1   | 2019-08-25  5:08:33 0 [Note] InnoDB: Creating shared tablespace for temporary tables
db_1   | 2019-08-25  5:08:33 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
db_1   | 2019-08-25  5:08:33 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
db_1   | 2019-08-25  5:08:33 0 [Note] InnoDB: Waiting for purge to start
db_1   | 2019-08-25  5:08:33 0 [Note] InnoDB: 10.4.7 started; log sequence number 140016; transaction id 21
db_1   | 2019-08-25  5:08:33 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
db_1   | 2019-08-25  5:08:33 0 [Note] Plugin 'FEEDBACK' is disabled.
db_1   | 2019-08-25  5:08:33 0 [Note] Server socket created on IP: '::'.
db_1   | 2019-08-25  5:08:33 0 [Warning] 'proxies_priv' entry '@% root@0d988a449cca' ignored in --skip-name-resolve mode.
db_1   | 2019-08-25  5:08:33 0 [Note] InnoDB: Buffer pool(s) load completed at 190825  5:08:33
db_1   | 2019-08-25  5:08:33 0 [Note] Reading of all Master_info entries succeeded
db_1   | 2019-08-25  5:08:33 0 [Note] Added new Master_info '' to hash table
db_1   | 2019-08-25  5:08:33 0 [Note] mysqld: ready for connections.
db_1   | Version: '10.4.7-MariaDB-1:10.4.7+maria~bionic'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution
web_1  | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.19.0.3. Set the 'ServerName' directive globally to suppress this message
web_1  | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.19.0.3. Set the 'ServerName' directive globally to suppress this message
web_1  | [Sun Aug 25 05:08:33.526373 2019] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.2.21 configured -- resuming normal operations
web_1  | [Sun Aug 25 05:08:33.526419 2019] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
db_1   | 2019-08-25  5:08:46 8 [Warning] Access denied for user 'dbuser'@'172.19.0.3' (using password: YES)
web_1  | 172.19.0.1 - - [25/Aug/2019:05:08:46 +0000] "GET /test.php HTTP/1.1" 200 402 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Safari/605.1.15"
db_1   | 2019-08-25  5:08:47 9 [Warning] Access denied for user 'dbuser'@'172.19.0.3' (using password: YES)
web_1  | 172.19.0.1 - - [25/Aug/2019:05:08:47 +0000] "GET /test.php HTTP/1.1" 200 401 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Safari/605.1.15"
db_1   | 2019-08-25  5:08:49 10 [Warning] Access denied for user 'dbuser'@'172.19.0.3' (using password: YES)
web_1  | 172.19.0.1 - - [25/Aug/2019:05:08:49 +0000] "GET /test.php HTTP/1.1" 200 401 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Safari/605.1.15"
db_1   | 2019-08-25  5:09:35 11 [Warning] Access denied for user 'root'@'localhost' (using password: NO)

docker-compose.yml이 정상적으로 동작하고 있지 않음을 나타내는 다른 증거는 http://localhost:8080/test.php를 참조했을 때 다음과 같은 내용이 있습니다.

<!-- language: php -->
<?php

echo "Testing database<br>";

try{
$db = new PDO('mysql:dbname=mydb;host=db', 'dbuser', '12345678');
}catch(PDOException $e){
    echo $e->getMessage();
    return;
}
 ?>

이 페이지에는 다음이 표시됩니다.

 <!-- language: plain -->
 Testing database
 SQLSTATE[HY000] [1045] Access denied for user 'dbuser'@'172.19.0.3' (using password: YES)

한편, 명령줄에서 mariadb 이미지를 실행하여 환경 변수와 스키마를 가리키는 볼륨을 전달합니다.sql 명령줄을 사용하여 데이터베이스를 초기화합니다.예를 들어 다음 명령어가 작동합니다.

<!-- language: lang-bash -->
docker run --name mariadbtest  --rm -ti -d   -e "MYSQL_ROOT_PASSWORD=12345678" -e "MYSQL_USER=dbuser" -e MYSQL_PASSWORD=12345678  -v /home/johndoe/src/proj/data/schema.sql:/docker-entrypoint-initdb.d/schema.sql  mariadb 

그럼 내 도커콤포지트.yml은 뭐가 문제죠?Adam CulpYouTube 비디오는 매우 유사한 docker-compose.yaml을 사용합니다.

단답:
/var/lib/mysql에 매핑된 데이터 볼륨의 이름을 변경하고 mysql_data를 db_data로 이름을 변경합니다.docker-compose up다시.

장황한 답변:

나는 결국 그 문제를 알아냈다.MariaDB의 도커 허브 페이지는 컨테이너가 처음 작성되었을 때만 /docker-entrypoint-initdb.d에서 사용할 수 있는 데이터베이스 스키마를 사용하여 초기화된다는 사실에 대해 약간 언급하고 있습니다.

컨테이너가 처음 시작되면 지정된 이름의 새 데이터베이스가 생성되고 제공된 구성 변수를 사용하여 초기화됩니다.또한 /docker-entrypoint-initdb.d에 있는 확장자 .sh, .sql 및 .sql.gz 파일을 실행합니다.파일은 알파벳 순으로 실행됩니다.mariadb 서비스는 SQL 덤프를 해당 디렉토리에 마운트하여 쉽게 채울 수 있습니다.또, 커스텀 이미지에 투고된 데이터를 제공할 수 있습니다.SQL 파일은 기본적으로 MYSQL_DATABASE 변수에 의해 지정된 데이터베이스로 Import됩니다.

이로 인해 docker-compose에서 db 서비스 이름을 변경하고 다시 실행했지만 작동하지 않았습니다., 정지해 있던 용기도 모두 했습니다.docker container prune그것도 도움이 안 됐어요

나는 이 있어야 한다고 생각했다.docker-compose up이치노docker-compose.yml의 입니다.데이터 계층은 재구현 시 재작성되지 않는 것 같습니다.또한 환경변수를 제공하지 않았기 때문에 처음 실행했을 때 잘못 초기화되어 있었습니다. db_data를 .docker-compose up! 이번에 되어 초기화 파일 에 도입한 에 접근할 수 .이번에는 데이터베이스가 초기화되어 초기화 시 스키마 파일의 컨테이너에 소개한 데이터베이스 테이블에 액세스할 수 있었습니다.

언급URL : https://stackoverflow.com/questions/57643617/mariadb-docker-container-refuses-to-initialize-with-database-schema