当前位置:   article > 正文

docker启动mysql服务_docker run mysql

docker run mysql

查找指定的配置文件

find / -name docker-compose-*
find / -name my.cnf

创建基础文件

mkdir mysql
mkdir -p mysql/data
  • 1
  • 2

获取默认的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

  • 1
  • 2
  • 3
  • 4
  • 5

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/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
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");

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/710240
推荐阅读
相关标签
  

闽ICP备14008679号