最近换了mac m1芯片,docker的mysql镜像不支持arm64架构,被迫使用mariadb(mysql的一个分支,完全兼容mysql)
1.背景
最近使用docker起的mysql连接不上去了,每次连接都出现连接不上的问题
% mysql -h127.0.0.1 -P3306 -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
以为是密码出现问题了,我就去看了一下配置的信息,确实是root,并且通过容器镜像去登陆mysql是正常的
% docker exec -it 2fb224eb4869 /bin/bash
root@2fb224eb4869:/# ls
bin boot dev docker-entrypoint-initdb.d etc home lib media mnt opt proc root run sbin srv sys tmp usr var
root@2fb224eb4869:/# mysql -uroot -proot
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.5.11-MariaDB-1:10.5.11+maria~focal mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
于是我以为端口映射出现了错误了,于是我又看了一下docker的端口映射
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2fb224eb4869 arm64v8/mariadb:latest "docker-entrypoint.s…" 19 minutes ago Up 19 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp mysql
端口映射也是没有问题的!!!那为什么会出现这个问题呢?
2.解决
出于一个偶然操作,我把-p去掉了(其实是错误的命令),但是他连上了。于是急忙去看一下mysql的数据库,突然间就发现不对劲了。
原来我docker映射3306端口是错误的,以为我本机启动了localhost mysql服务,而mysql服务默认的端口是3306,所以我宿主机去连docker的mysql的时候,宿主机一直以为我要连本地的mysql。(至于为什么没有-p就进去了,就碰巧我偷懒了,本地的localhost mysql 没有设置密码!!!)
建议:docker映射出来的mysql不要使用3306端口,不论本地是否起了mysql服务!