docker-compose文件如下:
1 version: '3' 2 services: 3 # mysql服务 4 service-mysql: 5 image: docker.io/mysql:8.0.27 6 command: 7 --default-authentication-plugin=mysql_native_password 8 --sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION 9 --max_connections=2000 10 ports: 11 - '3306:3306' 12 environment: 13 - MYSQL_ROOT_PASSWORD=root 14 - MYSQL_USER: 'test' 15 - MYSQL_PASS: 'test' 16 privileged: true 17 volumes: 18 - /etc/localtime:/etc/localtime:ro 19 - /etc/timezone:/etc/timezone:ro 20 - ./conf/mysql.cnf:/etc/mysql/my.cnf 21 - ./data:/var/lib/mysql 22 restart: always 23 container_name: service-mysql 24 networks: 25 - custom_network 26 27 # mysql phpmyadmin服务 28 service-mysql-admin: 29 image: phpmyadmin/phpmyadmin 30 ports: 31 - '3307:80' 32 environment: 33 - PMA_ARBITRARY=1 34 - PMA_HOST=service-mysql 35 volumes: 36 - /etc/localtime:/etc/localtime:ro 37 depends_on: 38 - service-mysql 39 restart: always 40 container_name: service-mysql-admin 41 networks: 42 - custom_network 43 44 networks: 45 custom_network: 46 external: 47 name: service
my.cn映射如下:
[mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql skip-character-set-client-handshake skip-name-resolve secure-file-priv= NULL sql_mode =STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION max_connections = 2000
配置成功后,使用docker up -d安装,安装成功后,通过docker ps -a查看安装运行情况:
此时安装成功,正常连接mysql即可。
可能会遇到问题:
docker-compose文件中设置的密码无效、远程方式无效
解决方案:
在映射的my.cnf文件中,加入 :skip-grant-tables 跳过密码验证
重启docker容器,通过:docker exec -it service-mysql bash(注:ocker exec -it 容器名称 bash) 进入容器,登录mysql,此时无需密码
登录mysql后执行命令:
alter user 'root'@'localhost' identified with mysql_native_password by 'youpassword';
刷新权限:flush privileges;
设置远程可登录:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
刷新权限:flush privileges;
全部执行完毕后,重启docker容器,再测试连接:
标签:dockercompose,service,NO,mysql8,etc,ZERO,初学,mysql,docker From: https://www.cnblogs.com/wql-987426546/p/16891686.html