欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

docker安装mysql8.0

程序员文章站 2024-03-21 16:49:34
...
  1. 下载镜像
    docker pull mysql:8.0
  2. 创建docker-compose.yml文件,网络模式选择用host,不然可能会连接比较慢。
version: '2'
services:
  mysql:
    image: "mysql:8.0"
    hostname: mysql
    container_name: mysql
    ports:
      - 3307:3306
    volumes:
      - /wdata/server/mysql/conf/:/etc/mysql/
      - /wdata/server/mysql/data/:/var/lib/mysql/
    environment:
      - TZ=Asia/Shanghai
      - MYSQL_ROOT_PASSWORD=mycs123456
    restart: always
    network_mode: host

  1. 创建对应挂载目录
    mkdir -p /wdata/server/mysql/conf/conf.d/
    mkdir -p /wdata/server/mysql/data/
  2. 编写my.cnf文件
    vim /wdata/server/mysql/conf/my.cnf
[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL
port            = 3307

!includedir /etc/mysql/conf.d/

  1. 修改目录权限属组为999,容器mysql用户的uid是999
    chown -R 999 /wdata/server/mysql/
  2. 启动容器
    docker安装mysql8.0
  3. 授权用户,8.0版本创建用户和授权要分开写,密码方式也和5.5版本有区别
    create user ‘aulgx’@’%’ identified by ‘passwd’;
    GRANT all PRIVILEGES ON . TO ‘aulgx’@’%’;
    ALTER USER ‘aulgx’@’%’ IDENTIFIED WITH mysql_native_password BY ‘passwd’;
  4. 如果不加WITH mysql_native_password 的话连接会报密码错误或者下面的错
    ERROR 2059 (HY000): Authentication plugin ‘caching_sha2_password’ cannot be loaded: /wdata/server/mysql/lib/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
    docker安装mysql8.0
  5. 我一开始就是没加WITH mysql_native_password的,加上后就可以连接了
    docker安装mysql8.0
相关标签: 数据库 docker