创建基础文件
mkdir mysql
mkdir -p mysql/data
获取默认的my.cnf
docker run -name mysql -d -p 3306:3306 mysql:latest
docker cp mysql:/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=mysqlpid-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.0restart: alwayscontainer_name: nacos_hostenvironment:- MODE=standalone- PREFER_HOST_MODE=hostnamevolumes:- ./sores/nacos/log:/home/nacos/logsports:- 8848:8848- 9848:9848 #2.0新增了两个端口,需要暴露出来- 9849:9849 #2.0新增了两个端口,需要暴露出来networks:- qar-netdepends_on:- redis_hostnginx_host:image: nginx:1.23.4restart: alwayscontainer_name: nginx_hostvolumes:- ./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/nginxports:- 80:80# - 81:81networks:- qar-netredis_host:image: redis:7.0.4container_name: redis_hostrestart: alwayscommand: redis-server /usr/local/etc/redis.conf# command: redis-server --appendonly novolumes:- ./sores/redis/conf:/usr/local/etc- ./sores/redis/data:/dataports:- 6379:6379networks:- qar-netpgsql_host:image: postgres:10.12container_name: pgsql_hostrestart: alwaysenvironment:- POSTGRES_PASSWORD=qar123456volumes:- ./sores/postgres/data:/var/lib/postgresql/dataports:- 5432:5432networks:- qar-netmysql_host:image: mysql:latestcontainer_name: mysql_hostrestart: alwaysenvironment:- MYSQL_ROOT_PASSWORD=123456volumes:- ./sores/mysql/data:/var/lib/mysql- ./sores/mysql/my.cnf:/etc/my.cnfports:- 3306:3306networks:- qar-netnetworks: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;