通过docker启动的mysql,发现navicat无法连接,后来进入容器内部也是无法连接,产生以下错误
root@9f3b90339a14:/var/run/mysqld# mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
查看容器日志信息显示如下
root@root:/opt/mysql# docker logs ef36bee61984
2024-10-22 22:59:48+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.20-1debian10 started.
2024-10-22 22:59:49+08:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2024-10-22 22:59:49+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.20-1debian10 started.
2024-10-22T14:59:49.286487Z 0 [Warning] [MY-010099] [Server] Insecure configuration for --secure-file-priv: Data directory is accessible through --secure-file-priv. Consider choosing a different directory.
2024-10-22T14:59:49.286497Z 0 [Warning] [MY-010101] [Server] Insecure configuration for --secure-file-priv: Location is accessible to all OS users. Consider choosing a different directory.
2024-10-22T14:59:49.286552Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.20) starting as process 1
2024-10-22T14:59:49.292891Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2024-10-22T14:59:50.543534Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-10-22T14:59:50.752764Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
2024-10-22T14:59:51.174812Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2024-10-22T14:59:51.252468Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.20' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
看这个样子好像是一些安全东西引起的,导致拒绝密码连接
看到日志里有个 /var/run/mysqld/mysqlx.sock。貌似是需要绑定这个路径,以前还没有注意到,因此在配置文件中绑定这个路径
my.cnf配置文件如下
[mysqld]
user=mysql
default-storage-engine=INNODB
character-set-server=utf8
secure_file_priv=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
[client]
default-character-set=utf8
socket=/var/run/mysqld/mysqld.sock
[mysql]
default-character-set=utf8
socket=/var/run/mysqld/mysqld.sock
配置好这个文件之后就可以正常连接了
下面附带利用docker-compose启动的配置文件
version: '3'
services:
mysql:
image: mysql:8.0.20
restart: always
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: 123456
TZ: Asia/Shanghai
ports:
- 3306:3306
volumes:
- ./data:/var/lib/mysql
- ./config/my.cnf:/etc/mysql/my.cnf
- ./mysql-files:/var/lib/mysql-files
command:
--max_connections=1000
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--default-authentication-plugin=mysql_native_password
--server-id=1
--log-bin=master-binlog
--innodb-flush-log-at-trx-commit=1
--sync-binlog=1
--innodb-file-per-table=1
--innodb-buffer-pool-size=2G