最近在服务器上跑docker应用映射通过smb挂载的nas目录时遇到一些问题,在这里记录下,服务器系统是Linux Ubuntu 22.04。
Sqlite database is locked
最初的挂载参数如下,uid和gid取的是当前登录用户的用户id和用户组id。结果就是报database is locked,修改文件权限,添加dir_mode=0777,file_mode=0777参数也还是不行。
//192.168.1.4/xx /mnt/xx cifs credentials=/home/.smbcredentials,uid=1000,gid=1000,rw,noauto,x-systemd.automount,iocharset=utf8 0 0
最后添加nobrl参数后可以了,官方文档里对这个参数的解释:Do not send byte range lock requests to the server. This is necessary for certain applications that break with cifs style mandatory byte range locks (and most cifs servers do not yet support requesting advisory byte range locks).
大概意思是:不要向服务发送字节范围的锁请求,也就是挂载的smb目录不支持字节范围的锁请求。修改后挂载参数如下:
//192.168.1.4/xx /mnt/xx cifs credentials=/home/.smbcredentials,uid=1000,gid=1000,rw,noauto,x-systemd.automount,nobrl,iocharset=utf8 0 0
附上github上关于这个问题的讨论:
SQLITE_BUSY: database is locked · Issue #1678 · NginxProxyManager/nginx-proxy-manager (github.com)
mariadb/mysql 使用远程目录不可用
尝试使用挂载的smb目录映射到mariadb的数据目录,先是报如下问题:
Installation of system tables failed! Examine the logs in /var/lib/mysql for more information.
The problem could be conflicting information in an external my.cnf files. You can ignore these by doing:
shell> /usr/bin/mariadb-install-db --defaults-file=~/.my.cnf You can also try to start the mysqld daemon with:
shell> /usr/bin/mariadbd --skip-grant-tables --general-log &
and use the command line tool /usr/bin/mariadb to connect to the mysql database and look at the grant tables:
shell> /usr/bin/mysql -u root mysql
mysql> show tables;
Try 'mysqld --help' if you have problems with paths. Using --general-log gives you a log in /var/lib/mysql that may be helpful.
尝试添加查了一圈应该是权限问题,上面挂载uid和gid是1000,mariadb和mysql的docker镜像构建的时候创建了mysql用户和用户组id是999,导致没有目录权限。尝试修改文件权限,直接挂载成0777的权限,docker启动没有报错,但是却卡住没有进行db初始化,登录容器后使用命令行连接db连接失败,就像没有启动成功。
又查了下docker映射smb目录,db启动失败相关的问题,发现还挺多人碰到这个问题的,有人建议不要使用SBM挂载要用nfsv4,开启NAS的的nfs共享,尝试通过nfs挂载,结果还是不行,报没有写权限,不清楚是不是nas的nfs是不是没有开启写权限,还是说协议版本不对,折腾了半天还是没有成功。
最后找到了官方说法
You should be cautious when considering whether to use NFS with MySQL. Potential issues, which vary by operating system and NFS version, include the following:
MySQL data and log files placed on NFS volumes becoming locked and unavailable for use. Locking issues may occur in cases where multiple instances of MySQL access the same data directory or where MySQL is shut down improperly, due to a power outage, for example. NFS version 4 addresses underlying locking issues with the introduction of advisory and lease-based locking. However, sharing a data directory among MySQL instances is not recommended.
Data inconsistencies introduced due to messages received out of order or lost network traffic. To avoid this issue, use TCP with hard and intr mount options.
Maximum file size limitations. NFS Version 2 clients can only access the lowest 2GB of a file (signed 32 bit offset). NFS Version 3 clients support larger files (up to 64 bit offsets). The maximum supported file size also depends on the local file system of the NFS server.
Using NFS within a professional SAN environment or other storage system tends to offer greater reliability than using NFS outside of such an environment. However, NFS within a SAN environment may be slower than directly attached or bus-attached non-rotational storage.
If you choose to use NFS, NFS Version 4 or later is recommended, as is testing your NFS setup thoroughly before deploying into a production environment.
翻译如下,使用nfs需谨慎:
考虑将 MySQL 与 NFS 一起使用时,您应该谨慎行事。潜在问题因操作系统和 NFS 版本而异,包括以下几点:
MySQL 数据和日志文件放置在 NFS 卷上,可能会被锁定并无法使用。锁定问题可能发生在多个 MySQL 实例访问相同数据目录或 MySQL 因断电等原因不正确关闭的情况下。NFS 版本 4 通过引入咨询和基于租期的锁定来解决底层锁定问题。但是,不建议在 MySQL 实例之间共享数据目录。
由于消息乱序接收或网络流量丢失而导致的数据不一致。为了避免这个问题,请使用带有 hard 和 intr 挂载选项的 TCP。
最大文件大小限制。NFS 版本 2 客户端只能访问文件最低的 2GB(带符号的 32 位偏移量)。NFS 版本 3 客户端支持更大的文件(高达 64 位偏移量)。支持的最大文件大小还取决于 NFS 服务器的本地文件系统。
在专业 SAN 环境或其他存储系统中使用 NFS 通常比在该环境之外使用 NFS 更可靠。但是,SAN 环境中的 NFS 可能比直接连接或总线连接的非旋转存储更慢。
如果您选择使用 NFS,建议使用 NFS 4 或更高版本,并在部署到生产环境之前彻底测试您的 NFS 设置。
最终还是选择放弃挂载nas磁盘作为db数据盘,改为直接使用本地磁盘。
参考:
SQLITE_BUSY: database is locked · Issue #1678 · NginxProxyManager/nginx-proxy-manager (github.com)
MySQL :: MySQL 8.0 Reference Manual :: 10.12.1 Optimizing Disk I/O