MariaDB卷在重新启动Docker容器时丢失记录

tp5buhyn  于 2022-11-08  发布在  Docker
关注(0)|答案(1)|浏览(205)

我有一个Laravel应用程序运行在Docker上,我有Breeze安装。这是我的设置
这是我的docker-compose.yml文件

version: "3.8"

services:
    web:
        container_name: apache
        build:
            context: .
            dockerfile: docker/apache/Dockerfile
        volumes:
            - ./public:/var/www/html/public
        ports:
            - "8080:80"
        depends_on:
            - php
            - mysql
        networks:
            - localnet
        env_file:
            - ./.env
            - ./docker/docker.env
    php:
        container_name: php
        build:
            context: .
            dockerfile: docker/php/Dockerfile
        ports:
            - "9000:9000"
        volumes:
            - .:/var/www/html
        networks:
            - localnet
        depends_on:
            - mysql
        env_file:
            - ./.env
            - ./docker/docker.env
    mysql:
        container_name: mysql
        image: mariadb:10.6
        volumes:
            - dbdata:/var/lib/mysql
            - ./docker/mysql/conf.d:/etc/mysql/conf.d
        ports:
            - "4306:3306"
        networks:
            - localnet
        env_file:
            - ./.env
            - ./docker/docker.env
networks:
    localnet:
        driver: 'bridge'
volumes:
    dbdata:
        driver: 'local'

环境包含所需的MYSQL_* 变量,因此

// .env
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravelapp
DB_USERNAME=ltdev
DB_PASSWORD=secret

// docker.env
MYSQL_DATABASE="${DB_DATABASE}"
MYSQL_USER="${DB_USERNAME}"
MYSQL_PASSWORD="${DB_PASSWORD}"
MYSQL_ROOT_PASSWORD=root

// also runnin printenv | grep MYSQL_ shows me that the variables are set correctly
MYSQL_ROOT_PASSWORD=root
MYSQL_PASSWORD=secret
MYSQL_USER=ltdev
MYSQL_PORT=4306
MYSQL_DATABASE=laravelapp

还在运行docker-compose ps我可以看到容器正在按预期运行

NAME                COMMAND                  SERVICE             STATUS              PORTS
apache              "httpd -D FOREGROUND"    web                 running             0.0.0.0:8080->80/tcp
mysql               "docker-entrypoint.s…"   mysql               running             0.0.0.0:4306->3306/tcp        
php                 "docker-php-entrypoi…"   php                 running

当容器启动并运行时,我从浏览器注册一个新用户,我可以看到数据库中显示的记录。(即不持久)并且表是空的。即使我将使用的db映像从mariadb切换到mysql,也会发生同样的情况。我一直在检查如何挂载卷,以及我是否做错了什么,但没有发现我做了什么与我找到的示例或文档不同的事情。不确定是否与容器开始的顺序有关,我试着玩了一下depends_on,但没有运气。你知道我做错了什么吗?
这是注册新用户后的数据库

select id, name, email from users;
+----+-----------+-----------------------+
| id | name      | email                 |
+----+-----------+-----------------------+
| 12 | Test User | test@test.com         |
+----+-----------+-----------------------+
1 row in set (0.000 sec)

这是在我运行docker-compose stopdocker-compose up -d之后

select id, name, email from users;
Empty set (0.000 sec)

还有MySQL日志

