我已经在Laradock上部署了一个Laravel应用程序。
我想指出数据库连接工作正常(用户可以注册,登录,...等)。
为了备份应用程序,我已经安装了Spatie的Laravel备份包。
我相应地设置了所有配置变量,如下所示:
配置/备份.php
<?php
return [
'backup' => [
/*
* The name of this application. You can use this name to monitor
* the backups.
*/
'name' => env('APP_NAME', 'laravel-backup'),
'source' => [
/*......*/
/*
* The names of the connections to the databases that should be backed up
* MySQL, PostgreSQL, SQLite and Mongo databases are supported.
*
* The content of the database dump may be customized for each connection
* by adding a 'dump' key to the connection settings in config/database.php.
* E.g.
* 'mysql' => [
* ...
* 'dump' => [
* 'excludeTables' => [
* 'table_to_exclude_from_backup',
* 'another_table_to_exclude'
* ]
* ],
* ],
*
* If you are using only InnoDB tables on a MySQL server, you can
* also supply the useSingleTransaction option to avoid table locking.
*
* E.g.
* 'mysql' => [
* ...
* 'dump' => [
* 'useSingleTransaction' => true,
* ],
* ],
*
* For a complete list of available customization options, see https://github.com/spatie/db-dumper
*/
'databases' => [
'mysql',
],
],
/*
* The database dump can be compressed to decrease diskspace usage.
*
* Out of the box Laravel-backup supplies
* SpatieDbDumperCompressorsGzipCompressor::class.
*
* You can also create custom compressor. More info on that here:
* https://github.com/spatie/db-dumper#using-compression
*
* If you do not want any compressor at all, set it to null.
*/
'database_dump_compressor' => null,
/*....*/
],
],
];
在Laravel Homestead中,备份工作正常。
$ php artisan backup:run
.
根据这个包的文档,我们需要指定mysqldump
二进制文件的路径,如下所示:
配置/数据库.php
//config/database.php
'connections' => [
'mysql' => [
'driver' => 'mysql'
...,
'dump' => [
'dump_binary_path' => '/path/to/the/binary', // only the path, so without `mysqldump` or `pg_dump`
'use_single_transaction',
'timeout' => 60 * 5, // 5 minute timeout
'exclude_tables' => ['table1', 'table2'],
'add_extra_option' => '--optionname=optionvalue',
]
],
这是我在同一文件中默认得到的:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
'dump'=>[
'dump_binary_path' => env('DB_DUMP_PATH'), // only the path, so without `mysqldump` or `pg_dump`
'use_single_transaction',
'timeout' => 60 * 5, // 5 minute timeout
//'exclude_tables' => ['table1', 'table2'],
//'add_extra_option' => '--optionname=optionvalue',
'add_extra_option' => '--host='.env('DB_HOST'),
]
],
在我的.env文件中:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DUMP_PATH='/usr/bin/'
如果我在 MySQL 容器中执行mysqldump
,它确实有效:
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help
但是,如果我在工作区容器内运行备份命令,
artisan backup:run
我收到错误:
备份失败,因为:转储过程失败,退出代码为 127: 找不到命令:sh:1:mysqldump:未找到
那么我如何告诉 Laradock mysqldump 二进制路径的位置呢?
有解决方法吗?也许设置一个新容器来连接Workspace
和MySQL
容器?
有什么替代方法吗?
在 Adrien 的帮助下解决 在.env
文件中,我确实发现以下变量设置为 false:
### WORKSPACE #############################################
####
# ...
WORKSPACE_INSTALL_MYSQL_CLIENT=false
# ...
所以我把它改成真:
### WORKSPACE #############################################
####
# ...
WORKSPACE_INSTALL_MYSQL_CLIENT=true
# ...
我保存并退出。
这也意味着我不必再对docker-compose.custom.yml
文件进行任何更改
为了应用该更改,我执行了(无需停止任何容器)
$ docker-compose build workspace
$ docker-compose -f docker-compose.custom.yml up -d workspace
然后我进入了容器
$ docker exec -it my_workspace bash
进去后,我寻找mysqldump:
# which mysqldump
/usr/bin/mysqldump
最后,我可以执行Spatie的Laravel备份包:
# artisan backup:run
Starting backup...
Dumping database xyz...
Determining files to backup...
Zipping x files and directories...
Created zip containing x files and directories. Size is x.x MB
Copying zip to disk named backMeUp...
Successfully copied zip to disk named backMeUp.
Backup completed!
这就成功了!
进一步的问题
完全相同的错误
The dump process failed with exitcode 127 : Command not found : sh: /usr/bin/mysqldump: not found
尝试运行时显示
artisan backup:run
来自拉拉维尔计划任务。这意味着php-worker
容器没有安装mysql-client
(因此,mysqldump
)软件包。
解决方法我按如下方式登录到容器:
docker exec -it php-worker sh
然后我安装了 mysql 客户端,如下所示:
/etc/supervisor/conf.d # apk add mysql-client
fetch https://dl-cdn.alpinelinux.org/alpine/v3.13/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.13/community/x86_64/APKINDEX.tar.gz
(1/3) Installing mariadb-common (10.5.17-r0)
(2/3) Installing mariadb-client (10.5.17-r0)
(3/3) Installing mysql-client (10.5.17-r0)
Executing busybox-1.32.1-r3.trigger
OK: 358 MiB in 107 packages
/etc/supervisor/conf.d # which mysqldump
/usr/bin/mysqldump
/etc/supervisor/conf.d # exit
这似乎奏效了?
如果是这样,一个新的问题出现了:如何直接从php-worker Dockerfile包含mysql-client?
回答自己好吧,我忘了将与"_INSTALL_MYSQL_CLIENT"相关的所有变量设置为 true......
如此处所述,编辑 laradock.env
文件并设置:
WORKSPACE_INSTALL_MYSQL_CLIENT=true
然后运行:
docker-compose build workspace && docker-composer up -d workspace
这将更新您的workspace
容器并重新启动它。
连接到容器:
docker-compose exec workspace bash
您应该有权访问mysqldump
:
root@82d8b3b3c0a0:/var/www# which mysqldump
/usr/bin/mysqldump
删除转储及其中的所有内容或在config/database.php中对其进行注释,那么它无需转储即可正常工作,如下所示:
//'dump' => [
// 'dump_binary_path' => '/path/to/the/binary', // only the path, so //without `mysqldump` or `pg_dump`
// 'use_single_transaction',
// 'timeout' => 60 * 5, // 5 minute timeout
// 'exclude_tables' => ['table1', 'table2'],
// 'add_extra_option' => '--optionname=optionvalue',
// ]
但是,如果您在Windows上使用XAMP服务器,则需要对其进行更改,如下所示:
'dump' => [
'dump_binary_path' => 'C:/xampp/mysql/bin/', // only the path, so without `mysqldump` or `pg_dump`
'use_single_transaction',
'timeout' => 60 * 5, // 5 minute timeout
],