使用 mariadb 10.4.8 docker 容器的用户“root”@“localhost”的访问被拒绝,使用 docker compose 并在附加外部卷时发出问题
Posted
技术标签:
【中文标题】使用 mariadb 10.4.8 docker 容器的用户“root”@“localhost”的访问被拒绝,使用 docker compose 并在附加外部卷时发出问题【英文标题】:Access denied for user 'root'@'localhost' with mariadb 10.4.8 docker container using docker compose and issue while attaching external volume 【发布时间】:2020-01-24 21:44:08 【问题描述】:我是 Docker 新手,我试图为我的应用程序创建 mariadb 的 docker 容器,但是当我开始运行 mariadb 容器时它显示 Access denied for user 'root'@'localhost'(使用密码:YES) dockerfile
以下是我正在使用的 docker compose。
version: '3'
services:
mysql:
image: mariadb
container_name: mariadb
volumes:
- dbvolume:/var/lib/mysql
- ./AppDatabase.sql:/docker-entrypoint-initdb.d/AppDatabase.sql
environment:
MYSQL_ROOT_PASSWORD: root123
MYSQL_ROOT_USER: root
MYSQL_USER: root
MYSQL_PASSWORD: root123
MYSQL_DATABASE: appdata
ports:
- "3333:3306"
volumes:
dbvolume:
通过引用几个链接多次尝试后,我能够将我的应用程序连接到 docker 容器,但在创建 docker 容器时无法导入 AppDatabase.sql 脚本。
但是现在通过使用相同的 docker compose 文件,我无法将 mariadb 连接到我的应用程序,而且我认为即使它没有将 SQL 脚本导入数据库(基于我观察到的日志)。
以下是运行 docker compose 时生成的 docker 日志:
$ docker logs 3fde358ff015
2019-09-24 17:40:37 0 [Note] mysqld (mysqld 10.4.8-MariaDB-1:10.4.8+maria~bionic) starting as process 1 ...
2019-09-24 17:40:37 0 [Note] InnoDB: Using Linux native AIO
2019-09-24 17:40:37 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-09-24 17:40:37 0 [Note] InnoDB: Uses event mutexes
2019-09-24 17:40:37 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-09-24 17:40:37 0 [Note] InnoDB: Number of pools: 1
2019-09-24 17:40:37 0 [Note] InnoDB: Using SSE2 crc32 instructions
2019-09-24 17:40:37 0 [Note] mysqld: O_TMPFILE is not supported on /tmp (disabling future attempts)
2019-09-24 17:40:37 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
2019-09-24 17:40:37 0 [Note] InnoDB: Completed initialization of buffer pool
2019-09-24 17:40:37 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2019-09-24 17:40:37 0 [Note] InnoDB: Upgrading redo log: 2*50331648 bytes; LSN=21810033
2019-09-24 17:40:38 0 [Note] InnoDB: Starting to delete and rewrite log files.
2019-09-24 17:40:38 0 [Note] InnoDB: Setting log file ./ib_logfile101 size to 50331648 bytes
2019-09-24 17:40:38 0 [Note] InnoDB: Setting log file ./ib_logfile1 size to 50331648 bytes
2019-09-24 17:40:38 0 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2019-09-24 17:40:38 0 [Note] InnoDB: New log files created, LSN=21810033
2019-09-24 17:40:38 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
2019-09-24 17:40:38 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2019-09-24 17:40:38 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2019-09-24 17:40:38 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2019-09-24 17:40:38 0 [Note] InnoDB: Waiting for purge to start
2019-09-24 17:40:38 0 [Note] InnoDB: 10.4.8 started; log sequence number 21810033; transaction id 14620
2019-09-24 17:40:38 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2019-09-24 17:40:38 0 [Note] Plugin 'FEEDBACK' is disabled.
2019-09-24 17:40:38 0 [Note] Server socket created on IP: '::'.
2019-09-24 17:40:38 0 [Warning] 'proxies_priv' entry '@% root@c980daa43351' ignored in --skip-name-resolve mode.
2019-09-24 17:40:38 0 [Note] InnoDB: Buffer pool(s) load completed at 190924 17:40:38
2019-09-24 17:40:38 0 [Note] Reading of all Master_info entries succeeded
2019-09-24 17:40:38 0 [Note] Added new Master_info '' to hash table
2019-09-24 17:40:38 0 [Note] mysqld: ready for connections.
Version: '10.4.8-MariaDB-1:10.4.8+maria~bionic' socket: '/var/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution
我正在尝试导入的 SQL 脚本:
create database appdata;
use appdata;
CREATE TABLE `appdatadetails` (
`Name` varchar(8) NOT NULL,
`appIndex` int(11) NOT NULL,
`connector` varchar(16) DEFAULT NULL,
`intName` varchar(12) DEFAULT NULL,
`intIndex` int(11) DEFAULT NULL,
PRIMARY KEY (`Name`,`appIndex`)
)
请帮助我了解我做错了什么,我已经尝试了在不同博客上发布的所有可能的解决方案。
更新:
最新更新:
我能够使用 10.1 启动并运行 mariadb docker 映像。但是,如果我附加音量,那么我仍然面临问题。
Docker 编写:
version: '3'
services:
mysql:
image: mariadb:10.1
container_name: mariadb
volumes:
- container-volume:/var/lib/mysql
- ./AppDatabase.sql:/docker-entrypoint-initdb.d/AppDatabase.sql
environment:
MYSQL_ROOT_PASSWORD: root123
MYSQL_DATABASE: appdata
ports:
- "3333:3306"
volumes:
container-volume:
还有日志错误消息,如果我附加 container-volume 卷。
Creating mariadb ... done
Attaching to mariadb
mariadb | 2019-09-25 6:56:26 140542855440384 [Note] mysqld (mysqld 10.1.41-MariaDB-1~bionic) starting as process 1 ...
mariadb | 2019-09-25 6:56:26 140542855440384 [Note] InnoDB: Using mutexes to ref count buffer pool pages
mariadb | 2019-09-25 6:56:26 140542855440384 [Note] InnoDB: The InnoDB memory heap is disabled
mariadb | 2019-09-25 6:56:26 140542855440384 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
mariadb | 2019-09-25 6:56:26 140542855440384 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
mariadb | 2019-09-25 6:56:26 140542855440384 [Note] InnoDB: Compressed tables use zlib 1.2.11
mariadb | 2019-09-25 6:56:26 140542855440384 [Note] InnoDB: Using Linux native AIO
mariadb | 2019-09-25 6:56:26 140542855440384 [Note] InnoDB: Using SSE crc32 instructions
mariadb | 2019-09-25 6:56:26 140542855440384 [Note] InnoDB: Initializing buffer pool, size = 256.0M
mariadb | 2019-09-25 6:56:26 140542855440384 [Note] InnoDB: Completed initialization of buffer pool
mariadb | 2019-09-25 6:56:26 140542855440384 [Note] InnoDB: Highest supported file format is Barracuda.
mariadb | InnoDB: No valid checkpoint found.
mariadb | InnoDB: A downgrade from MariaDB 10.2.2 or later is not supported.
mariadb | InnoDB: If this error appears when you are creating an InnoDB database,
mariadb | InnoDB: the problem may be that during an earlier attempt you managed
mariadb | InnoDB: to create the InnoDB data files, but log file creation failed.
mariadb | InnoDB: If that is the case, please refer to
mariadb | InnoDB: http://dev.mysql.com/doc/refman/5.6/en/error-creating-innodb.html
mariadb | 2019-09-25 6:56:26 140542855440384 [ERROR] Plugin 'InnoDB' init function returned error.
mariadb | 2019-09-25 6:56:26 140542855440384 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
mariadb | 2019-09-25 6:56:26 140542855440384 [Note] Plugin 'FEEDBACK' is disabled.
mariadb | 2019-09-25 6:56:26 140542855440384 [ERROR] Unknown/unsupported storage engine: InnoDB
mariadb | 2019-09-25 6:56:26 140542855440384 [ERROR] Aborting
mariadb |
mariadb exited with code 1
如果我删除 container-volume 那么它正在导入 .sql 脚本并且运行良好。
使用工作脚本更新:在我使用 mariadb 10.4.8 或最新版本并面临问题来访问数据库和附加外部卷之前。
现在我已经降级(正如@Adiii 所建议的那样)并尝试过。它运行完美,我们无需在卷服务中指定 external: true
version: '3'
services:
mysql:
image: mariadb:10.1
container_name: mariadb
volumes:
- ./dbvolume:/var/lib/mysql
- ./AppDatabase.sql:/docker-entrypoint-initdb.d/AppDatabase.sql
environment:
MYSQL_ROOT_PASSWORD: root123
MYSQL_DATABASE: appdata
ports:
- "3333:3306"
【问题讨论】:
似乎还没有合适的答案。这真的很难调试。 【参考方案1】:即使您创建新容器,它也会影响新用户和密钥。
删除安装位置,因为它会从此位置选择用户名和密码,也不会运行您的数据库初始化脚本。
volumes:
- dbvolume:/var/lib/mysql
你也不需要
create database appdata;
由于数据库已经创建于
MYSQL_ROOT_PASSWORD: root123
MYSQL_ROOT_USER: root
MYSQL_DATABASE: appdata
这一步。
更新:
删除用户 root,因为 root 已经定义。你可以试试
version: '3.7'
services:
mysql:
environment:
MYSQL_ROOT_PASSWORD: root123
MYSQL_DATABASE: appdata
image: mariadb
或者第二个用户应该不同
version: '3.7'
services:
mysql:
environment:
MYSQL_ROOT_PASSWORD: root123
MYSQL_DATABASE: appdata
MYSQL_USER: test
MYSQL_PASSWORD: root123
image: mariadb
你可以试试
docker exec -it container_name bash -c "mysql -u test -proot123"
或
docker exec -it mysql bash -c "mysql -u root -proot123"
如果仍然有问题,请删除数据库映像,拉一个新的。
或者试试图片标签10.1
如果我删除容器卷然后它正在导入 .sql 脚本和 运行良好。
如果挂载该位置,则初始化脚本将不会运行,因为容器期望已经存在数据库或尝试删除命名卷并创建新卷。
所以挂载位置,使用 MySQL 命令导入数据库,下次使用挂载位置。
【讨论】:
感谢您的重播,但我仍然无法访问 mariadb 容器。我不断收到 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 您要针对哪个用户登录?尝试调试docker exec -it mysql bash -c "mysql -u root -proot"
我已经用屏幕截图更新了我的帖子,我创建了一个文件夹并创建了 docker compose yml 文件并执行了 docker-compose up 并尝试访问容器,但它没有帮助。我已经使用了你的第二个脚本。
尝试删除,我测试过,你运行调试脚本了吗
使用密码yes很清楚,密码有问题。您可以尝试提供两个示例。一个应该工作以上是关于使用 mariadb 10.4.8 docker 容器的用户“root”@“localhost”的访问被拒绝,使用 docker compose 并在附加外部卷时发出问题的主要内容,如果未能解决你的问题,请参考以下文章
mariadb 作为 docker 容器 - 主机挂载绑定上的启动损坏
fbf centos7.2 二进制方式安装mariadb-10.4.8-linux-systemd-x86_64.tar.gz
从另一个 Docker 访问一个 Docker 中的 MariaDB