mysql 无法连接到Docker上的MariaDB

wbrvyc0a  于 2023-03-22  发布在  Mysql
关注(0)|答案(1)|浏览(156)

我有下面的docker-compose-yml:

version: '3.1'

services:

  wordpress:
    image: wordpress
    restart: always
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wp_user
      WORDPRESS_DB_PASSWORD: password
      WORDPRESS_DB_NAME: wp_db
    volumes:
      - ./wordpress:/var/www/html
    networks:
      - my_network

  db:
    image: mariadb:10.7.8
    restart: always
    environment:
      MYSQL_DATABASE: wp_db
      MYSQL_USER: wp_user
      MYSQL_PASSWORD: password
      MYSQL_ROOT_PASSWORD: password
      MARIADB_MYSQL_LOCALHOST_USER: 1
      MARIADB_MYSQL_LOCALHOST_GRANTS: "RELOAD, PROCESS, LOCK TABLES, BINLOG MONITOR"
    volumes:
      - ./data:/docker-entrypoint-initdb.d
      - ./datadir:/var/lib/mysql
      - backup:/backup
    networks:
      - my_network

volumes:
  backup:

networks:
  my_network:
    driver: bridge

这样就可以正常工作了。站点正在运行并使用数据库。
当我运行命令时:

docker compose run db mariabackup --backup --target-dir=/backup

我得到了以下结果:

[00] 2023-03-18 16:32:15 Connecting to MariaDB server host: localhost, user: not set, password: not set, port: not set, socket: /run/mysqld/mysqld.sock
[00] 2023-03-18 16:32:15 Failed to connect to MariaDB server: Can't connect to local server through socket '/run/mysqld/mysqld.sock' (2 "No such file or directory").

我不知道为什么套接字被定义成这样,我想应该是'/var/run/mysqld/mysqld. sock'?
任何帮助感激不尽!

6jjcrrmo

6jjcrrmo1#

根据在容器中获取备份的documentation

docker run --user mysql -v some-mariadb-socket:/var/run/mysqld -v some-mariadb-backup:/backup -v /my/own/datadir:/var/lib/mysql --rm mariadb:latest mariabackup --backup --target-dir=/backup

因为我们有一个套接字问题,如果你想使用compose,尝试在db服务中添加新的套接字卷:

volumes:
      - ./data:/docker-entrypoint-initdb.d
      - ./datadir:/var/lib/mysql
      - backup:/backup
      - some-mariadb-socket:/var/run/mysqld

并将some-mariadb-socket添加到volumes

volumes:
  backup:
  some-mariadb-socket:

试试看:

docker compose exec db mariabackup --backup --target-dir=/backup

然后是exec而不是run

相关问题