docker配置mysql8报错 ERROR 2002 (HY000)

通过docker启动的mysql,发现navicat无法连接,后来进入容器内部也是无法连接,产生以下错误

bash 复制代码
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)

查看容器日志信息显示如下

bash 复制代码
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配置文件如下

bash 复制代码
[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启动的配置文件

yaml 复制代码
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
相关推荐
一叶飘零_sweeeet2 小时前
Docker 部署 Java 项目实践
java·docker
Toky Zhu3 小时前
修改 Docker 镜像默认存储位置的方法
spring cloud·docker·eureka
Toormi3 小时前
在k8s环境中如何在本地和pod之间同步文件?
云原生·容器·kubernetes
少陽君3 小时前
k8s Service四层负载:服务端口暴露
云原生·容器·kubernetes
小蜜蜂爱编程4 小时前
记录一个狗血的docker问题
运维·docker·容器
菜菜-plus7 小时前
微服务技术,SpringCloudAlibaba,Redis,RocketMQ,Docker,分库分表
java·spring boot·redis·spring cloud·docker·微服务·java-rocketmq
瑕、疵7 小时前
使用Docker Compose简化微服务部署
docker·微服务·容器
甲柒9 小时前
12-Docker发布微服务
java·docker·微服务
Arc星语11 小时前
Docker Redis集群3主3从模式
redis·docker
工作不忙14 小时前
不使用docker-compose不使用zookeeper启动ApacheKafka3.8.0单机运行KRAFT模式
ubuntu·docker·zookeeper·kafka·apache