mariadbd, Version: 10.8.3-MariaDB-1:10.8.3+maria~jammy-log (mariadb.org binary distribution). started with:
Tcp port: 0  Unix socket: /run/mysqld/mysqld.sock
Time                Id Command  Argument
220528  9:47:01      3 Connect  ltdev@172.24.0.5 on laravelapp using TCP/IP
                     3 Query    use `laravelapp`
                     3 Prepare  set names 'utf8mb4' collate 'utf8mb4_unicode_ci'
                     3 Execute  set names 'utf8mb4' collate 'utf8mb4_unicode_ci'
                     3 Close stmt
                     3 Prepare  set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
                     3 Execute  set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
                     3 Close stmt
                     3 Prepare  SHOW FULL TABLES WHERE table_type = 'BASE TABLE'
                     3 Execute  SHOW FULL TABLES WHERE table_type = 'BASE TABLE'
                     3 Close stmt
                     3 Prepare  SET FOREIGN_KEY_CHECKS=0
                     3 Execute  SET FOREIGN_KEY_CHECKS=0
                     3 Close stmt
                     3 Prepare  drop table `failed_jobs`,`migrations`,`password_resets`,`personal_access_tokens`,`users`
                     3 Execute  drop table `failed_jobs`,`migrations`,`password_resets`,`personal_access_tokens`,`users`
                     3 Close stmt
                     3 Prepare  SET FOREIGN_KEY_CHECKS=1
                     3 Execute  SET FOREIGN_KEY_CHECKS=1
                     3 Close stmt
                     3 Prepare  select * from information_schema.tables where table_schema = ? and table_name = ? and table_type = 'BASE TABLE'
                     3 Execute  select * from information_schema.tables where table_schema = 'laravelapp' and table_name = 'migrations' and table_type = 'BASE TABLE'
                     3 Close stmt
                     3 Prepare  create table `migrations` (`id` int unsigned not null auto_increment primary key, `migration` varchar(255) not null, `batch` int not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
                     3 Execute  create table `migrations` (`id` int unsigned not null auto_increment primary key, `migration` varchar(255) not null, `batch` int not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
                     3 Close stmt
                     3 Prepare  select * from information_schema.tables where table_schema = ? and table_name = ? and table_type = 'BASE TABLE'
                     3 Execute  select * from information_schema.tables where table_schema = 'laravelapp' and table_name = 'migrations' and table_type = 'BASE TABLE'
                     3 Close stmt
                     3 Prepare  select `migration` from `migrations` order by `batch` asc, `migration` asc
                     3 Execute  select `migration` from `migrations` order by `batch` asc, `migration` asc
                     3 Close stmt
                     3 Prepare  select `migration` from `migrations` order by `batch` asc, `migration` asc
                     3 Execute  select `migration` from `migrations` order by `batch` asc, `migration` asc
                     3 Close stmt
                     3 Prepare  select max(`batch`) as aggregate from `migrations`
                     3 Execute  select max(`batch`) as aggregate from `migrations`
                     3 Close stmt
                     3 Prepare  create table `users` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(255) not null, `email` varchar(255) not null, `email_verified_at` timestamp null, `password` varchar(255) not null, `remember_token` varchar(100) null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
                     3 Execute  create table `users` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(255) not null, `email` varchar(255) not null, `email_verified_at` timestamp null, `password` varchar(255) not null, `remember_token` varchar(100) null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
                     3 Close stmt
                     3 Prepare  alter table `users` add unique `users_email_unique`(`email`)
                     3 Execute  alter table `users` add unique `users_email_unique`(`email`)
                     3 Close stmt
                     3 Prepare  insert into `migrations` (`migration`, `batch`) values (?, ?)
                     3 Execute  insert into `migrations` (`migration`, `batch`) values ('2014_10_12_000000_create_users_table', 1)
                     3 Close stmt
                     3 Prepare  create table `password_resets` (`email` varchar(255) not null, `token` varchar(255) not null, `created_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
                     3 Execute  create table `password_resets` (`email` varchar(255) not null, `token` varchar(255) not null, `created_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
                     3 Close stmt
                     3 Prepare  alter table `password_resets` add index `password_resets_email_index`(`email`)
                     3 Execute  alter table `password_resets` add index `password_resets_email_index`(`email`)
                     3 Close stmt
                     3 Prepare  insert into `migrations` (`migration`, `batch`) values (?, ?)
                     3 Execute  insert into `migrations` (`migration`, `batch`) values ('2014_10_12_100000_create_password_resets_table', 1)
                     3 Close stmt
                     3 Prepare  create table `failed_jobs` (`id` bigint unsigned not null auto_increment primary key, `uuid` varchar(255) not null, `connection` text not null, `queue` text not null, `payload` longtext not null, `exception` longtext not null, `failed_at` timestamp default CURRENT_TIMESTAMP not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
                     3 Execute  create table `failed_jobs` (`id` bigint unsigned not null auto_increment primary key, `uuid` varchar(255) not null, `connection` text not null, `queue` text not null, `payload` longtext not null, `exception` longtext not null, `failed_at` timestamp default CURRENT_TIMESTAMP not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
                     3 Close stmt
                     3 Prepare  alter table `failed_jobs` add unique `failed_jobs_uuid_unique`(`uuid`)
                     3 Execute  alter table `failed_jobs` add unique `failed_jobs_uuid_unique`(`uuid`)
                     3 Close stmt
                     3 Prepare  insert into `migrations` (`migration`, `batch`) values (?, ?)
                     3 Execute  insert into `migrations` (`migration`, `batch`) values ('2019_08_19_000000_create_failed_jobs_table', 1)
                     3 Close stmt
                     3 Prepare  create table `personal_access_tokens` (`id` bigint unsigned not null auto_increment primary key, `tokenable_type` varchar(255) not null, `tokenable_id` bigint unsigned not null, `name` varchar(255) not null, `token` varchar(64) not null, `abilities` text null, `last_used_at` timestamp null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
                     3 Execute  create table `personal_access_tokens` (`id` bigint unsigned not null auto_increment primary key, `tokenable_type` varchar(255) not null, `tokenable_id` bigint unsigned not null, `name` varchar(255) not null, `token` varchar(64) not null, `abilities` text null, `last_used_at` timestamp null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
                     3 Close stmt
                     3 Prepare  alter table `personal_access_tokens` add index `personal_access_tokens_tokenable_type_tokenable_id_index`(`tokenable_type`, `tokenable_id`)
                     3 Execute  alter table `personal_access_tokens` add index `personal_access_tokens_tokenable_type_tokenable_id_index`(`tokenable_type`, `tokenable_id`)
                     3 Close stmt
                     3 Prepare  alter table `personal_access_tokens` add unique `personal_access_tokens_token_unique`(`token`)
                     3 Execute  alter table `personal_access_tokens` add unique `personal_access_tokens_token_unique`(`token`)
                     3 Close stmt
                     3 Prepare  insert into `migrations` (`migration`, `batch`) values (?, ?)
                     3 Execute  insert into `migrations` (`migration`, `batch`) values ('2019_12_14_000001_create_personal_access_tokens_table', 1)
                     3 Close stmt
                     3 Query    START TRANSACTION
220528  9:47:02      3 Query    ROLLBACK
                     3 Quit
                     4 Connect  laravel@172.24.0.5 on laravelapp using TCP/IP
                     4 Query    use `laravelapp`
                     4 Prepare  set names 'utf8mb4' collate 'utf8mb4_unicode_ci'
                     4 Execute  set names 'utf8mb4' collate 'utf8mb4_unicode_ci'
                     4 Close stmt
                     4 Prepare  set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
                     4 Execute  set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
                     4 Close stmt
                     5 Query    START TRANSACTION
                     5 Prepare  insert into `users` (`name`, `email`, `email_verified_at`, `password`, `remember_token`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?, ?)
                     5 Execute  insert into `users` (`name`, `email`, `email_verified_at`, `password`, `remember_token`, `updated_at`, `created_at`) values ('Arely Hoppe', 'langworth.adele@example.net', '2022-05-28 09:47:03', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'QYnNmxKzse', '2022-05-28 09:47:03', '2022-05-28 09:47:03')
                     5 Close stmt
                     5 Query    ROLLBACK
                     5 Quit
                     6 Connect  laravel@172.24.0.5 on laravelapp using TCP/IP
                     6 Query    use `laravelapp`
                     6 Prepare  set names 'utf8mb4' collate 'utf8mb4_unicode_ci'
                     6 Execute  set names 'utf8mb4' collate 'utf8mb4_unicode_ci'
                     6 Close stmt
                     6 Prepare  set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
                     6 Execute  set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
                     6 Close stmt
                     6 Query    START TRANSACTION
                     6 Prepare  insert into `users` (`name`, `email`, `email_verified_at`, `password`, `remember_token`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?, ?)
                     6 Execute  insert into `users` (`name`, `email`, `email_verified_at`, `password`, `remember_token`, `updated_at`, `created_at`) values ('Jayson Lind', 'jonathon.ritchie@example.com', NULL, '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'soG9Ck1eIN', '2022-05-28 09:47:03', '2022-05-28 09:47:03')
                     6 Close stmt
                     6 Query    ROLLBACK
                     6 Quit
    ...
    ...

// This is where the user gets added

220528  9:47:46     18 Connect  ltdev@172.24.0.8 on laravelapp using TCP/IP
                    18 Query    use `laravelapp`
                    18 Prepare  set names 'utf8mb4' collate 'utf8mb4_unicode_ci'
                    18 Execute  set names 'utf8mb4' collate 'utf8mb4_unicode_ci'
                    18 Close stmt
                    18 Prepare  set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
                    18 Execute  set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
                    18 Close stmt
                    18 Prepare  select count(*) as aggregate from `users` where `email` = ?
                    18 Execute  select count(*) as aggregate from `users` where `email` = 'ltdev@laravelapp.com'
                    18 Close stmt
                    18 Prepare  insert into `users` (`name`, `email`, `password`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?)
                    18 Execute  insert into `users` (`name`, `email`, `password`, `updated_at`, `created_at`) values ('LT Dev', 'ltdev@laravelapp.com', '$2y$10$OY8YIf.x8s1IOW9zO8Zi8e1veMsv7ISKAWcMIZkE2o1Swy7cDYhpO', '2022-05-28 09:47:46', '2022-05-28 09:47:46')
                    18 Close stmt
                    18 Quit
                    19 Connect  laravel@172.24.0.8 on laravelapp using TCP/IP
                    19 Query    use `laravelapp`
                    19 Prepare  set names 'utf8mb4' collate 'utf8mb4_unicode_ci'
                    19 Execute  set names 'utf8mb4' collate 'utf8mb4_unicode_ci'
                    19 Close stmt
                    19 Prepare  set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
                    19 Execute  set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
                    19 Close stmt
                    19 Prepare  select * from `users` where `id` = ? limit 1
                    19 Execute  select * from `users` where `id` = 12 limit 1
                    19 Close stmt
                    19 Quit

// This is where the new session probably starts after compose up -d

mariadbd, Version: 10.8.3-MariaDB-1:10.8.3+maria~jammy-log (mariadb.org binary distribution). started with:
Tcp port: 0  Unix socket: /run/mysqld/mysqld.sock
Time                Id Command  Argument
220528  9:48:01      3 Connect  laravel@172.24.0.3 on laravelapp using TCP/IP
                     3 Query    use `laravelapp`
                     3 Prepare  set names 'utf8mb4' collate 'utf8mb4_unicode_ci'
                     3 Execute  set names 'utf8mb4' collate 'utf8mb4_unicode_ci'
                     3 Close stmt
                     3 Prepare  set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
                     3 Execute  set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
                     3 Close stmt
                     3 Prepare  SHOW FULL TABLES WHERE table_type = 'BASE TABLE'
                     3 Execute  SHOW FULL TABLES WHERE table_type = 'BASE TABLE'
                     3 Close stmt
                     3 Prepare  SET FOREIGN_KEY_CHECKS=0
                     3 Execute  SET FOREIGN_KEY_CHECKS=0
                     3 Close stmt
                     3 Prepare  drop table `failed_jobs`,`migrations`,`password_resets`,`personal_access_tokens`,`users`
                     3 Execute  drop table `failed_jobs`,`migrations`,`password_resets`,`personal_access_tokens`,`users`
                     3 Close stmt
                     3 Prepare  SET FOREIGN_KEY_CHECKS=1
                     3 Execute  SET FOREIGN_KEY_CHECKS=1
                     3 Close stmt
                     3 Prepare  select * from information_schema.tables where table_schema = ? and table_name = ? and table_type = 'BASE TABLE'
                     3 Execute  select * from information_schema.tables where table_schema = 'laravelapp' and table_name = 'migrations' and table_type = 'BASE TABLE'
                     3 Close stmt
                     3 Prepare  create table `migrations` (`id` int unsigned not null auto_increment primary key, `migration` varchar(255) not null, `batch` int not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
                     3 Execute  create table `migrations` (`id` int unsigned not null auto_increment primary key, `migration` varchar(255) not null, `batch` int not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
                     3 Close stmt
                     3 Prepare  select * from information_schema.tables where table_schema = ? and table_name = ? and table_type = 'BASE TABLE'
                     3 Execute  select * from information_schema.tables where table_schema = 'laravelapp' and table_name = 'migrations' and table_type = 'BASE TABLE'
                     3 Close stmt
                     3 Prepare  select `migration` from `migrations` order by `batch` asc, `migration` asc
                     3 Execute  select `migration` from `migrations` order by `batch` asc, `migration` asc
                     3 Close stmt
                     3 Prepare  select `migration` from `migrations` order by `batch` asc, `migration` asc
                     3 Execute  select `migration` from `migrations` order by `batch` asc, `migration` asc

如果我列出存储数据的mysql/mariadb容器中的文件路径的内容,就会得到这样的结果


# ls -lah /var/lib/mysql/laravelapp

total 600K
drwx------ 2 mysql mysql 4.0K May 29 20:29 .
drwxr-xr-x 6 mysql mysql 4.0K May 29 20:29 ..
-rw-rw---- 1 mysql mysql   67 May 29 20:29 db.opt
-rw-rw---- 1 mysql mysql 2.6K May 29 20:29 failed_jobs.frm
-rw-rw---- 1 mysql mysql 112K May 29 20:29 failed_jobs.ibd
-rw-rw---- 1 mysql mysql 2.0K May 29 20:29 migrations.frm
-rw-rw---- 1 mysql mysql  96K May 29 20:29 migrations.ibd
-rw-rw---- 1 mysql mysql 3.0K May 29 20:29 password_resets.frm
-rw-rw---- 1 mysql mysql 112K May 29 20:29 password_resets.ibd
-rw-rw---- 1 mysql mysql 4.4K May 29 20:29 personal_access_tokens.frm
-rw-rw---- 1 mysql mysql 128K May 29 20:29 personal_access_tokens.ibd
-rw-rw---- 1 mysql mysql 5.0K May 29 20:29 users.frm
-rw-rw---- 1 mysql mysql 112K May 29 20:29 users.ibd

任何帮助都将不胜感激

shstlldc

shstlldc1#

更新-调试

尝试1:使用本地文件夹Map进行监视
Map到本地目录(只读绑定)并观察本地文件更改。您执行只读绑定,以便可以观察容器文件夹/文件更新是否写入本地数据文件夹,但任何本地更改都不会复制到容器。官方文档。
尝试2:外部体积
Docker可以创建独立于容器的卷。请尝试创建一个独立的卷,并在您的合成yaml文件中为该卷使用外部标志。

原始答案

这可能与两个问题有关,第一个问题可能性很小,是mysql容器(MariaDB 10.6)的数据目录的位置,这可能会因容器/硬件配置而异。
第二种情况很可能是您的Docker版本没有持久保存卷,在这种情况下,您可以将其Map到本地文件夹......就像您Mapconf.d(配置文件)一样。
第三种可能的方法是从卷中删除driver:local线路。
希望能有所帮助!

相关问题