查找指定的配置文件
find / -name docker-compose-*
find / -name my.cnf
创建基础文件
mkdir mysql
mkdir -p mysql/data
获取默认的my.cnf
docker run --name mysql -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:latest
docker cp mysql:/etc/my.cnf ./
docker cp mysql:/var/lib/mysql ./data
sudo chmod 640 /etc/my.cnf
vim mysql/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.1/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.1/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
# my.cnf的来源 docker cp mysql:/etc/mysql/my.cnf ./
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock
!includedir /etc/mysql/conf.d/
docker-compose-base.yml
version: '3'
services:
nacos_host:
image: nacos/nacos-server:v2.2.0
restart: always
container_name: nacos_host
environment:
- MODE=standalone
- PREFER_HOST_MODE=hostname
volumes:
- ./sores/nacos/log:/home/nacos/logs
ports:
- 8848:8848
- 9848:9848 #2.0新增了两个端口,需要暴露出来
- 9849:9849 #2.0新增了两个端口,需要暴露出来
networks:
- qar-net
depends_on:
- redis_host
nginx_host:
image: nginx:1.23.4
restart: always
container_name: nginx_host
volumes:
- ./sores/nginx/html:/usr/share/nginx/html
- ./sores/nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf
# - ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf
- ./sores/nginx/logs:/var/log/nginx
ports:
- 80:80
# - 81:81
networks:
- qar-net
redis_host:
image: redis:7.0.4
container_name: redis_host
restart: always
command: redis-server /usr/local/etc/redis.conf
# command: redis-server --appendonly no
volumes:
- ./sores/redis/conf:/usr/local/etc
- ./sores/redis/data:/data
ports:
- 6379:6379
networks:
- qar-net
pgsql_host:
image: postgres:10.12
container_name: pgsql_host
restart: always
environment:
- POSTGRES_PASSWORD=qar123456
volumes:
- ./sores/postgres/data:/var/lib/postgresql/data
ports:
- 5432:5432
networks:
- qar-net
mysql_host:
image: mysql:latest
container_name: mysql_host
restart: always
environment:
- MYSQL_ROOT_PASSWORD=123456
volumes:
- ./sores/mysql/data/mysql:/var/lib/mysql
- ./sores/mysql/my.cnf:/etc/my.cnf
ports:
- 3306:3306
networks:
- qar-net
networks:
qar-net:
external: true
docker-compose -f docker-compose-base.yml up -d mysql_host
docker exec -it mysql_host bash
mysql -uroot -p
password: 123456
# Access denied for user 'root'@'localhost' (using password:YES)
mysql -uroot -p
直接回车输入密码
进入:
> use mysql;
> alter user root@'localhost' identified by '123456'
> exit
# 重新启动服务
docker-compose -f docker-compose-base.yml up -d mysql_host
docker exec -it mysql_host bash
mysql -uroot -p
password: 123456
正常
> CREATE DATABASE mydb DEFAULT CHARACTER SET utf8;
use mydb;
CREATE TABLE `customers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(255),
`last_name` varchar(255),
`email` varchar(255),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
insert into `customers` values(null, "Sally","Thomas", "sally.thomas@acme.com");